image_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import os
  2. import cv2
  3. import glob
  4. import pathlib
  5. import random
  6. import cv2 as cv
  7. import numpy as np
  8. # 将-数字-中的训练数据进行图像数据增强,这里使用随机图像增强方法
  9. # images_data_path = r'E:\datasets\gen_number_str_hw\train'
  10. images_data_path = r'images/Snipaste_2023-06-05_10-31-56.jpg'
  11. # images_save_path = r'E:\datasets\gen_number_str_hw\train_enhance'
  12. # if not os.path.exists(images_save_path):
  13. # os.makedirs(images_save_path)
  14. img = cv.imread(images_data_path)
  15. # cv.imshow('img', img)
  16. # cv.waitKey(1000)
  17. def tfactor(img):
  18. hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
  19. hsv[:, :, 0] = hsv[:, :, 0] * (0.8 + np.random.random() * 0.2)
  20. hsv[:, :, 1] = hsv[:, :, 1] * (0.3 + np.random.random() * 0.7)
  21. hsv[:, :, 2] = hsv[:, :, 2] * (0.2 + np.random.random() * 0.8)
  22. img = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
  23. return img
  24. def AddGauss(img, level=0):
  25. # return cv2.blur(img, (level * 2 + 1, level * 2 + 1))
  26. return cv.blur(img, (1 + r(1), 1 + r(1)))
  27. def r(val):
  28. return int(np.random.random() * val)
  29. def AddNoiseSingleChannel(single):
  30. diff = 255 - single.max()
  31. noise = np.random.normal(0, 1 + r(6), single.shape)
  32. noise = (noise - noise.min()) / (noise.max() - noise.min())
  33. noise = diff * noise
  34. noise = noise.astype(np.uint8)
  35. dst = single + noise
  36. return dst
  37. def addNoise(img, sdev=0.5, avg=10):
  38. img[:, :, 0] = AddNoiseSingleChannel(img[:, :, 0])
  39. img[:, :, 1] = AddNoiseSingleChannel(img[:, :, 1])
  40. img[:, :, 2] = AddNoiseSingleChannel(img[:, :, 2])
  41. return img
  42. def add_noise(img):
  43. for i in range(20): #添加点噪声
  44. temp_x = np.random.randint(0,img.shape[0])
  45. temp_y = np.random.randint(0,img.shape[1])
  46. img[temp_x][temp_y] = 255
  47. return img
  48. def add_erode(img):
  49. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
  50. img = cv2.erode(img,kernel)
  51. return img
  52. # 适当膨胀
  53. def add_dilate(img):
  54. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
  55. img = cv2.dilate(img,kernel)
  56. return img
  57. # img = tfactor(img)
  58. # img = cv.bitwise_not(img)
  59. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  60. # img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  61. img = add_noise(img)
  62. # img = add_dilate(img)
  63. # img = add_erode(img)
  64. cv.imshow('img', img)
  65. cv.waitKey(5000)
  66. # img = AddGauss(img)
  67. # cv.imshow('img', img)
  68. # cv.waitKey(1000)
  69. # img = AddNoiseSingleChannel(img)
  70. # cv.imshow('img', img)
  71. # cv.waitKey(1000)
  72. # img = addNoise(img)
  73. # cv.imshow('img', img)
  74. # cv.waitKey(10000)
  75. image_cnt = 0
  76. # for img_path in glob.glob(images_data_path + '/*.jpg', recursive=True):
  77. # img_file = pathlib.Path(img_path)
  78. # imagename = str(img_file.stem)
  79. # imagesuffix = str(img_file.suffix)
  80. # imgnamewithsuffix = imagename + imagesuffix
  81. # image_cnt = image_cnt + 1
  82. # print(image_cnt)
  83. #
  84. # img = cv.imread(os.path.join(images_data_path, imgnamewithsuffix))
  85. # if img is None:
  86. # print("错误:%s", imgnamewithsuffix)
  87. # continue
  88. # img_h, img_w, img_c = img.shape
  89. # image_src = img[0:, 0:].copy() # 这里可以保存原图
  90. #
  91. # if random.randint(1, 1000) % 5 == 0:
  92. # img = image_src[0:, 0:].copy()
  93. # img = cv.bitwise_not(img) # 得到黑底白字
  94. # elif random.randint(1, 1000) % 7 == 0:
  95. # img = image_src[0:, 0:].copy()
  96. # img = tfactor(img) # 调灰度
  97. # elif random.randint(1, 1000) % 6 == 0:
  98. # img = image_src[0:, 0:].copy()
  99. # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  100. # img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  101. # elif random.randint(1, 1000) % 7 == 0:
  102. # img = image_src[0:, 0:].copy()
  103. # img = AddGauss(img, 0) # 加高斯平滑
  104. # elif random.randint(1, 1000) % 6 == 0:
  105. # img = image_src[0:, 0:].copy()
  106. # img = addNoise(img) # 加噪声
  107. #
  108. # imgname = imagename + imagesuffix
  109. # savepath = os.path.join(images_save_path, imgname)
  110. # cv.imwrite(savepath, img)