Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- imgg = cv2.imread('12.png', cv2.IMREAD_GRAYSCALE)
- _, imggt = cv2.threshold(imgg, 150, 255, cv2.THRESH_BINARY_INV)
- _, contours, _ = cv2.findContours(imggt, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
- max_area = 8000
- min_area = 10
- max_rec_area = max_area * 4 / 3.14
- count = 0
- img_with_recs = imggt.copy()
- for cnt in contours:
- if cv2.contourArea(cnt) < max_area and cv2.contourArea(cnt) > min_area:
- [x, y, w, h] = cv2.boundingRect(cnt)
- if w * h < max_rec_area:
- cv2.rectangle(img_with_recs, (x, y), (x + w, y + h), 0, 1)
- count += 1
- print(count)
- cv2.imwrite(str(count) + '.jpg', img_with_recs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement