【深度学习】 图像增强操作

该博客介绍了一个Python类`Image`,用于实现图像的各种增强操作,包括平移、缩放、水平和垂直镜像以及旋转。通过实例展示了如何使用这个类对图像进行变换,特别是对垂直镜像的展示。代码中加载了MNIST手写体数字数据集并应用变换进行展示。
摘要由CSDN通过智能技术生成
### 图像的类别不进行改变,平移,旋转,缩放等增强。
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()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值