实现几类图像增广
常用图像增广方法主要有:左右翻转(上下翻转对于许多目标并不常用),随机裁剪,变换颜色(亮度,对比度,饱和度和色调)等等,我们拟用opencv-python实现部分数据增强方法。
结构如下:
class FunctionClass:
def __init__(self, parameter):
self.parameter=parameter
def __call__(self, img):
0 demo示例
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
filename = '2.jpg'
## [Load an image from a file]
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
print(img.shape)
(1080, 1920, 3)
1.图片缩放
class Resize():
def __init__(self, size):
self.size=size
def __call__(self, img):
# 此处插入代码
return cv2.resize(img,self.size)
resize=Resize((600, 600))
img2=resize(img)
plt.imshow(img2)
print(img2.shape)
(600, 600, 3)
2.图片翻转
class Flip:
def __init__(self, mode):
self.mode=mode
def __call__(self, img):
# 此处插入代码
dst = cv2.flip(img, 0)
return dst
flip=Flip(mode=0)
img2=flip(img)
plt.imshow(img2)
3 图片旋转
class Rotate:
def __init__(self, degree,size):
self.degree=degree
self.size=size
def __call__(self, img):
# 此处插入代码
rows,cols,h = img.shape
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),45, 0.7)
dst = cv2.warpAffine(img,M,(cols,rows))
return dst
rotate=Rotate( 45, 0.7)
img2=rotate(img)
plt.imshow(img2)
4.图片亮度调节
class Brightness:
def __init__(self,brightness_factor):
self.brightness_factor=brightness_factor
def __call__(self, img):
# 此处插入代码
rows, cols, chunnel = img.shape
blank = np.zeros([rows, cols, chunnel], img.dtype)
dst = cv2.addWeighted(img, 0.6, blank,0.6,0.6)
return dst
brightness=Brightness(0.6)
img2=brightness(img)
plt.imshow(img2)
5 图片随机裁剪
import random
import math
class RandCropImage(object):
""" random crop image """
""" 随机裁剪图片 """
def __init__(self, size, scale=None, ratio=None, interpolation=-1):
self.interpolation = interpolation if interpolation >= 0 else None
if type(size) is int:
self.size = (size, size) # (h, w)
else:
self.size = size
self.scale = [0.08, 1.0] if scale is None else scale
self.ratio = [3. / 4., 4. / 3.] if ratio is None else ratio
def __call__(self, img):
size = self.size
scale = self.scale
ratio = self.ratio
aspect_ratio = math.sqrt(random.uniform(*ratio))
w = 1. * aspect_ratio
h = 1. / aspect_ratio
img_h, img_w = img.shape[:2]
bound = min((float(img_w) / img_h) / (w**2),
(float(img_h) / img_w) / (h**2))
scale_max = min(scale[1], bound)
scale_min = min(scale[0], bound)
target_area = img_w * img_h * random.uniform(scale_min, scale_max)
target_size = math.sqrt(target_area)
w = int(target_size * w)
h = int(target_size * h)
i = random.randint(0, img_w - w)
j = random.randint(0, img_h - h)
img = img[j:j + h, i:i + w, :]
if self.interpolation is None:
return cv2.resize(img, size)
else:
return cv2.resize(img, size, interpolation=self.interpolation)
crop = RandCropImage(350)
rn cv2.resize(img, size, interpolation=self.interpolation)
crop = RandCropImage(350)
plt.imshow(crop(img))
6 结语
数据增强的方法有很多种,需要结合数据特征具体情况选择合适的方法使用。
附上paddle的api文档链接:paddle图像增广文档
运行代码请点击:本文项目