image_generator.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import cv2
  2. import math
  3. import pandas as pd
  4. import numpy as np
  5. import os
  6. from TextModel import text_model
  7. def get_range(bboxs):
  8. ranges = []
  9. for i in bboxs:
  10. xs = i[::2]
  11. ys = i[1::2]
  12. box = [min(xs), min(ys), max(xs), max(ys)]
  13. ranges.append(box)
  14. return ranges
  15. def check_range(pixel_points, ranges):
  16. pixel_point = pixel_points[0]
  17. for i in ranges:
  18. if i[2] >= pixel_point[0] >= i[0] and i[3] >= pixel_point[1] >= i[1]:
  19. return 1
  20. return 0
  21. def is_text(bboxs, contours):
  22. ranges = get_range(bboxs)
  23. illustration_box = []
  24. for region in contours:
  25. contain = [check_range(pixel_points, ranges) for pixel_points in region]
  26. contain = sum(contain) / len(contain)
  27. if contain < 0.2:
  28. pixel = [math.inf, math.inf, -1, -1]
  29. for i in region:
  30. if i[0][0] < pixel[0]:
  31. pixel[0] = i[0][0]
  32. if i[0][0] > pixel[2]:
  33. pixel[2] = i[0][0]
  34. if i[0][1] < pixel[1]:
  35. pixel[1] = i[0][1]
  36. if i[0][1] > pixel[3]:
  37. pixel[3] = i[0][1]
  38. x_min, y_min, x_max, y_max = pixel
  39. if (x_max - x_min) * (y_max - y_min) > 30:
  40. illustration_box.append(pixel)
  41. # pts = np.array([[x_min,y_min], [x_max,y_min], [x_max, y_max], [x_min, y_max]], np.int32)
  42. # # 顶点个数:4,矩阵变成4*1*2维
  43. # # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵
  44. # # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的
  45. # pts = pts.reshape((-1, 1, 2))
  46. # cv2.polylines(img, [pts], True, (0, 0, 255)) #画轮廓图
  47. return illustration_box
  48. def processed(text_box, i_box):
  49. i_box = np.array(i_box)
  50. np.sort(i_box, axis=0)
  51. np.sort(i_box, axis=1)
  52. i_box = i_box[::-1][1:]
  53. x_min, y_min, x_max, y_max = 0, 1, 2, 3
  54. tmp_bbx = []
  55. for i in range(len(i_box)):
  56. for j in range(len(i_box)):
  57. if i_box[i][x_min] > i_box[j][x_min] and i_box[i][y_min] > i_box[j][y_min] \
  58. and i_box[i][x_max] < i_box[j][x_max] and i_box[i][y_max] < i_box[j][y_max]:
  59. tmp_bbx.append(i)
  60. inx = [i for i in range(len(i_box)) if i not in tmp_bbx]
  61. i_box_c = [i_box[i] for i in inx]
  62. return i_box_c
  63. def run_cut(path):
  64. if os.path.exists(r'D:\试卷切割\result\image'):
  65. os.system(r'rd /s/q D:\试卷切割\result\image')
  66. if os.path.exists(r'D:\试卷切割\result\text_img'):
  67. os.system(r'rd /s/q D:\试卷切割\result\text_img')
  68. if not os.path.exists(r'D:\试卷切割\result\image'):
  69. os.makedirs(r'D:\试卷切割\result\image')
  70. if not os.path.exists(r'D:\试卷切割\result\text_img'):
  71. os.makedirs(r'D:\试卷切割\result\text_img')
  72. text_bbx = text_model(path)
  73. img = cv2.imread(path)
  74. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  75. ret, binary = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY)
  76. contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1)
  77. i_box = is_text(text_bbx, contours)
  78. i_box = processed(1, i_box)
  79. for pixel in i_box:
  80. x_min, y_min, x_max, y_max = pixel
  81. if (x_max - x_min) * (y_max - y_min) > 30:
  82. pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32)
  83. # 顶点个数:4,矩阵变成4*1*2维
  84. # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵
  85. # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的
  86. # pts = pts.reshape((-1, 1, 2))
  87. # cv2.polylines(img, [pts], True, (0, 0, 255)) #画文字图
  88. cv2.imwrite('./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), img[y_min:y_max, x_min:x_max])
  89. for pixel in get_range(text_bbx):
  90. x_min, y_min, x_max, y_max = pixel
  91. pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32)
  92. # 顶点个数:4,矩阵变成4*1*2维
  93. # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵
  94. # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的
  95. # pts = pts.reshape((-1, 1, 2))
  96. # cv2.polylines(img, [pts], True, (0, 255, 0)) #画插图
  97. cv2.imwrite('./result/text_img/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), img[y_min:y_max, x_min:x_max])
  98. # contours 轮廓所有的点
  99. # cv2.drawContours(img, contours, -1, (0, 0, 255), 3)
  100. # cv2.imshow("img", img)
  101. # cv2.waitKey(0)
  102. # cv2.imwrite('bbb.png', img)
  103. if __name__ == '__main__':
  104. run_cut('./img/5.png')