Advertisement
dan-masek

Untitled

Nov 5th, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. img = cv2.imread('portu_form.jpg', cv2.IMREAD_GRAYSCALE)
  5. norm_img = cv2.normalize(img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
  6. _, th = cv2.threshold(norm_img, 64, 255, cv2.THRESH_BINARY)
  7. _, contours, _ = cv2.findContours(th, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  8.  
  9.  
  10. good_contours = []
  11. for contour in contours:
  12.     perimeter = cv2.arcLength(contour, True)
  13.     approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True)
  14.     if len(approx) == 4:
  15.         (x, y, w, h) = cv2.boundingRect(approx)
  16.         ar = w / float(h)
  17.         if (ar >= 0.95) and (ar < 1.05):
  18.             if (w > 20) and (w < 100):
  19.                 good_contours.append(approx)
  20.  
  21.  
  22. output = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)                
  23. cv2.drawContours(output, good_contours, -1, (0,255,0), 3)
  24. cv2.imwrite('portu_out.png', output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement