You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

finemapping.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #coding=utf-8
  2. import cv2
  3. import numpy as np
  4. from . import niblack_thresholding as nt
  5. from . import deskew
  6. def fitLine_ransac(pts,zero_add = 0 ):
  7. if len(pts)>=2:
  8. [vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
  9. lefty = int((-x * vy / vx) + y)
  10. righty = int(((136- x) * vy / vx) + y)
  11. return lefty+30+zero_add,righty+30+zero_add
  12. return 0,0
  13. #精定位算法
  14. def findContoursAndDrawBoundingBox(image_rgb):
  15. line_upper = [];
  16. line_lower = [];
  17. line_experiment = []
  18. grouped_rects = []
  19. gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
  20. # for k in np.linspace(-1.5, -0.2,10):
  21. for k in np.linspace(-50, 0, 15):
  22. # thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
  23. # binary_niblack = gray_image > thresh_niblack
  24. # binary_niblack = binary_niblack.astype(np.uint8) * 255
  25. binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
  26. # cv2.imshow("image1",binary_niblack)
  27. # cv2.waitKey(0)
  28. imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
  29. for contour in contours:
  30. bdbox = cv2.boundingRect(contour)
  31. if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
  32. # cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
  33. line_upper.append([bdbox[0],bdbox[1]])
  34. line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
  35. line_experiment.append([bdbox[0],bdbox[1]])
  36. line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
  37. # grouped_rects.append(bdbox)
  38. rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
  39. leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
  40. rows,cols = rgb.shape[:2]
  41. # rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
  42. leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
  43. rows,cols = rgb.shape[:2]
  44. # rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
  45. pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
  46. pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
  47. mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
  48. image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
  49. image,M = deskew.fastDeskew(image)
  50. return image
  51. #多级
  52. def findContoursAndDrawBoundingBox2(image_rgb):
  53. line_upper = [];
  54. line_lower = [];
  55. line_experiment = []
  56. grouped_rects = []
  57. gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
  58. for k in np.linspace(-1.6, -0.2,10):
  59. # for k in np.linspace(-15, 0, 15):
  60. # #
  61. # thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
  62. # binary_niblack = gray_image > thresh_niblack
  63. # binary_niblack = binary_niblack.astype(np.uint8) * 255
  64. binary_niblack = nt.niBlackThreshold(gray_image,19,k)
  65. # cv2.imshow("binary_niblack_opencv",binary_niblack_)
  66. # cv2.imshow("binary_niblack_skimage", binary_niblack)
  67. # cv2.waitKey(0)
  68. imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
  69. for contour in contours:
  70. bdbox = cv2.boundingRect(contour)
  71. if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
  72. # cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
  73. line_upper.append([bdbox[0],bdbox[1]])
  74. line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
  75. line_experiment.append([bdbox[0],bdbox[1]])
  76. line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
  77. # grouped_rects.append(bdbox)
  78. rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
  79. leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
  80. rows,cols = rgb.shape[:2]
  81. # rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
  82. leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
  83. rows,cols = rgb.shape[:2]
  84. # rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
  85. pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
  86. pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
  87. mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
  88. image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
  89. image,M= deskew.fastDeskew(image)
  90. return image