图像数据增强:平移、加噪、旋转、缩放(padding)1

图像数据增强:平移、加噪、旋转、缩放(padding)

  1. 首先是平移
  2. path = '/home/chenjia/HWDB1.1tst_gnt/img/'
  3. #左右、上下平移
  4. #a为平移的尺度,这里设置为10.
  5. def moving(img, a, size, path):
  6. img1 = img
  7. img2 = img
  8. img3 = img
  9. img4 = img
  10. img1 = np.concatenate((img1[:, a:], img1[:, :a]), axis=1) #左
  11. cv2.imwrite(path + 'mov_zuo.png', img1)
  12. img2 = np.concatenate((img2[:, size[1] - a:], img2[:, :size[1] - a]), axis=1) # 右
  13. cv2.imwrite(path + 'mov_you.png', img2)
  14. img3 = np.concatenate((img3[a:, :], img3[:a, :]), axis=0) #上
  15. cv2.imwrite(path + 'mov_shang.png', img3)
  16. img4 = np.concatenate((img4[size[0] - a:, :], img4[:size[0] -a, :]), axis=0) #下
  17. cv2.imwrite(path + 'mov_xia.png', img4)
  18. moving(img_1, 10, size1, path)


加噪,参考链接:http://blog.****.net/myhaspl/article/details/37693429

  1. def noiseing(img):
  2. param = 30
  3. grayscale = 256
  4. w = img.shape[1]
  5. h = img.shape[0]
  6. newimg = np.zeros((h, w), np.uint8)
  7. for x in xrange(0, h):
  8. for y in xrange(0, w, 2):
  9. r1 = np.random.random_sample()
  10. r2 = np.random.random_sample()
  11. z1 = param * np.cos(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
  12. z2 = param * np.sin(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
  13. fxy = int(img[x, y] + z1)
  14. fxy1 = int(img[x, y + 1] + z2)
  15. if fxy < 0:
  16. fxy_val = 0
  17. elif fxy > grayscale - 1:
  18. fxy_val = grayscale - 1
  19. else:
  20. fxy_val = fxy
  21. if fxy1 < 0:
  22. fxy1_val = 0
  23. elif fxy1 > grayscale - 1:
  24. fxy1_val = grayscale - 1
  25. else:
  26. fxy1_val = fxy1
  27. newimg[x, y] = fxy_val
  28. newimg[x, y + 1] = fxy1_val
  29. cv2.destroyAllWindows()
  30. return newimg


旋转

  1. def rotate(image, angle, center=None, scale=1.0):
  2. (h, w) = image.shape[:2]
  3. # 若未指定旋转中心,则将图像中心设为旋转中心
  4. if center is None:
  5. center = (w / 2, h / 2)
  6. M = cv2.getRotationMatrix2D(center, angle, scale)
  7. rotated = cv2.warpAffine(image, M, (w, h))
  8. return rotated
  9. img1 = rotate(img, 5) #5 -5 10 -10等角度均可 正负值表示顺逆时针



  1. img = cv2.imread(path + '1.png', -1)
  2. size = img.shape
  3. #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #转换了灰度化
  4. size1 = (size[0], size[1] + 1) #把img129,83 补成129,84.
  5. img_1 = 255 * np.ones(size1, np.uint8)
  6. img_1[:, :size[1]] = img
  7. cv2.imshow('preview', img_1)
  8. cv2.waitKey() #显示图像



缩放  +    以下代码是把图像缩小后,又进行边缘padding的操作。  主要采用边缘镜像padding和边缘点像素值padding两种思想:

  1. #coding=utf-8
  2. import cv2
  3. import numpy as np
  4. def ImageScale(img, scale):
  5. size = img.shape
  6. SIZE1 = size[0]
  7. SIZE2 = size[1]
  8. if scale > 1:
  9. size1 = int(SIZE1 * scale) + 1
  10. size2 = int(SIZE2 * scale) #是否加1,根据具体图像尺寸的奇偶决定
  11. #这里需要注意,对于w h 不等的图像, w h 的顺序值需要调整好.
  12. img = cv2.resize(img, (size2, size1), interpolation = cv2.INTER_CUBIC) #双三次线性插值法.
  13. a1 = (size1 - SIZE1) / 2
  14. b1 = size1 - a1
  15. a2 = (size2 - SIZE2) / 2
  16. b2 = size2 - a2
  17. #print a1,b1,a2,b2
  18. img = img[a1:b1, a2:b2]
  19. #print img.shape
  20. else: #即scale<1
  21. size1 = int(SIZE1 * scale)
  22. size2 = int(SIZE2 * scale) + 1 # 是否加1,根据具体图像尺寸的奇偶决定
  23. img = cv2.resize(img, (size2, size1), interpolation=cv2.INTER_CUBIC) # 双三次线性插值法.
  24. return img
  25. def mirrpadding(img, s1, s2):
  26. orgsize = img.shape
  27. size1 = orgsize[0]
  28. size2 = orgsize[1]
  29. a1 = (s1 - size1) / 2 #例:(129-103)/2 = 13
  30. b1 = size1 - a1 #103-13
  31. a2 = (s2 - size2) / 2 #例:(84-68)/2 = 8
  32. b2 = size2 - a2 # 68-8
  33. #print a1,b1,a2,b2
  34. img1 = np.rot90((np.rot90(img[:a1, :a2].T)).T, 3)
  35. print img1.shape, '1'
  36. img2 = np.rot90(img[:a1,:].T)
  37. print img2.shape, '2'
  38. img3 = np.rot90((np.rot90(img[:a1, b2:].T)).T, 3)
  39. print img3.shape, '3'
  40. img4 = np.rot90(img[:,:a2].T, 3)
  41. print img4.shape, '4'
  42. img5 = np.rot90(img[:, b2:].T, 3)
  43. print img5.shape, '5'
  44. img6 = np.rot90((np.rot90(img[b1:, :a2].T)).T, 3)
  45. print img6.shape, '6'
  46. img7 = np.rot90(img[b1:, :].T)
  47. print img7.shape, '7'
  48. img8 = np.rot90((np.rot90(img[b1:, b2:].T)).T, 3)
  49. print img8.shape, '8'
  50. img = np.concatenate((img4, img, img5), axis=1) #concatenate拼接函数,axis=1即在第二个维度上进行拼接.
  51. img1 = np.concatenate((img1, img2, img3), axis=1)
  52. img6 = np.concatenate((img6, img7, img8), axis=1)
  53. img = np.concatenate((img1, img, img6), axis=0)
  54. print img.shape, 'img'
  55. cv2.imwrite('/.../img/mirror.png', img)
  56. #关于填充什么像素值,可以根据图像特点进行修改.
  57. def padding(img, s1, s2): #s1 s2为原图的w h值
  58. img_1 = img
  59. orgsize = img.shape
  60. size1 = orgsize[0]
  61. size2 = orgsize[1]
  62. a1 = (s1 - size1) / 2 #例:(129-103)/2 = 13
  63. b1 = size1 - a1 #103-13
  64. a2 = (s2 - size2) / 2 #例:(84-68)/2 = 8
  65. b2 = size2 - a2 # 68-8
  66. #print a1,b1,a2,b2
  67. img1 = np.zeros([a1, a2],np.uint)
  68. size = img1.shape
  69. for i in range(size[0]):
  70. for j in range(size[1]):
  71. img1[i, j] = img[0, 0] #padding为最上角的像素值.
  72. print img1.shape, '1'
  73. img2 = img_1[:a1,:]
  74. size = img2.shape
  75. for i in range(size[0]):
  76. for j in range(size[1]):
  77. img2[i, j] = img[0, 0] # 得视情况而定...
  78. print img2.shape, '2'
  79. img3 = img_1[:a1, b2:]
  80. size = img3.shape
  81. for i in range(size[0]):
  82. for j in range(size[1]):
  83. img3[i, j] = img[0, 0] #
  84. print img3.shape, '3'
  85. img4 = img_1[:,:a2]
  86. size = img4.shape
  87. for i in range(size[0]):
  88. for j in range(size[1]):
  89. img4[i, j] = img[0, 0] #
  90. print img4.shape, '4'
  91. img5 = img_1[:, b2:]
  92. size = img5.shape
  93. for i in range(size[0]):
  94. for j in range(size[1]):
  95. img5[i, j] = img[0, 0] #
  96. print img5.shape, '5'
  97. img6 = img_1[b1:, :a2]
  98. size = img6.shape
  99. for i in range(size[0]):
  100. for j in range(size[1]):
  101. img6[i, j] = img[0, 0] #
  102. print img6.shape, '6'
  103. img7 = img_1[b1:, :]
  104. size = img7.shape
  105. for i in range(size[0]):
  106. for j in range(size[1]):
  107. img7[i, j] = img[0, 0] #
  108. print img7.shape, '7'
  109. img8 = img_1[b1:, b2:]
  110. size = img8.shape
  111. for i in range(size[0]):
  112. for j in range(size[1]):
  113. img8[i, j] = img[0, 0] #
  114. print img8.shape, '8'
  115. img = np.concatenate((img4, img, img5), axis=1) #concatenate拼接函数,axis=1即在第二个维度上进行拼接.
  116. img1 = np.concatenate((img1, img2, img3), axis=1)
  117. img6 = np.concatenate((img6, img7, img8), axis=1)
  118. img = np.concatenate((img1, img, img6), axis=0)
  119. cv2.imwrite('.../img/padding.png', img)
  120. img = cv2.imread('.../img/1_1.png', -1)
  121. s1 = img.shape[0]
  122. s2 = img.shape[1]
  123. img1 = ImageScale(img, 0.8)
  124. cv2.imwrite('/home/chenjia/HWDB1.1tst_gnt/img/0.8.png', img1)
  125. # img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
  126. # img = cv2.resize(img, (102,102), interpolation = cv2.INTER_CUBIC)
  127. # cv2.imwrite('/home/lenovo/2Tdisk/face/code/test/gray.jpg', img)
  128. mirrpadding(img1, s1, s2)
  129. padding(img1, s1, s2)



镜像padding的手稿:

图像数据增强:平移、加噪、旋转、缩放(padding)1