• 批量生成图片的数据增强脚本


    整理了一个脚本,实现图片的数据增强,从几个角度来处理平移、旋转、椒盐、高斯、亮度、昏暗,并保存图片到指定目录。连续执行两次可以三五张扩充至上千张。

    1. # -*- coding: utf-8 -*-
    2. import cv2
    3. import numpy as np
    4. import os.path
    5. import copy
    6. import random
    7. # 椒盐噪声
    8. def SaltAndPepper(src, percetage):
    9. SP_NoiseImg = src.copy()
    10. SP_NoiseNum = int(percetage * src.shape[0] * src.shape[1])
    11. for i in range(SP_NoiseNum):
    12. randR = np.random.randint(0, src.shape[0] - 1)
    13. randG = np.random.randint(0, src.shape[1] - 1)
    14. randB = np.random.randint(0, 3)
    15. if np.random.randint(0, 1) == 0:
    16. SP_NoiseImg[randR, randG, randB] = 0
    17. else:
    18. SP_NoiseImg[randR, randG, randB] = 255
    19. return SP_NoiseImg
    20. # 高斯噪声
    21. def addGaussianNoise(image, percetage):
    22. G_Noiseimg = image.copy()
    23. w = image.shape[1]
    24. h = image.shape[0]
    25. G_NoiseNum = int(percetage * image.shape[0] * image.shape[1])
    26. for i in range(G_NoiseNum):
    27. temp_x = np.random.randint(0, h)
    28. temp_y = np.random.randint(0, w)
    29. G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
    30. return G_Noiseimg
    31. # 昏暗
    32. def darker(image, percetage=0.9):
    33. image_copy = image.copy()
    34. w = image.shape[1]
    35. h = image.shape[0]
    36. # get darker
    37. for xi in range(0, w):
    38. for xj in range(0, h):
    39. image_copy[xj, xi, 0] = int(image[xj, xi, 0] * percetage)
    40. image_copy[xj, xi, 1] = int(image[xj, xi, 1] * percetage)
    41. image_copy[xj, xi, 2] = int(image[xj, xi, 2] * percetage)
    42. return image_copy
    43. # 亮度
    44. def brighter(image, percetage=1.5):
    45. image_copy = image.copy()
    46. w = image.shape[1]
    47. h = image.shape[0]
    48. # get brighter
    49. for xi in range(0, w):
    50. for xj in range(0, h):
    51. image_copy[xj, xi, 0] = np.clip(int(image[xj, xi, 0] * percetage), a_max=255, a_min=0)
    52. image_copy[xj, xi, 1] = np.clip(int(image[xj, xi, 1] * percetage), a_max=255, a_min=0)
    53. image_copy[xj, xi, 2] = np.clip(int(image[xj, xi, 2] * percetage), a_max=255, a_min=0)
    54. return image_copy
    55. # 旋转
    56. def rotate(image, angle, center=None, scale=1.0):
    57. (h, w) = image.shape[:2]
    58. # If no rotation center is specified, the center of the image is set as the rotation center
    59. if center is None:
    60. center = (w / 2, h / 2)
    61. m = cv2.getRotationMatrix2D(center, angle, scale)
    62. rotated = cv2.warpAffine(image, m, (w, h))
    63. return rotated
    64. # 平移
    65. def translation(img, M):
    66. rows, cols = img.shape[:2]
    67. # 用仿射变换实现平移,第三个参数为输出的图像大小,值得注意的是该参数形式为(width, height)。
    68. dst = cv2.warpAffine(img, M, (cols, rows))
    69. return dst
    70. # 翻转
    71. def flip(image):
    72. flipped_image = np.fliplr(image)
    73. return flipped_image
    74. # 图片文件夹路径
    75. file_dir = r'E:/Users/raychiu/Desktop/temp/'
    76. for img_name in os.listdir(file_dir):
    77. img_path = file_dir + img_name
    78. img = cv2.imread(img_path)
    79. # cv2.imshow("1",img)
    80. # cv2.waitKey(5000)
    81. # 旋转10个任意角度
    82. for i in range(10):
    83. item = random.randint(1, 360) # 随机生成最小值为1,最大值为360的整数(可以等于上下限)
    84. rotated_item = rotate(img, item)
    85. cv2.imwrite(file_dir + img_name[0:-4] + '_r' + str(item) + '.jpg', rotated_item)
    86. # 平移10个任意位置
    87. for i in range(10):
    88. item1 = random.randint(1, 100)
    89. item2 = random.randint(1, 100)
    90. # 定义平移矩阵,需要是numpy的float32类型
    91. # x轴平移item1,y轴平移item2
    92. M = np.float32([[1, 0, item1], [0, 1, item2]])
    93. translation_item = translation(img, M)
    94. cv2.imwrite(file_dir + img_name[0:-4] + '_t' + str(item1)+ '_'+ str(item2) + '.jpg', translation_item)
    95. for img_name in os.listdir(file_dir):
    96. img_path = file_dir + img_name
    97. img = cv2.imread(img_path)
    98. # 镜像
    99. flipped_img = flip(img)
    100. cv2.imwrite(file_dir + img_name[0:-4] + '_fli.jpg', flipped_img)
    101. # 增加噪声
    102. # img_salt = SaltAndPepper(img, 0.3)
    103. # cv2.imwrite(file_dir + img_name[0:7] + '_salt.jpg', img_salt)
    104. img_gauss = addGaussianNoise(img, 0.3)
    105. cv2.imwrite(file_dir + img_name[0:-4] + '_noise.jpg', img_gauss)
    106. # 变亮、变暗
    107. img_darker = darker(img)
    108. cv2.imwrite(file_dir + img_name[0:-4] + '_darker.jpg', img_darker)
    109. img_brighter = brighter(img)
    110. cv2.imwrite(file_dir + img_name[0:-4] + '_brighter.jpg', img_brighter)
    111. blur = cv2.GaussianBlur(img, (7, 7), 1.5)
    112. # cv2.GaussianBlur(图像,卷积核,标准差)
    113. cv2.imwrite(file_dir + img_name[0:-4] + '_blur.jpg', blur)

  • 相关阅读:
    使用adb命令打开应用
    【paper】Cam2BEV论文浅析
    jQuery学习:内置动画 淡出/淡入 展开/收缩 显示/隐藏
    Oculus 手势追踪
    快速学会git版本管理——创建分支和合并分支
    推荐系统相关论文阅读整理
    深圳工会-杨搏老师手机摄影课程小结
    sourceTree的下载和安装
    蓝桥杯每日一题2023.10.3
    Java项目:台球室计费管理系统(java+SSM+JSP+HTML+JavaScript+mysql)
  • 原文地址:https://blog.csdn.net/RayChiu757374816/article/details/125443396