0 背景
在要对一批正常图像进行扭曲处理,相当于为了做一些数据增强,代码参考了一篇帖子(附在文章末尾),我在基础上做了一些符合自己场景的调整:处理中文路径、彩色图像、批量处理、随机调整幅度、随机扭曲方式等。
1 依赖
numpy、random、math、tqdm、opencv-pyhton
2 封装功能
包含定义的用来限制最大范围的全局变量和初始化的函数、水平扭曲(幅度随机)、垂直扭曲(幅度随机)、垂直和水平扭曲(幅度随机)、图像倾斜(幅度随机)
import numpy as np
import random
import math
import cv2
global MAX_V
global MAX_H
global MAX_R
def Init(max_v,max_h,max_r):
global MAX_V
global MAX_H
global MAX_R
MAX_V = max_v
MAX_H = max_h
MAX_R = max_r
#####################
# Vertical wave
def VerWave(img):
rows, cols, chs = img.shape
img_output = np.zeros(img.shape, dtype=img.dtype)
randa = random.randint(0,MAX_V)
for k in range(chs):
for i in range(rows):
for j in range(cols):
offset_x = int(randa * math.sin(2 * 3.14 * i / 180))
offset_y = 0
if j+offset_x < cols:
img_output[i,j][k] = img[i,(j+offset_x)%cols][k]
else:
img_output[i,j][k] = 0
return img_output
#####################
# Horizontal wave
def HorWave(img):
rows, cols, chs = img.shape
randa = random.randint(0,MAX_H)
img_output = np.zeros(img.shape, dtype=img.dtype)
for k in range(chs):
for i in range(rows):
for j in range(cols):
offset_x = 0
offset_y = int(randa* math.sin(2 * 3.14 * j / 150))
if i+offset_y < rows:
img_output[i,j][k] = img[(i+offset_y)%rows,j][k]
else:
img_output[i,j][k] = 0
return img_output
#####################
# Both horizontal and vertical
def BothVH(img):
rows, cols, chs = img.shape
randa = random.randint(0,MAX_V)
randb = random.randint(1,MAX_H)
img_output = np.zeros(img.shape, dtype=img.dtype)
for k in range(chs):
for i in range(rows):
for j in range(cols):
offset_x = int(randa * math.sin(2 * 3.14 * i / 150))
offset_y = int(randb * math.cos(2 * 3.14 * j / 150))
if i+offset_y < rows and j+offset_x < cols:
img_output[i,j][k] = img[(i+offset_y)%rows,(j+offset_x)%cols][k]
else:
img_output[i,j][k] = 0
return img_output
#####################
# Concave effect
def Con(img):
rows, cols, chs = img.shape
randx = random.randint(-MAX_R,MAX_R)
img_output = np.zeros(img.shape, dtype=img.dtype)
for k in range(chs):
for i in range(rows):
for j in range(cols):
offset_x = int(randx * math.sin(2 * 3.14 * i / (2*cols)))
offset_y = 0
if j+offset_x < cols:
img_output[i,j][k] = img[i,(j+offset_x)%cols][k]
else:
img_output[i,j][k] = 0
return img_output
3 运行调用
自定义读取和保存路径、扭曲、倾斜幅度限制范围。
对于单张图片,随机倾斜移,随机采用水平扭曲、垂直扭曲、水平和垂直扭曲、对处理后的图片进行保存。
from a import Con,VerWave,HorWave,BothVH,Init
from tqdm import tqdm
import numpy as np
import random
import cv2
import os
"""
Path: 待处理的图片路径
SavePath: 图片保存路径
Max_v: 水平扭曲幅度 随机最大范围 int
Max_h: 垂直扭曲幅度 随机最大范围 int
Max_r: 倾斜幅度 [-Max_r Max_r] int
"""
Path = "./ye/"
SavePath ="./yea/"
Max_v = 10
Max_h = 10
Max_r = 50
Init(Max_v,Max_h,Max_r)
names = os.listdir(Path)
for name in tqdm(names):
image_name = Path+name
#img = cv2.imread(image_name)
img = cv2.imdecode(np.fromfile(image_name,dtype=np.uint8),-1)#可读取中文路径
img = Con(img)
opt = random.randint(0,2)
if opt==0:
img = VerWave(img)
elif opt == 1:
img = HorWave(img)
else:
img = BothVH(img)
#cv2.imwrite(SavePath+name,img)
cv2.imencode('.jpg', img)[1].tofile(SavePath+name)#可保存中文路径