Advertisement
Guest User

Untitled

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