Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- img = cv2.imread('portu_form.jpg', cv2.IMREAD_GRAYSCALE)
- norm_img = cv2.normalize(img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
- _, th = cv2.threshold(norm_img, 64, 255, cv2.THRESH_BINARY)
- _, contours, _ = cv2.findContours(th, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
- good_contours = []
- for contour in contours:
- perimeter = cv2.arcLength(contour, True)
- approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True)
- if len(approx) == 4:
- (x, y, w, h) = cv2.boundingRect(approx)
- ar = w / float(h)
- if (ar >= 0.95) and (ar < 1.05):
- if (w > 20) and (w < 100):
- good_contours.append(approx)
- output = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
- cv2.drawContours(output, good_contours, -1, (0,255,0), 3)
- cv2.imwrite('portu_out.png', output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement