Advertisement
Guest User

Modified so it'll work with cv2 but the page is black

a guest
Jun 13th, 2019
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # https://stackoverflow.com/a/18908023/2444609
  2. # This is broken. All I get is a black page.
  3.  
  4. import cv2, numpy as np
  5. import sys
  6.  
  7. def get_new(old):
  8. new = np.ones(old.shape, np.uint8)
  9. cv2.bitwise_not(new,new)
  10. return new
  11.  
  12. if __name__ == '__main__':
  13. orig = cv2.imread(sys.argv[1])
  14.  
  15. # these constants are carefully picked
  16. MORPH = 9
  17. CANNY = 84
  18. HOUGH = 25
  19.  
  20. img = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)
  21. cv2.GaussianBlur(img, (3,3), 0, img)
  22.  
  23.  
  24. # this is to recognize white on white
  25. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(MORPH,MORPH))
  26. dilated = cv2.dilate(img, kernel)
  27.  
  28. edges = cv2.Canny(dilated, 0, CANNY, apertureSize=3)
  29.  
  30. lines = cv2.HoughLinesP(edges, 1, 3.14/180, HOUGH)
  31. for line in lines[0]:
  32. cv2.line(edges, (line[0], line[1]), (line[2], line[3]),
  33. (255,0,0), 2, 8)
  34.  
  35. # finding contours
  36. image, contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL,
  37. cv2.CHAIN_APPROX_TC89_KCOS)
  38. contours = filter(lambda cont: cv2.arcLength(cont, False) > 100, contours)
  39. contours = filter(lambda cont: cv2.contourArea(cont) > 10000, contours)
  40.  
  41. # simplify contours down to polygons
  42. rects = []
  43. for cont in contours:
  44. rect = cv2.approxPolyDP(cont, 40, True).copy().reshape(-1, 2)
  45. rects.append(rect)
  46.  
  47. # that's basically it
  48. cv2.drawContours(orig, rects,-1,(0,255,0),1)
  49.  
  50. # show only contours
  51. new = get_new(img)
  52. cv2.drawContours(new, rects,-1,(0,255,0),1)
  53. cv2.GaussianBlur(new, (9,9), 0, new)
  54. new = cv2.Canny(new, 0, CANNY, apertureSize=3)
  55.  
  56. cv2.namedWindow('result', cv2.WINDOW_NORMAL)
  57. cv2.imshow('result', orig)
  58. cv2.waitKey(0)
  59. cv2.imshow('result', dilated)
  60. cv2.waitKey(0)
  61. cv2.imshow('result', edges)
  62. cv2.waitKey(0)
  63. cv2.imshow('result', new)
  64. cv2.waitKey(0)
  65.  
  66. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement