Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. def main(array):
  2.     for x, value in enumerate(array):
  3.         img = cv2.imread(value)
  4.  
  5.         lower_red = numpy.array([255,255,255])
  6.         upper_red = numpy.array([255,255,255])
  7.         mask = cv2.inRange(img, lower_red, upper_red)
  8.  
  9.         contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  10.  
  11.         # area1 and area2 are the range of contour area, change accordingly
  12.         area1 = 100
  13.         area2 = 1000
  14.         totalDots = []
  15.  
  16.         # Count the total number of contours
  17.         for cnt in contours:
  18.             if area1 < cv2.contourArea(cnt) < area2:
  19.                 totalDots.append(cnt)
  20.  
  21.         i = 0
  22.         for c in contours:
  23.             i += 1
  24.             # compute the center of the contour
  25.             M = cv2.moments(c)
  26.             cX = int(M["m10"] / M["m00"])
  27.             cY = int(M["m01"] / M["m00"])
  28.  
  29.             # draw the contour and center of the shape on the image
  30.             #cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
  31.             cv2.circle(img, (cX, cY), 2, (255, 0, 0), -1)
  32.             cv2.putText(img, str(i), (cX - 20, cY - 20),
  33.                 cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
  34.  
  35.         text = "Wszystkich obiektow na zdjeciu: {} ".format(len(totalDots))
  36.         cv2.putText(img, text, (50, 20),  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 255, 255), 2)
  37.         if len(percentage) > 0:
  38.             text1 = "Udzial procentowy obiektow: {} %".format(percentage[x])
  39.             cv2.putText(img, text1, (50, 40),  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 255, 255), 2)
  40.         cv2.imshow(value, img)
  41.         cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement