使用之前注意如下三点:
1.确定原始图像存储路径以及掩码文件存储路径
2.路径下的图像格式要保持一致 比如都是PNG (不然生成不了,检测不到图片)
3.image和label的mode格式最好都是RGB (不然会报错如:ValueError: image has wrong mode)
#######################数据增强工具##################
import Augmentor
p = Augmentor.Pipeline("/data1/gyx/QR/multiyolov5_detect_seg/data/customdata/convert_tools/qr/segtest/PNGImages")
p.ground_truth("/data1/gyx/QR/multiyolov5_detect_seg/data/customdata/convert_tools/qr/segtest/SegmentationClassPNG")
#图像旋转
p.rotate(probability=0.8, max_left_rotation=20, max_right_rotation=20)
#图像左右互换
p.flip_left_right(probability=0.5)
p.flip_top_bottom(probability=0.5)
p.flip_random(probability=0.5)
# 旋转90
p.rotate90(probability=0.5)
# # 图像放大缩小,按照概率为0.5,面积为原来的0.9倍
p.zoom_random(probability=.5, percentage_area=0.9)
# # 裁剪
p.crop_random(probability=.5, percentage_area=0.8)
# 透视变换
p.skew(probability=1, magnitude=0.8)
# 剪切
p.shear(probability=1, max_shear_left=15, max_shear_right=15)
#### 遮挡(暂时不需要)
##### p.random_erasing(probability=1, rectangle_area=0.2)
# #最终扩充的数据样本数
p.sample(1000)
p.process() #对每个图像做一次操作
还有一点要注意:
这里需要单独对原图进行如下处理,不然标签的颜色也会跟着颜色对比度和亮度一起改变
# 亮度
# p.random_brightness(probability=.5, min_factor=0.3, max_factor=1.2)
# 颜色
# p.random_color(probability=1, min_factor=0.0, max_factor=1)
# 对比度
# p.random_contrast(probability=1, min_factor=0.7, max_factor=1.2)
附加代码一:
图像格式的转换 bmp/jpg -> png
import cv2
import os
img_path='/data1/gyx/QR/multiyolov5_detect_seg/data/customdata/convert_tools/qr/segtest/JPEGImages/'
output_path='/data1/gyx/QR/multiyolov5_detect_seg/data/customdata/convert_tools/qr/segtest/PNGImages/'
for im_name in os.listdir(img_path):
print(img_path+im_name)
img=cv2.imread(img_path+im_name)
im_name_new=im_name.split('.')[0]+'.png'
cv2.imwrite(output_path+im_name_new,img)
附加代码二:
图像mode的转换 L -> RGB
from PIL import Image
import numpy as np
import os
import cv2
img_path='/data1/gyx/QR/multiyolov5_detect_seg/data/customdata/convert_tools/qr/segtest/PNGImages/test/label/'
for im_name in os.listdir(img_path):
# print(im_name)
img_mode=Image.open(img_path+im_name)
print(img_mode.mode)
# if img_mode.mode=='L':
# print(img_path+im_name)
# img=img_mode.convert('RGB')
# img = np.array(img)
# cv2.imwrite(img_path+im_name,img)
img=img_mode.convert('RGB')
img.save(img_path+im_name)