def augment_image(image_num_pair, args): filepath, n = image_num_pair img = cv2.imread(filepath) filename = filepath.split(os.sep)[-1] dot_pos = filename.rfind('.') imgname = filename[:dot_pos] ext = filename[dot_pos:] print('Augmenting {} ...'.format(filename)) for i in range(n): img_varied = img.copy() varied_imgname = '{}_{:0>3d}_'.format(imgname, i) if random.random() < args.p_mirror: img_varied = cv2.flip(img_varied, 1) varied_imgname += 'm' if random.random() < args.p_crop: img_varied = ia.random_crop(img_varied, args.crop_size, args.crop_hw_vari) varied_imgname += 'c' if random.random() < args.p_rotate: img_varied = ia.random_rotate(img_varied, args.rotate_angle_vari, args.p_rotate_crop) varied_imgname += 'r' if random.random() < args.p_hsv: img_varied = ia.random_hsv_transform(img_varied, args.hue_vari, args.sat_vari, args.val_vari) varied_imgname += 'h' if random.random() < args.p_gamma: img_varied = ia.random_gamma_transform(img_varied, args.gamma_vari) varied_imgname += 'g' output_filepath = os.sep.join( [args.output_dir, '{}{}'.format(varied_imgname, ext)]) cv2.imwrite(output_filepath, img_varied)
def augment_images(filelist, args): # 遍历所有列表内的文件 for filepath, n in filelist: img = cv2.imread(filepath) filename = filepath.split(os.sep)[-1] dot_pos = filename.rfind('.') # 获取文件名和后缀名 imgname = filename[:dot_pos] ext = filename[dot_pos:] print('Augmenting {} ...'.format(filename)) for i in range(n): img_varied = img.copy() # 扰动后文件名的前缀 varied_imgname = '{}_{:0>3d}_'.format(imgname, i) # 按照比例随机对图像进行镜像 if random.random() < args.p_mirror: # 利用numpy.fliplr(img_varied)也能实现 img_varied = cv2.flip(img_varied, 1) varied_imgname += 'm' # 按照比例随机对图像进行裁剪 if random.random() < args.p_crop: img_varied = ia.random_crop(img_varied, args.crop_size, args.crop_hw_vari) varied_imgname += 'c' # 按照比例随机对图像进行旋转 if random.random() < args.p_rotate: img_varied = ia.random_rotate(img_varied, args.rotate_angle_vari, args.p_rotate_crop) varied_imgname += 'r' # 按照比例随机对图像进行HSV扰动 if random.random() < args.p_hsv: img_varied = ia.random_hsv_transform(img_varied, args.hue_vari, args.sat_vari, args.val_vari) varied_imgname += 'h' # 按照比例随机对图像进行Gamma扰动 if random.random() < args.p_gamma: img_varied = ia.random_gamma_transform(img_varied, args.gamma_vari) varied_imgname += 'g' # 生成扰动后的文件名并保存在指定的路径 output_filepath = os.sep.join( [args.output_dir, '{}{}'.format(varied_imgname, ext)]) cv2.imwrite(output_filepath, img_varied)
def random_augmentation(image): if random.random() < 0.0: return image # 按照比例随机对图像进行镜像 img_varied = random_flip(image) # 随机亮度 if random.random() < 0.5: t = random.randint(-100, 100) blank = np.zeros(image.shape, image.dtype) img_varied = cv2.addWeighted(img_varied, 1, blank, 1, t) # 随机对比度 if random.random() < 0.5: t = random.randint(-100, 100) blank = np.zeros(image.shape, image.dtype) img_varied = cv2.addWeighted(img_varied, 1 + t / 100.0, blank, 1 - t / 100.0, 0) # 按照比例随机对图像进行HSV扰动 if random.random() < 1: img_varied = ia.random_hsv_transform(img_varied, 10, 0.1, 0.1) # 按照比例随机对图像进行Gamma扰动 if random.random() < 0.5: img_varied = ia.random_gamma_transform(img_varied, 2.0) return img_varied # files = glob.glob("./data/image/mouth/pos/*.png") # random.shuffle(files) # c_image = cv2.imread(files[0]) # cv2.imshow("o",c_image) # imgae = random_augmentation(c_image) # cv2.imshow("a",imgae) # cv2.waitKey(0) # # 按照比例随机对图像进行裁剪 # if random.random() < 1: # img_varied = ia.random_crop( # img_varied, # 0.1, # 0.8) # # 按照比例随机对图像进行旋转 # if random.random() < 1.0: # img_varied = ia.random_rotate( # img_varied, # 1.0, # 10.0)
def augment_image(image_num_pair, args): filepath, n = image_num_pair img = cv2.imread(filepath) filename = filepath.split(os.sep)[-1] dot_pos = filename.rfind('.') imgname = filename[:dot_pos] ext = filename[dot_pos:] print('Augmenting {} ...'.format(filename)) for i in range(n): img_varied = img.copy() varied_imgname = '{}_{:0>3d}_'.format(imgname, i) if random.random() < args.p_mirror: img_varied = cv2.flip(img_varied, 1) varied_imgname += 'm' if random.random() < args.p_crop: img_varied = ia.random_crop( img_varied, args.crop_size, args.crop_hw_vari) varied_imgname += 'c' if random.random() < args.p_rotate: img_varied = ia.random_rotate( img_varied, args.rotate_angle_vari, args.p_rotate_crop) varied_imgname += 'r' if random.random() < args.p_hsv: img_varied = ia.random_hsv_transform( img_varied, args.hue_vari, args.sat_vari, args.val_vari) varied_imgname += 'h' if random.random() < args.p_gamma: img_varied = ia.random_gamma_transform( img_varied, args.gamma_vari) varied_imgname += 'g' output_filepath = os.sep.join([ args.output_dir, '{}{}'.format(varied_imgname, ext)]) cv2.imwrite(output_filepath, img_varied)
def augment_images(filelist, args): for filepath, n in filelist: filepath = filepath.replace('\\', '/') img = cv_imread(filepath) #print(filepath) #print(img) filename = filepath.split('/')[-1] dot_pos = filename.rfind('.') imgname = filename[:dot_pos] ext = filename[dot_pos:] #print(ext, imgname,filename) print('Augmenting {} ...'.format(filename)) for i in range(n): img_varied = img.copy() varied_imgname = '{}_{:0>3d}_'.format(imgname, i) if random.random() < args.p_mirror: img_varied = cv.flip(img_varied, 1) varied_imgname += 'm' if random.random() < args.p_crop: img_varied = ia.random_crop(img_varied, args.crop_size, args.crop_hw_vari) varied_imgname += 'c' if random.random() < args.p_rotate: img_varied = ia.random_rotate(img_varied, args.rotate_angle_vari, args.p_rotate_crop) varied_imgname += 'r' if random.random() < args.p_hsv: img_varied = ia.random_hsv_transform(img_varied, args.hue_vari, args.sat_vari, args.val_vari) varied_imgname += 'h' if random.random() < args.p_gamma: img_varied = ia.random_gamma_transform(img_varied, args.gamma_vari) varied_imgname += 'g' output_filepath = os.sep.join( [args.output_dir, '{}{}'.format(varied_imgname, ext)]).replace('\\', '/') #cv.imwrite(output_filepath, img_varied) cv.imencode('.png', img_varied)[1].tofile(output_filepath)