数据增强--opencv自定义编写

opencv自定义数据增强

目录

  1. 旋转
  2. 模糊处理
  3. 增加噪声
  4. 光照调整
  5. 使用方法
  6. padding为同样大小图片

opencv官方api

1. 旋转

def rotate(xb, yb, angle):
	# xb, yb为原图及label图片,angle为逆时针旋转角度
	M_rotate = cv2.getRotationMatrix2D((img_w/2,img_h/2),angle,1)
	xb = cv2.warpAffine(xb, M_rotate, (img_w, img_h))
	yb = cv2.warpAffine(yb, M_rotate,(img_w,img_h))
	return xb,yb

1) getRotationMatrix2D( point, angle, scale ):获得图像绕着中心点的旋转矩阵,angle方向为逆时针。该步骤获取旋转矩阵。
2)cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]):图像仿射变换,使用上述旋转矩阵进行变换。
在这里插入图片描述

https://www.cnblogs.com/sdu20112013/p/11378474.html
https://www.cnblogs.com/dupuleng/articles/4055020.html

2. 模糊处理

def blur(img):
	img = cv2.blur(img, (3, 3));
	return img

1)cv2.blur(img, ksize): 图像均值平滑滤波。img为原图像,ksize为核大小。用目标像素点及周围像素点(由ksize决定)均值代替目标像素点。
2)opencv提供了四种模糊技术:cv2.blur(), cv2.GaussianBlur(), cv2.medianBlur(), cv2.bilateralFilter()。

https://www.jianshu.com/p/4ae5e8cef9ae

# 高斯模糊:每个像素点的值由本身和邻域内的其他像素值经过加权平均后得到。
> dst = cv.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])

* src: 图像矩阵
* ksize: 滤波窗口尺寸
* sigmax: 标准差

# 中值滤波:将数字图像或数字序列中一点的值用该点的一个邻域中各点值的中值代替。
> dst = cv.medianBlur(src, ksize[, dst] )

* src:图像矩阵
* ksize:滤波窗口尺寸

# 双边滤波:结合图像的空间邻近度和像素值相似度的一种折衷处理。
> dst = cv.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])

* src:图像矩阵
* d:邻域直径
* sigmaColor:颜色标准差
* sigmaSpace:空间标准差

3. 添加噪声

def add_noise(img):
	for i in range(200): 
		temp_x = np.random.randint(0,img.shape[0])
		temp_y = np.random.randint(0,img.shape[1])
		img[temp_x][temp_y] = 255
		return img

4. 光照调整/gamma变换

# gamma变换,先归一化到1,然后gamma作为指数值求出新的像素值再还原。最后使用opencv的查表函数进行映射
def gamma_transform(img, gamma):
	gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
	gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
	return cv2.LUT(img, gamma_table)
	
def random_gamma_transform(img, gamma_vari):
	log_gamma_vari = np.log(gamma_vari)
	alpha = np.random.uniform(-log_gamma_vari, log_gamma_vari)
	gamma = np.exp(alpha)
	return gamma_transform(img, gamma)

1) cv2.LUT(): 将一组RGB值输出为另一组RGB值,从而改变画面的曝光与色彩
2)gamma值以1为分界,值越小,对图像低灰度部分的扩展作用就越强,值越大,对图像高灰度部分的扩展作用就越强,通过不同的γ值,就可以达到增强低灰度或高灰度部分细节的作用

5. 使用方法

def data_augment(xb,yb):
	if np.random.random() < 0.25:
		xb,yb = rotate(xb,yb,90)
	if np.random.random() < 0.25:
		xb,yb = rotate(xb,yb,180)
	if np.random.random() < 0.25:
		xb,yb = rotate(xb,yb,270)
	if np.random.random() < 0.25:
		xb = cv2.flip(xb, 1) # flipcode > 0ғဠy᫰ᘉ᫨
		yb = cv2.flip(yb, 1)
	
	if np.random.random() < 0.25:
		xb = random_gamma_transform(xb,1.0)
	
	if np.random.random() < 0.25:
		xb = blur(xb)
	
	if np.random.random() < 0.2:
		xb = add_noise(xb)
	
	return xb, yb
def creat_dataset(image_num = 100000, mode = 'original'):
	print('creating dataset...')
	image_each = image_num / len(image_sets)
	g_count = 0
	for i in tqdm(range(len(image_sets))):
		count = 0
		src_img = cv2.imread('./data/src/' + image_sets[i]) # 3 channels
		label_img = cv2.imread('./data/label/' + image_sets[i],cv2.IMREAD_GRAYSCALE) # single channel
		X_height,X_width,_ = src_img.shape
		while count < image_each:
			# 随机裁切
			random_width = random.randint(0, X_width - img_w - 1)
			random_height = random.randint(0, X_height - img_h - 1)
			src_roi = src_img[random_height: random_height + img_h, random_width: random_width + img_w,:]
			label_roi = label_img[random_height: random_height + img_h, random_width: random_width + img_w]
			if mode == 'augment':
				src_roi,label_roi = data_augment(src_roi,label_roi)
			
			visualize = np.zeros((256,256)).astype(np.uint8)
			visualize = label_roi *50
		
			cv2.imwrite(('./aug/train/visualize/%d.png' % g_count),visualize)
			cv2.imwrite(('./aug/train/src/%d.png' % g_count),src_roi)
			cv2.imwrite(('./aug/train/label/%d.png' % g_count),label_roi)
			count += 1
			g_count += 1

6. padding为同样大小

def preprocess_image(image, image_size):
    # image, RGB
    image_height, image_width = image.shape[:2]
    if image_height > image_width:
        scale = image_size / image_height
        resized_height = image_size
        resized_width = int(image_width * scale)
    else:
        scale = image_size / image_width
        resized_height = int(image_height * scale)
        resized_width = image_size

    image = cv2.resize(image, (resized_width, resized_height))
    image = image.astype(np.float32)
    image /= 255.
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    image -= mean
    image /= std
    pad_h = image_size - resized_height
    pad_w = image_size - resized_width
    image = np.pad(image, [(0, pad_h), (0, pad_w), (0, 0)], mode='constant')

    return image, scale
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值