Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. def draw_MAR(path, flipped=False):
  2.     img = io.imread(path, as_grey=True)
  3.     _, img = cv2.threshold(img, 127, 255, 0)
  4.     im2, contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  5.     cont_len = [len(x) for x in contours]
  6.     index = cont_len.index(max(cont_len))
  7.     cnt = contours[index]
  8.     rect = cv2.minAreaRect(cnt)
  9.     box = cv2.boxPoints(rect)
  10.     box = np.int0(box)
  11.  
  12.     width = int(rect[1][0])
  13.     height = int(rect[1][1])
  14.  
  15.     src_pts = box.astype("float32")
  16.     dst_pts = np.array([[0, height - 1],
  17.                         [0, 0],
  18.                         [width - 1, 0],
  19.                         [width - 1, height - 1]], dtype="float32")
  20.     M = cv2.getPerspectiveTransform(src_pts, dst_pts)
  21.     warped = cv2.warpPerspective(img, M, (width, height))
  22.     img = warped
  23.     # io.imsave("rotated_" + path, img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement