import cv2
import math
import numpy as np
import os
class data_stength:
def __init__(self, imgae, rows, cols, center=[0, 0]):
self.src = imgae # 图像
self.rows = rows # 行尺寸
self.cols = cols # 列尺寸
self.center = center # 旋转中心
# 平移
def move(self, x, y):
self.transform = np.array([[1, 0, x], [0, 1, y], [0, 0, 1]])
# 缩放
def zoom(self, factor):
self.transform = np.array([[factor, 0, 0], [0, factor, 0], [0, 0, 1]])
# 竖直镜像
def Vertically(self):
self.transform = np.array([[1, 0, 0], [0, -1, self.cols - 1], [0, 0, 1]])
# 水平镜像
def Horizontal(self):
self.transform = np.array([[-1, 0, self.rows - 1], [0, 1, 0], [0, 0, 1]])
# 旋转
'''def Rotate(self, beta):
# beta>0表示逆时针旋转;beta<0表示顺时针旋转
self.transform = np.array([[math.cos(beta), -math.sin(beta), 0],
[math.sin(beta), math.cos(beta), 0],
[0, 0, 1]])'''
# 三色图像运算
def B_process(self):
self.dst_B = np.zeros((self.rows, self.cols), dtype=np.uint8) # red_channel
for i in range(self.rows):
# print("第{}列".format(i))
for j in range(self.cols):
# print("第{}行".format(j))
src_pos = np.array([i - self.center[0], j - self.center[1], 1])
[x, y, z] = np.dot(self.transform, src_pos) # 矩阵乘法
x = math.floor(x)
y = math.floor(y)
# x = int(x) + self.center[0] # 变换后(i,j)对应原图(x,y)
# y = int(y) + self.center[1] # 变换后(i,j)对应原图(x,y)
# 边界判定
if x >= self.rows or y >= self.cols or x < 0 or y < 0:
self.dst_B[i][j] = 0 # 边界
else:
self.dst_B[i][j] = self.src[:, :, 0][x][y] # 内容
return self.dst_B
def G_process(self):
self.dst_G = np.zeros((self.rows, self.cols), dtype=np.uint8) # red_channel
for i in range(self.rows):
# print("第{}列".format(i))
for j in range(self.cols):
# print("第{}行".format(j))
src_pos = np.array([i - self.center[0], j - self.center[1], 1])
[x, y, z] = np.dot(self.transform, src_pos) # 矩阵乘法
x = math.floor(x)
y = math.floor(y)
# x = int(x) + self.center[0] # 变换后(i,j)对应原图(x,y)
# y = int(y) + self.center[1] # 变换后(i,j)对应原图(x,y)
# 边界判定
if x >= self.rows or y >= self.cols or x < 0 or y < 0:
self.dst_G[i][j] = 0 # 边界
else:
self.dst_G[i][j] = self.src[:, :, 1][x][y] # 内容
return self.dst_G
def R_process(self):
self.dst_R = np.zeros((self.rows, self.cols), dtype=np.uint8) # red_channel
for i in range(self.rows):
# print("第{}列".format(i))
for j in range(self.cols):
# print("第{}行".format(j))
src_pos = np.array([i - self.center[0], j - self.center[1], 1])
[x, y, z] = np.dot(self.transform, src_pos) # 矩阵乘法
x = math.floor(x)
y = math.floor(y)
# x = int(x) + self.center[0] # 变换后(i,j)对应原图(x,y)
# y = int(y) + self.center[1] # 变换后(i,j)对应原图(x,y)
# 边界判定
if x >= self.rows or y >= self.cols or x < 0 or y < 0:
self.dst_R[i][j] = 0 # 边界
else:
self.dst_R[i][j] = self.src[:, :, 2][x][y] # 内容
return self.dst_R
if __name__ == '__main__':
'''
# 进制问题
a = 1.3
b = math.ceil(a) # 向上取整
c = math.floor(a) # 向下取整
d = round(a) # 四舍五入
print("a:{},向上取整:{},向下取整:{},四舍五入:{}".format(a, b, c, d))
'''
'''src = np.random.randint(1, 10, (5, 5))
print(src)
rows = src.shape[0]
cols = src.shape[1]
# print(rows, cols)
img = data_stength(src, rows, cols)
img.move(1, 2)
a = img.process()
print(a)'''
path = r'' # 目标文件夹路径
folders = os.listdir(path) # 读取全部图像目录树
for folder in folders:
folder_path = os.path.join(path, folder) # 图片地址
src = cv2.imread(folder_path) # 读取图片
print("src_type{},src_size{}".format(type(src), src.shape))
rows = src.shape[0]
cols = src.shape[1]
img = data_stength(src, rows, cols)
img.move(100, 200) # 平移,左上
# img.Rotate(30) # 旋转
# img.zoom(2)# 放缩
# img.Horizontal() # y镜像
# img.Vertically() # x镜像
B_output = img.B_process()
G_output = img.G_process()
R_output = img.R_process()
tran = src
tran[:, :, 0] = B_output
tran[:, :, 1] = G_output
tran[:, :, 2] = R_output
cv2.imwrite(r''.format(folder), tran)
(接上)python实现批量深度学习数据增强
最新推荐文章于 2022-10-03 12:20:45 发布