当前位置:   article > 正文

python对图像数据增强,包括翻转、镜像、加噪。_图像线下增强镜像旋转代码

图像线下增强镜像旋转代码
  1. import cv2
  2. import numpy as np
  3. import os.path
  4. import copy
  5. def rotate(image, angle, center=None, scale=1.0):
  6. (h, w) = image.shape[:2]
  7. # If no rotation center is specified, the center of the image is set as the rotation center
  8. if center is None:
  9. center = (w / 2, h / 2)
  10. M = cv2.getRotationMatrix2D(center, angle, scale)
  11. rotated = cv2.warpAffine(image, M, (w, h))
  12. return rotated
  13. def noiseing(img):
  14. #img = cv2.cvtColor(rgbimg, cv2.COLOR_BGR2GRAY)
  15. param = 30
  16. grayscale = 256
  17. w = img.shape[1]
  18. h = img.shape[0]
  19. newimg = np.zeros((h, w, 3), np.uint8)
  20. #row and col
  21. for x in xrange(0, h):
  22. for y in xrange(0, w, 2): #Avoid exceeding boundaries
  23. r1 = np.random.random_sample()
  24. r2 = np.random.random_sample()
  25. z1 = param * np.cos(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
  26. z2 = param * np.sin(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
  27. fxy_0 = int(img[x, y, 0] + z1)
  28. fxy_1 = int(img[x, y, 1] + z1)
  29. fxy_2 = int(img[x, y, 2] + z1)
  30. fxy1_0 = int(img[x, y + 1, 0] + z2)
  31. fxy1_1 = int(img[x, y + 1, 1] + z2)
  32. fxy1_2 = int(img[x, y + 1, 2] + z2)
  33. # f(x,y)
  34. if fxy_0 < 0:
  35. fxy_val_0 = 0
  36. elif fxy_0 > grayscale - 1:
  37. fxy_val_0 = grayscale - 1
  38. else:
  39. fxy_val_0 = fxy_0
  40. if fxy_1 < 0:
  41. fxy_val_1 = 0
  42. elif fxy_1 > grayscale - 1:
  43. fxy_val_1 = grayscale - 1
  44. else:
  45. fxy_val_1 = fxy_1
  46. if fxy_2 < 0:
  47. fxy_val_2 = 0
  48. elif fxy_2 > grayscale - 1:
  49. fxy_val_2 = grayscale - 1
  50. else:
  51. fxy_val_2 = fxy_2
  52. # f(x,y+1)
  53. if fxy1_0 < 0:
  54. fxy1_val_0 = 0
  55. elif fxy1_0 > grayscale - 1:
  56. fxy1_val_0 = grayscale - 1
  57. else:
  58. fxy1_val_0 = fxy1_0
  59. if fxy1_1 < 0:
  60. fxy1_val_1 = 0
  61. elif fxy1_1 > grayscale - 1:
  62. fxy1_val_1 = grayscale - 1
  63. else:
  64. fxy1_val_1 = fxy1_1
  65. if fxy1_2 < 0:
  66. fxy1_val_2 = 0
  67. elif fxy1_2 > grayscale - 1:
  68. fxy1_val_2 = grayscale - 1
  69. else:
  70. fxy1_val_2 = fxy1_2
  71. newimg[x, y, 0] = fxy_val_0
  72. newimg[x, y, 1] = fxy_val_1
  73. newimg[x, y, 2] = fxy_val_2
  74. newimg[x, y + 1, 0] = fxy1_val_0
  75. newimg[x, y + 1, 1] = fxy1_val_1
  76. newimg[x, y + 1, 2] = fxy1_val_2
  77. #newimg = cv2.cvtColor(newimg, cv2.COLOR_GRAY2RGB)
  78. cv2.destroyAllWindows()
  79. return newimg
  80. #i = 0
  81. file_dir = "/home/xn/caffe/examples/facetestquestions/ImageDatainc/"
  82. for class_name in os.listdir(file_dir):
  83. #for index,name in enumerate(classes):
  84. class_path = file_dir+class_name+"/"
  85. for img_name in os.listdir(class_path):
  86. img_path = class_path + img_name
  87. image = cv2.imread(img_path)
  88. #Simple rotation 90 degrees
  89. rotated = rotate(image, 90)
  90. cv2.imwrite(class_path + '/' + img_name[0:7] +'_ro90.jpg', rotated)
  91. #Rotate 180 degrees and add Gaussian noise
  92. rotated = rotate(image, 180)
  93. # if __name__ == '__main__':
  94. #print 'load %s ...' % fn
  95. #img = cv2.imread(rotated)
  96. # coutn = 100000
  97. # for k in xrange(0, coutn):
  98. # get the random point
  99. # xi = int(np.random.uniform(0, rotated.shape[1]))
  100. # xj = int(np.random.uniform(0, rotated.shape[0]))
  101. # # add noise
  102. # if rotated.ndim == 2:
  103. # rotated[xj, xi] = 255
  104. # elif rotated.ndim == 3:
  105. # rotated[xj, xi, 0] = 25
  106. # rotated[xj, xi, 1] = 20
  107. # rotated[xj, xi, 2] = 20
  108. #cv2.namedWindow('img')
  109. #cv2.imshow('img', img)
  110. #cv2.waitKey()
  111. # cv2.destroyAllWindows()
  112. #newimg = skimage.util.random_noise(rotated, mode='salt', seed=None, clip=False)
  113. newimg = noiseing(rotated)
  114. #newimg = cv2.cvtColor(newing, cv2.COLOR_GRAY2BGR)
  115. cv2.imwrite(class_path + '/' + img_name[0:7] + '_rono.jpg', newimg)
  116. #Image processing
  117. size = image.shape
  118. #Get an image that is the same as the original image, note this to use deep copy
  119. iLR = copy.deepcopy(image)
  120. h = size[0]
  121. w = size[1]
  122. for i in range(h): # row and col
  123. for j in range(w):
  124. iLR[i, w - 1 - j] = image[i, j] # Mirror formula
  125. cv2.imwrite(class_path + '/' + img_name[0:7] + '_mirr.jpg', iLR)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号