(接上)python实现批量深度学习数据增强

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZRX_GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值