Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import cv2
  2. imgg = cv2.imread('12.png', cv2.IMREAD_GRAYSCALE)
  3.  
  4. _, imggt = cv2.threshold(imgg, 150, 255, cv2.THRESH_BINARY_INV)
  5. _, contours, _ = cv2.findContours(imggt, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
  6.  
  7. max_area = 8000
  8. min_area = 10
  9. max_rec_area = max_area * 4 / 3.14
  10. count = 0
  11.  
  12. img_with_recs = imggt.copy()
  13. for cnt in contours:
  14.     if cv2.contourArea(cnt) < max_area and cv2.contourArea(cnt) > min_area:
  15.         [x, y, w, h] = cv2.boundingRect(cnt)
  16.         if w * h < max_rec_area:
  17.             cv2.rectangle(img_with_recs, (x, y), (x + w, y + h), 0, 1)
  18.             count += 1
  19. print(count)
  20. cv2.imwrite(str(count) + '.jpg', img_with_recs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement