pytorch进阶学习(三):在数据集数量不够时如何进行数据增强

对图片数据增强,可以对图片实现:

1. 尺寸放大缩小

2. 旋转(任意角度,如45°,90°,180°,270°)

3. 翻转(水平翻转,垂直翻转)

4. 明亮度改变(变亮,变暗)

5. 像素平移(往一个方向平移像素,空出部分自动填补黑色)

6. 添加噪声(椒盐噪声,高斯噪声)

目录

一、放大缩小

二、水平/垂直翻转

三、旋转

四、明亮度

五、平移

六、添加噪声

七、模糊

八、对一张图片进行单种变换

九、对一张图片进行多种变换

十、对数据集中所有类别的图片进行变换


数据集文件夹名为data4,第一个分类daisy中只有五张图片。 

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第1张图片

 

一、放大缩小

import os
import numpy as np
import cv2

# 放大缩小
def Scale(image, scale):
    return cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)

def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Scale(test_jpg,0.5)
    cv2.imshow("Img1", img1)
    img2 = Scale(test_jpg,2)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第2张图片

二、水平/垂直翻转

import os
import numpy as np
import cv2

# flipcode=1为水平翻转,flipcode=0为垂直翻转
def Horizontal(image):
    return cv2.flip(image,1,dst=None)

def Vertical(image):
    return cv2.flip(image,0,dst=None)

def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Horizontal(test_jpg)
    cv2.imshow("Img1", img1)
    img2 = Vertical(test_jpg)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第3张图片

三、旋转

image.shape[0], 图片垂直尺寸

image.shape[1], 图片水平尺寸

image.shape[2], 图片通道数

cv2.getRotationMatrix2D()经常被使用到的参数有三个:

  • 旋转中心
  • 旋转角度
  • 旋转后的缩放比例

利用opencv实现仿射变换一般会涉及到warpAffine和getRotationMatrix2D两个函数,其中warpAffine可以实现一些简单的重映射,而getRotationMatrix2D可以获得旋转矩阵。cv2.warpAffine()主要有以下参数:

  • src: 输入图像
  • dst: 输出图像,尺寸由dsize指定,图像类型与原图像一致
  • M: 2X3的变换矩阵
  • dsize: 指定图像输出尺寸
  • flags: 插值算法标识符,有默认值INTER_LINEAR,如果插值算法WARP_INVERSE_MAP, warpAffine函数使用如下矩阵进行图像转换
import os
import numpy as np
import cv2

#旋转
def Rotate(image,angle,scale):
    w = image.shape[1]
    h = image.shape[0]
    # 第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例
    M = cv2.getRotationMatrix2D((w/2,h/2),angle,scale)
    # 得到矩阵后得用到图像的仿射变换函数才可以进行最终图像的变化
    image = cv2.warpAffine(image,M,(w,h))
    return image


def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Rotate(test_jpg,45,2)
    cv2.imshow("Img1", img1)
    img2 = Rotate(test_jpg,90,1)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

旋转后如果图片大小不够大的话会填充黑色。

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第4张图片

四、明亮度

把图片变亮或者变暗。

import os
import numpy as np
import cv2

'''  
明亮度 
'''
# 变暗
def Darker(image,percetage):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get darker
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
            image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
            image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
    return image_copy

# 明亮
def Brighter(image, percetage):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get brighter
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
    return image_copy



def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Darker(test_jpg,0.9)
    cv2.imshow("Img1", img1)
    img2 = Brighter(test_jpg,1.1)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第5张图片

五、平移

import os
import numpy as np
import cv2


def Move(img,x,y):
    img_info=img.shape
    height=img_info[0]
    width=img_info[1]

    mat_translation=np.float32([[1,0,x],[0,1,y]])  #变换矩阵:设置平移变换所需的计算矩阵:2行3列
    #[[1,0,20],[0,1,50]]   表示平移变换:其中x表示水平方向上的平移距离,y表示竖直方向上的平移距离。
    dst=cv2.warpAffine(img,mat_translation,(width,height))  #变换函数
    return dst

def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Move(test_jpg,10,20)
    cv2.imshow("Img1", img1)
    img2 = Move(test_jpg,-20,-10)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第6张图片

六、添加噪声

import os
import numpy as np
import cv2

'''
增加噪声
'''
# 椒盐噪声
def SaltAndPepper(src,percetage):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0,src.shape[0]-1)
        randG=np.random.randint(0,src.shape[1]-1)
        randB=np.random.randint(0,3)
        if np.random.randint(0,1)==0:
            SP_NoiseImg[randR,randG,randB]=0
        else:
            SP_NoiseImg[randR,randG,randB]=255
    return SP_NoiseImg

# 高斯噪声
def GaussianNoise(image,percetage):
    G_Noiseimg = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
    for i in range(G_NoiseNum):
        temp_x = np.random.randint(0,h)
        temp_y = np.random.randint(0,w)
        G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    return G_Noiseimg

def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = SaltAndPepper(test_jpg,0.05)
    cv2.imshow("Img1", img1)
    img2 = GaussianNoise(test_jpg,0.05)
    cv2.imshow("Img2", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第7张图片

七、模糊

高斯模糊:

cv2.GaussianBlur(图像,卷积核,标准差)
import os
import numpy as np
import cv2


def Blur(img):
    blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    # #      cv2.GaussianBlur(图像,卷积核,标准差)
    return blur

def TestOnePic():
    test_jpg_loc = r"data4/daisy/1.jpg"
    test_jpg = cv2.imread(test_jpg_loc)
    cv2.imshow("ShowImg", test_jpg)
    img1 = Blur(test_jpg)
    cv2.imshow("Img1", img1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    TestOnePic()

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第8张图片

八、对一张图片进行单种变换

'''
    这是图片数据增强的代码,可以对图片实现:
    1. 尺寸放大缩小
    2. 旋转(任意角度,如45°,90°,180°,270°)
    3. 翻转(水平翻转,垂直翻转)
    4. 明亮度改变(变亮,变暗)
    5. 像素平移(往一个方向平移像素,空出部分自动填补黑色)
    6. 添加噪声(椒盐噪声,高斯噪声)
'''
import os
import numpy as np
import cv2

'''
缩放
'''
# 放大缩小
def Scale(image, scale):
    return cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)

'''
翻转
'''
# 水平翻转
def Horizontal(image):
    return cv2.flip(image,1,dst=None) #水平镜像

# 垂直翻转
def Vertical(image):
    return cv2.flip(image,0,dst=None) #垂直镜像

# 旋转,R可控制图片放大缩小
def Rotate(image, angle=15, scale=0.9):
    w = image.shape[1]
    h = image.shape[0]
    #rotate matrix
    M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
    #rotate
    image = cv2.warpAffine(image,M,(w,h))
    return image

'''  
明亮度 
'''
# 变暗
def Darker(image,percetage=0.9):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get darker
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
            image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
            image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
    return image_copy

# 明亮
def Brighter(image, percetage=1.1):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get brighter
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
    return image_copy

# 平移
def Move(img,x,y):
    img_info=img.shape
    height=img_info[0]
    width=img_info[1]

    mat_translation=np.float32([[1,0,x],[0,1,y]])  #变换矩阵:设置平移变换所需的计算矩阵:2行3列
    #[[1,0,20],[0,1,50]]   表示平移变换:其中x表示水平方向上的平移距离,y表示竖直方向上的平移距离。
    dst=cv2.warpAffine(img,mat_translation,(width,height))  #变换函数
    return dst

'''
增加噪声
'''
# 椒盐噪声
def SaltAndPepper(src,percetage=0.05):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0,src.shape[0]-1)
        randG=np.random.randint(0,src.shape[1]-1)
        randB=np.random.randint(0,3)
        if np.random.randint(0,1)==0:
            SP_NoiseImg[randR,randG,randB]=0
        else:
            SP_NoiseImg[randR,randG,randB]=255
    return SP_NoiseImg

# 高斯噪声
def GaussianNoise(image,percetage=0.05):
    G_Noiseimg = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
    for i in range(G_NoiseNum):
        temp_x = np.random.randint(0,h)
        temp_y = np.random.randint(0,w)
        G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    return G_Noiseimg

def Blur(img):
    blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    # #      cv2.GaussianBlur(图像,卷积核,标准差)
    return blur


def TestOneDir():
    root_path = "data4/daisy"
    save_path = root_path
    for a, b, c in os.walk(root_path):
        for file_i in c:
            file_i_path = os.path.join(a, file_i)
            print(file_i_path)
            img_i = cv2.imread(file_i_path)

            img_scale = Scale(img_i,1.5)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_scale.jpg"), img_scale)

            img_horizontal = Horizontal(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_horizontal.jpg"), img_horizontal)

            img_vertical = Vertical(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_vertical.jpg"), img_vertical)

            img_rotate = Rotate(img_i,90)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate90.jpg"), img_rotate)

            img_rotate = Rotate(img_i, 180)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate180.jpg"), img_rotate)

            img_rotate = Rotate(img_i, 270)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate270.jpg"), img_rotate)

            img_move = Move(img_i,15,15)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_move.jpg"), img_move)

            img_darker = Darker(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_darker.jpg"), img_darker)

            img_brighter = Brighter(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_brighter.jpg"), img_brighter)

            img_blur = Blur(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_blur.jpg"), img_blur)

            img_salt = SaltAndPepper(img_i,0.05)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_salt.jpg"), img_salt)


if __name__ == "__main__":
    TestOneDir()

    # root_path = "data4/"
    # AllData(root_path)

可以看到daisy中由原来的5张图变为了60张,每张图都经过了11种变换。 

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第9张图片

 

九、对一张图片进行多种变换

想要对一张图片进行翻转、平移、模糊等叠加操作,就依次对所需代码进行执行。

1. 一开始我们有五张图片。

pytorch进阶学习(三):在数据集数量不够时如何进行数据增强_第10张图片

 2. 先对图片进行scale操作,把其他代码注释掉,现在变为10张图片。


import os
import numpy as np
import cv2

'''
缩放
'''
# 放大缩小
def Scale(image, scale):
    return cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)

'''
翻转
'''
# 水平翻转
def Horizontal(image):
    return cv2.flip(image,1,dst=None) #水平镜像

# 垂直翻转
def Vertical(image):
    return cv2.flip(image,0,dst=None) #垂直镜像

# 旋转,R可控制图片放大缩小
def Rotate(image, angle=15, scale=0.9):
    w = image.shape[1]
    h = image.shape[0]
    #rotate matrix
    M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
    #rotate
    image = cv2.warpAffine(image,M,(w,h))
    return image

'''  
明亮度 
'''
# 变暗
def Darker(image,percetage=0.9):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get darker
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
            image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
            image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
    return image_copy

# 明亮
def Brighter(image, percetage=1.1):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get brighter
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
    return image_copy

# 平移
def Move(img,x,y):
    img_info=img.shape
    height=img_info[0]
    width=img_info[1]

    mat_translation=np.float32([[1,0,x],[0,1,y]])  #变换矩阵:设置平移变换所需的计算矩阵:2行3列
    #[[1,0,20],[0,1,50]]   表示平移变换:其中x表示水平方向上的平移距离,y表示竖直方向上的平移距离。
    dst=cv2.warpAffine(img,mat_translation,(width,height))  #变换函数
    return dst

'''
增加噪声
'''
# 椒盐噪声
def SaltAndPepper(src,percetage=0.05):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0,src.shape[0]-1)
        randG=np.random.randint(0,src.shape[1]-1)
        randB=np.random.randint(0,3)
        if np.random.randint(0,1)==0:
            SP_NoiseImg[randR,randG,randB]=0
        else:
            SP_NoiseImg[randR,randG,randB]=255
    return SP_NoiseImg

# 高斯噪声
def GaussianNoise(image,percetage=0.05):
    G_Noiseimg = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
    for i in range(G_NoiseNum):
        temp_x = np.random.randint(0,h)
        temp_y = np.random.randint(0,w)
        G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    return G_Noiseimg

def Blur(img):
    blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    # #      cv2.GaussianBlur(图像,卷积核,标准差)
    return blur


def TestOneDir():
    root_path = "data4/daisy"
    save_path = root_path
    for a, b, c in os.walk(root_path):
        for file_i in c:
            file_i_path = os.path.join(a, file_i)
            print(file_i_path)
            img_i = cv2.imread(file_i_path)

            img_scale = Scale(img_i,1.5)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_scale.jpg"), img_scale)
#
#             img_horizontal = Horizontal(img_i)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_horizontal.jpg"), img_horizontal)
#
#             img_vertical = Vertical(img_i)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_vertical.jpg"), img_vertical)
#
#             img_rotate = Rotate(img_i,90)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate90.jpg"), img_rotate)
#
#             img_rotate = Rotate(img_i, 180)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate180.jpg"), img_rotate)
#
#             img_rotate = Rotate(img_i, 270)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate270.jpg"), img_rotate)
#
#             img_move = Move(img_i,15,15)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_move.jpg"), img_move)
#
#             img_darker = Darker(img_i)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_darker.jpg"), img_darker)
#
#             img_brighter = Brighter(img_i)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_brighter.jpg"), img_brighter)
#
#             img_blur = Blur(img_i)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_blur.jpg"), img_blur)
#
#             img_salt = SaltAndPepper(img_i,0.05)
#             cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_salt.jpg"), img_salt)
# #


if __name__ == "__main__":
    TestOneDir()

    # root_path = "data4/"
    # AllData(root_path)

十、对数据集中所有类别的图片进行变换

'''
    这是图片数据增强的代码,可以对图片实现:
    1. 尺寸放大缩小
    2. 旋转(任意角度,如45°,90°,180°,270°)
    3. 翻转(水平翻转,垂直翻转)
    4. 明亮度改变(变亮,变暗)
    5. 像素平移(往一个方向平移像素,空出部分自动填补黑色)
    6. 添加噪声(椒盐噪声,高斯噪声)
'''
import os
import numpy as np
import cv2

# envpath = '/home/zhaolei/anaconda3/envs/maweiyi/lib/python3.8/site-packages/cv2/qt/plugins/platforms'
# os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath

'''
缩放
'''
# 放大缩小
def Scale(image, scale):
    return cv2.resize(image,None,fx=scale,fy=scale,interpolation=cv2.INTER_LINEAR)

'''
翻转
'''
# 水平翻转
def Horizontal(image):
    return cv2.flip(image,1,dst=None) #水平镜像

# 垂直翻转
def Vertical(image):
    return cv2.flip(image,0,dst=None) #垂直镜像

# 旋转,R可控制图片放大缩小
def Rotate(image, angle=15, scale=0.9):
    w = image.shape[1]
    h = image.shape[0]
    #rotate matrix
    M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
    #rotate
    image = cv2.warpAffine(image,M,(w,h))
    return image

'''  
明亮度 
'''
# 变暗
def Darker(image,percetage=0.9):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get darker
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
            image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
            image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
    return image_copy

# 明亮
def Brighter(image, percetage=1.1):
    image_copy = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    #get brighter
    for xi in range(0,w):
        for xj in range(0,h):
            image_copy[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
            image_copy[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
    return image_copy

# 平移
def Move(img,x,y):
    img_info=img.shape
    height=img_info[0]
    width=img_info[1]

    mat_translation=np.float32([[1,0,x],[0,1,y]])  #变换矩阵:设置平移变换所需的计算矩阵:2行3列
    #[[1,0,20],[0,1,50]]   表示平移变换:其中x表示水平方向上的平移距离,y表示竖直方向上的平移距离。
    dst=cv2.warpAffine(img,mat_translation,(width,height))  #变换函数
    return dst

'''
增加噪声
'''
# 椒盐噪声
def SaltAndPepper(src,percetage=0.05):
    SP_NoiseImg=src.copy()
    SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
    for i in range(SP_NoiseNum):
        randR=np.random.randint(0,src.shape[0]-1)
        randG=np.random.randint(0,src.shape[1]-1)
        randB=np.random.randint(0,3)
        if np.random.randint(0,1)==0:
            SP_NoiseImg[randR,randG,randB]=0
        else:
            SP_NoiseImg[randR,randG,randB]=255
    return SP_NoiseImg

# 高斯噪声
def GaussianNoise(image,percetage=0.05):
    G_Noiseimg = image.copy()
    w = image.shape[1]
    h = image.shape[0]
    G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
    for i in range(G_NoiseNum):
        temp_x = np.random.randint(0,h)
        temp_y = np.random.randint(0,w)
        G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    return G_Noiseimg

def Blur(img):
    blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    # #      cv2.GaussianBlur(图像,卷积核,标准差)
    return blur


def AllData(rootpath):
    root_path = "data4/"
    save_loc = root_path
    for a,b,c in os.walk(root_path):
        for file_i in c:
            file_i_path = os.path.join(a,file_i)
            print(file_i_path)
            split = os.path.split(file_i_path)
            dir_loc = os.path.split(split[0])[1]
            save_path = os.path.join(save_loc,dir_loc)

            img_i = cv2.imread(file_i_path)
            img_scale = Scale(img_i,1.5)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_scale.jpg"), img_scale)

            img_horizontal = Horizontal(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_horizontal.jpg"), img_horizontal)

            img_vertical = Vertical(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_vertical.jpg"), img_vertical)

            img_rotate = Rotate(img_i, 90)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate90.jpg"), img_rotate)

            img_rotate = Rotate(img_i, 180)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate180.jpg"), img_rotate)

            img_rotate = Rotate(img_i, 270)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_rotate270.jpg"), img_rotate)

            img_move = Move(img_i, 15, 15)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_move.jpg"), img_move)

            img_darker = Darker(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_darker.jpg"), img_darker)

            img_brighter = Brighter(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_brighter.jpg"), img_brighter)

            img_blur = Blur(img_i)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_blur.jpg"), img_blur)

            img_salt = SaltAndPepper(img_i, 0.05)
            cv2.imwrite(os.path.join(save_path, file_i[:-4] + "_salt.jpg"), img_salt)


if __name__ == "__main__":
    root_path = "data4/"
    AllData(root_path)

你可能感兴趣的:(深度学习,人工智能,神经网络,数据分析,python)