### 图像的类别不进行改变,平移,旋转,缩放等增强。
import numpy as np
import math
import re
import matplotlib.pyplot as plt
class Image(object):
def __init__(self, image):
"""
进行各种图像增强的方式
:param image: 输入图像。
"""
self._set_params(image)
def _set_params(self,image):
self.img = image
self.row = image.shape[0] # 高
self.col = image.shape[1] # 宽
self.transform = None # 变换矩阵。
def Translation(self, delta_x, delta_y):
""" 图像的平移, 定义图像平移矩阵"""
# 图像平移
# | 1, 0, delta |
# |0, 1, delta | * |x, y, 1|.t = (x+delta, y+delta,1)
# |0, 0, 1 |
self.transform = np.array([[1, 0, delta_x],
[0, 1, delta_y],
[0, 0, 1]])
def Resize(self, alpha):
""" 缩放操作:
alpha : 缩放银子,不进行缩放则alpha为1"""
# 缩放因子大则缩小的幅度较大。
self.transform = np.array([[alpha,0,0],[0,alpha,0],[0,0,1]])
def HorMirror(self):
""" 水平镜像: 也就是翻转"""
self.transform = np.array([[1,0,0],
[0,-1,self.col-1],
[0,0,1]])
def VerMierror(self):
""" 垂直镜像:上下翻转"""
self.transform = np.array([[-1,0, self.row-1],
[0,1,0],
[0,0,1]])
def Rotate(self, angle):
""" 旋转"""
self.transform = np.array([[math.cos(angle), -math.sin(angle), 0],
[math.sin(angle), math.cos(angle), 0],
[0,0,1]])
def operate(self):
temp = np.zeros(self.img.shape, dtype=self.img.dtype)
for i in range(self.row):
for j in range(self.col):
temp_pos = np.array([i, j, 1]) # 坐标
# 用变换矩阵进行坐标转换,找到对应的原图的坐标。
# [x, y, z] = self.transform @ temp_pos
[x,y,z] = np.matmul(self.transform, temp_pos)
x = int(x)
y = int(y)
if x>= self.row or y>=self.col or x<0 or y<0: # 如果超出原图范围。
temp[i,j,:] = 0
else:
temp[i,j,:] = self.img[x,y,:]
return temp
def __call__(self, act):
"""
:param act: 操作
:return:
"""
r = r"([a-zA-Z]*)=([^,)]*)"
act_str = act.lower()
kwargs = dict([(i,eval(j)) for (i,j) in re.findall(r, act_str)])
if "translation" in act_str:
self.Translation(**kwargs)
elif "resize" in act_str:
self.Resize(**kwargs)
elif "hormirror" in act_str:
self.HorMirror(**kwargs)
elif "vermirror" in act_str:
self.VerMierror(**kwargs)
elif "rotat" in act_str:
self.Rotate(**kwargs) # 设置transform完毕
return self.operate()# 进行变换得到原图。
""" 导入数据, 手写体数字"""
def load_data(path = "..\data/mnist/mnist.npz"):
f = np.load(path)
X_train, y_train = f["x_train"], f["y_train"]
X_test, y_test = f["x_test"], f["y_test"]
f.close()
return (X_train,y_train),(X_test,y_test)
(X_train,y_train),(X_test,y_test) = load_data()
img = X_train[0].reshape(28, 28, 1)
Img = Image(img) # 定义变换类。
ax = plt.subplot(121)
plt.tight_layout()
plt.imshow(img.reshape(28,28))
plt.title("Original")
plt.axis("off")
ax = plt.subplot(122)
plt.imshow(Img("vermirror").reshape(28,28)) # 垂直变换。
plt.title("vermirror")
plt.axis("off")
plt.show()
【深度学习】 图像增强操作
最新推荐文章于 2024-05-14 06:33:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)