热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

图像增强代码汇总

##############模块:数据增强#相关函数:#功能:#1、#随机改变亮暗、对比度和颜色等defrandom_distor

#############
#模块:数据增强
#相关函数:
#功能:
#1、# 随机改变亮暗、对比度和颜色等 def random_distort(img):
#2、# 随机填充 def random_expand(#############
import numpy as np
import cv2
from PIL import Image, ImageEnhance
import random# 随机改变亮暗、对比度和颜色等
def random_distort(img):# 随机改变亮度def random_brightness(img, lower=0.5, upper=1.5):e = np.random.uniform(lower, upper)return ImageEnhance.Brightness(img).enhance(e)# 随机改变对比度def random_contrast(img, lower=0.5, upper=1.5):e = np.random.uniform(lower, upper)return ImageEnhance.Contrast(img).enhance(e)# 随机改变颜色def random_color(img, lower=0.5, upper=1.5):e = np.random.uniform(lower, upper)return ImageEnhance.Color(img).enhance(e)ops = [random_brightness, random_contrast, random_color]np.random.shuffle(ops)img = Image.fromarray(img)img = ops[0](img)img = ops[1](img)img = ops[2](img)img = np.asarray(img)return img# 随机填充
def random_expand(img,gtboxes,max_ratio&#61;4.,fill&#61;None,keep_ratio&#61;True,thresh&#61;0.5):if random.random() > thresh:return img, gtboxesif max_ratio < 1.0:return img, gtboxesh, w, c &#61; img.shaperatio_x &#61; random.uniform(1, max_ratio)if keep_ratio:ratio_y &#61; ratio_xelse:ratio_y &#61; random.uniform(1, max_ratio)oh &#61; int(h * ratio_y)ow &#61; int(w * ratio_x)off_x &#61; random.randint(0, ow - w)off_y &#61; random.randint(0, oh - h)out_img &#61; np.zeros((oh, ow, c))if fill and len(fill) &#61;&#61; c:for i in range(c):out_img[:, :, i] &#61; fill[i] * 255.0out_img[off_y:off_y &#43; h, off_x:off_x &#43; w, :] &#61; imggtboxes[:, 0] &#61; ((gtboxes[:, 0] * w) &#43; off_x) / float(ow)gtboxes[:, 1] &#61; ((gtboxes[:, 1] * h) &#43; off_y) / float(oh)gtboxes[:, 2] &#61; gtboxes[:, 2] / ratio_xgtboxes[:, 3] &#61; gtboxes[:, 3] / ratio_yreturn out_img.astype(&#39;uint8&#39;), gtboxes#随机裁剪
#随机裁剪之前需要先定义两个函数&#xff0c;multi_box_iou_xywh和box_crop这两个函数将被保存在box_utils.py文件中。
import numpy as npdef multi_box_iou_xywh(box1, box2):"""In this case, box1 or box2 can contain multi boxes.Only two cases can be processed in this method:1, box1 and box2 have the same shape, box1.shape &#61;&#61; box2.shape2, either box1 or box2 contains only one box, len(box1) &#61;&#61; 1 or len(box2) &#61;&#61; 1If the shape of box1 and box2 does not match, and both of them contain multi boxes, it will be wrong."""assert box1.shape[-1] &#61;&#61; 4, "Box1 shape[-1] should be 4."assert box2.shape[-1] &#61;&#61; 4, "Box2 shape[-1] should be 4."b1_x1, b1_x2 &#61; box1[:, 0] - box1[:, 2] / 2, box1[:, 0] &#43; box1[:, 2] / 2b1_y1, b1_y2 &#61; box1[:, 1] - box1[:, 3] / 2, box1[:, 1] &#43; box1[:, 3] / 2b2_x1, b2_x2 &#61; box2[:, 0] - box2[:, 2] / 2, box2[:, 0] &#43; box2[:, 2] / 2b2_y1, b2_y2 &#61; box2[:, 1] - box2[:, 3] / 2, box2[:, 1] &#43; box2[:, 3] / 2inter_x1 &#61; np.maximum(b1_x1, b2_x1)inter_x2 &#61; np.minimum(b1_x2, b2_x2)inter_y1 &#61; np.maximum(b1_y1, b2_y1)inter_y2 &#61; np.minimum(b1_y2, b2_y2)inter_w &#61; inter_x2 - inter_x1inter_h &#61; inter_y2 - inter_y1inter_w &#61; np.clip(inter_w, a_min&#61;0., a_max&#61;None)inter_h &#61; np.clip(inter_h, a_min&#61;0., a_max&#61;None)inter_area &#61; inter_w * inter_hb1_area &#61; (b1_x2 - b1_x1) * (b1_y2 - b1_y1)b2_area &#61; (b2_x2 - b2_x1) * (b2_y2 - b2_y1)return inter_area / (b1_area &#43; b2_area - inter_area)def box_crop(boxes, labels, crop, img_shape):x, y, w, h &#61; map(float, crop)im_w, im_h &#61; map(float, img_shape)boxes &#61; boxes.copy()boxes[:, 0], boxes[:, 2] &#61; (boxes[:, 0] - boxes[:, 2] / 2) * im_w, (boxes[:, 0] &#43; boxes[:, 2] / 2) * im_wboxes[:, 1], boxes[:, 3] &#61; (boxes[:, 1] - boxes[:, 3] / 2) * im_h, (boxes[:, 1] &#43; boxes[:, 3] / 2) * im_hcrop_box &#61; np.array([x, y, x &#43; w, y &#43; h])centers &#61; (boxes[:, :2] &#43; boxes[:, 2:]) / 2.0mask &#61; np.logical_and(crop_box[:2] <&#61; centers, centers <&#61; crop_box[2:]).all(axis&#61;1)boxes[:, :2] &#61; np.maximum(boxes[:, :2], crop_box[:2])boxes[:, 2:] &#61; np.minimum(boxes[:, 2:], crop_box[2:])boxes[:, :2] -&#61; crop_box[:2]boxes[:, 2:] -&#61; crop_box[:2]mask &#61; np.logical_and(mask, (boxes[:, :2] < boxes[:, 2:]).all(axis&#61;1))boxes &#61; boxes * np.expand_dims(mask.astype(&#39;float32&#39;), axis&#61;1)labels &#61; labels * mask.astype(&#39;float32&#39;)boxes[:, 0], boxes[:, 2] &#61; (boxes[:, 0] &#43; boxes[:, 2]) / 2 / w, (boxes[:, 2] - boxes[:, 0]) / wboxes[:, 1], boxes[:, 3] &#61; (boxes[:, 1] &#43; boxes[:, 3]) / 2 / h, (boxes[:, 3] - boxes[:, 1]) / hreturn boxes, labels, mask.sum()
# 随机裁剪
def random_crop(img,boxes,labels,scales&#61;[0.3, 1.0],max_ratio&#61;2.0,constraints&#61;None,max_trial&#61;50):if len(boxes) &#61;&#61; 0:return img, boxesif not constraints:constraints &#61; [(0.1, 1.0), (0.3, 1.0), (0.5, 1.0), (0.7, 1.0),(0.9, 1.0), (0.0, 1.0)]img &#61; Image.fromarray(img)w, h &#61; img.sizecrops &#61; [(0, 0, w, h)]for min_iou, max_iou in constraints:for _ in range(max_trial):scale &#61; random.uniform(scales[0], scales[1])aspect_ratio &#61; random.uniform(max(1 / max_ratio, scale * scale), \min(max_ratio, 1 / scale / scale))crop_h &#61; int(h * scale / np.sqrt(aspect_ratio))crop_w &#61; int(w * scale * np.sqrt(aspect_ratio))crop_x &#61; random.randrange(w - crop_w)crop_y &#61; random.randrange(h - crop_h)crop_box &#61; np.array([[(crop_x &#43; crop_w / 2.0) / w,(crop_y &#43; crop_h / 2.0) / h,crop_w / float(w), crop_h / float(h)]])iou &#61; multi_box_iou_xywh(crop_box, boxes)if min_iou <&#61; iou.min() and max_iou >&#61; iou.max():crops.append((crop_x, crop_y, crop_w, crop_h))breakwhile crops:crop &#61; crops.pop(np.random.randint(0, len(crops)))crop_boxes, crop_labels, box_num &#61; box_crop(boxes, labels, crop, (w, h))if box_num < 1:continueimg &#61; img.crop((crop[0], crop[1], crop[0] &#43; crop[2],crop[1] &#43; crop[3])).resize(img.size, Image.LANCZOS)img &#61; np.asarray(img)return img, crop_boxes, crop_labelsimg &#61; np.asarray(img)return img, boxes, labels# 随机缩放
def random_interp(img, size, interp&#61;None):interp_method &#61; [cv2.INTER_NEAREST,cv2.INTER_LINEAR,cv2.INTER_AREA,cv2.INTER_CUBIC,cv2.INTER_LANCZOS4,]if not interp or interp not in interp_method:interp &#61; interp_method[random.randint(0, len(interp_method) - 1)]h, w, _ &#61; img.shapeim_scale_x &#61; size / float(w)im_scale_y &#61; size / float(h)img &#61; cv2.resize(img, None, None, fx&#61;im_scale_x, fy&#61;im_scale_y, interpolation&#61;interp)return img# 随机翻转
def random_flip(img, gtboxes, thresh&#61;0.5):if random.random() > thresh:img &#61; img[:, ::-1, :]gtboxes[:, 0] &#61; 1.0 - gtboxes[:, 0]return img, gtboxes# 随机打乱真实框排列顺序
def shuffle_gtbox(gtbox, gtlabel):gt &#61; np.concatenate([gtbox, gtlabel[:, np.newaxis]], axis&#61;1)idx &#61; np.arange(gt.shape[0])np.random.shuffle(idx)gt &#61; gt[idx, :]return gt[:, :4], gt[:, 4]# 图像增广方法汇总
def image_augment(img, gtboxes, gtlabels, size, means&#61;None):# 随机改变亮暗、对比度和颜色等img &#61; random_distort(img)# 随机填充img, gtboxes &#61; random_expand(img, gtboxes, fill&#61;means)# 随机裁剪img, gtboxes, gtlabels, &#61; random_crop(img, gtboxes, gtlabels)# 随机缩放img &#61; random_interp(img, size)# 随机翻转img, gtboxes &#61; random_flip(img, gtboxes)# 随机打乱真实框排列顺序gtboxes, gtlabels &#61; shuffle_gtbox(gtboxes, gtlabels)return img.astype(&#39;float32&#39;), gtboxes.astype(&#39;float32&#39;), gtlabels.astype(&#39;int32&#39;)


推荐阅读
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
author-avatar
阿朵爸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有