一、原图和对应的mask统一名字,脚本如下
我的原图带有source字样,mask图带有mask字样,因此我将其都替换为NB
#coding:utf-8
import cv2 as cv
import os
import numpy as np
oriImg = os.listdir('./datasets/aug/mask/')
for img in oriImg:
oriimg = cv.imread('./datasets/aug/mask/'+img)
#newimg = cv.resize(oriimg,(720,960),interpolation = cv.INTER_AREA)
#newimg = cv.cvtColor(newimg, cv.COLOR_BGR2GRAY) ### mask 要加这一句 img 要注释
cv.imwrite('./datasets/aug/mask24/'+img.split('.')[0].replace('mask','NB')+'.png',oriimg) ## img 用这句
#cv.imwrite('./sample/imgresize/'+img,newimg) mask用
print (img)
执行完脚本以后原图和label文件夹中图片的名称就一模一样。
二、数据增强
我采用albumentations库进行数据增强,有些增强方式需要对label也做相应的增强,有些方式不需要,所以分开来做,我这里计划对每张图做10次增强。小数据集适合离线数据增强,大数据集适合在线数据增强。数据增强代码如下
#coding:utf-8
import albumentations as A
import cv2 as cv
import os
## 原图增强 label也增强
aug1 = A.Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Resize(height=512, width=512, interpolation=3, always_apply=False, p=1),
A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=15, p=0.2),
A.Transpose(always_apply=False,p=1),
A.GridDistortion(num_steps=10,distort_limit=0.3,border_mode=4,always_apply=False,p=1),##网格失真
])
### 原图增强 label 不变
aug2 = A.Compose([
A.OneOf([
A.GaussNoise(), # 将高斯噪声应用于输入图像。
], p=1), # 应用选定变换的概率
A.OneOf([
A.MotionBlur(p=0.5), # 使用随机大小的内核将运动模糊应用于输入图像。
A.MedianBlur(blur_limit=3, p=0.5), # 中值滤波
A.Blur(blur_limit=3, p=0.5), # 使用随机大小的内核模糊输入图像。
], p=1),
A.RandomBrightnessContrast(p=0.5), # 随机明亮对比度
A.RandomFog(fog_coef_lower=0.3,fog_coef_upper=0.6,alpha_coef=0.08,always_apply=False,p=0.3), ##随机雾化
A.Cutout(num_holes=20,max_h_size=20,max_w_size=20,fill_value=0,always_apply=False,p=0.5),###扣掉小块
A.CLAHE(clip_limit=4.0,tile_grid_size=(5,5),always_apply=False,p=0.5),###直方图均衡化
])
image_path = './datasets/aug/tmpimg/'
mask_path = './datasets/aug/tmplabel/'
imglist = os.listdir(image_path)
labellist = os.listdir(mask_path)
for image in imglist:
image_ori = cv.imread(image_path + image)
label_ori = cv.imread(mask_path + image )
for i in range(5):
augmented = aug1(image=image_ori, mask=label_ori)
image_aug = augmented['image']
label_aug = augmented['mask']
cv.imwrite('./datasets/aug/imgAug/'+'Aug_'+str(i)+'_'+ image,image_aug)
cv.imwrite('./datasets/aug/labelAug/'+'Aug_'+str(i)+'_'+ image,label_aug)
print (image,'----------------------',str(i))
for i in range(5,10):
augmented = aug2(image=image_ori, mask=label_ori)
image_aug = augmented['image']
label_aug = augmented['mask']
cv.imwrite('./datasets/aug/imgAug/'+'Aug_'+str(i)+'_'+ image,image_aug)
cv.imwrite('./datasets/aug/labelAug/'+'Aug_'+str(i)+'_'+ image,label_ori)
print (image,'----------------------',str(i))
三、增强后label转为8位单通道标签
原始图像和原始lalbel大小不一样,将原始图片与增强后的图像合并,并统一到同一个尺寸大小,将24位label转换为8位单通道,图像中的像素值从0开始递增,0代表背景,1代表第一类前景,依次类推
#coding:utf-8
import cv2 as cv
import os
import numpy as np
### valuelist的输出 与当时赋彩色值一致,当时是RGB 现在是BGR
## {(0, 255, 255), ywm ,2
# (0, 153, 255), min,3
# (0, 0, 0), 0, background
# (255, 255, 255), 1,ayg
# (128, 128, 128)},4,max
oriImg = os.listdir('./traindata/aug/labelAug/')
for img in oriImg:
oriimg = cv.imread('./traindata/aug/labelAug/'+img)
newimg = cv.resize(oriimg,(512,512),interpolation = cv.INTER_AREA)
img_row = newimg.shape[0]
img_col = newimg.shape[1]
dst = np.zeros((img_row,img_col),np.uint8) #np.uint8表示数据类型
# 利用遍历操作对二维灰度图作取反操作
for i in range(0,img_row):
for j in range(0,img_col):
#print (newimg[i][j])
(b,g,r) = newimg[i][j]
if (b,g,r) == (0, 0, 0):
dst[i,j] = np.uint8(0)
if (b,g,r) == (255, 255, 255):
dst[i,j] = np.uint8(1)
if (b,g,r) == (0, 255, 255):
dst[i,j] = np.uint8(2)
if (b,g,r) == (0, 153, 255):
dst[i,j] = np.uint8(3)
if (b,g,r) == (128, 128, 128):
dst[i,j] = np.uint8(4)
#oriimg[i, j] = 255-temp1
#newimg = cv.resize(oriimg,(512,512),interpolation = cv.INTER_AREA)
cv.imwrite('./traindata/aug/labelAug512/'+img,dst)
print (img)