Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main(array):
- for x, value in enumerate(array):
- img = cv2.imread(value)
- lower_red = numpy.array([255,255,255])
- upper_red = numpy.array([255,255,255])
- mask = cv2.inRange(img, lower_red, upper_red)
- contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
- # area1 and area2 are the range of contour area, change accordingly
- area1 = 100
- area2 = 1000
- totalDots = []
- # Count the total number of contours
- for cnt in contours:
- if area1 < cv2.contourArea(cnt) < area2:
- totalDots.append(cnt)
- i = 0
- for c in contours:
- i += 1
- # compute the center of the contour
- M = cv2.moments(c)
- cX = int(M["m10"] / M["m00"])
- cY = int(M["m01"] / M["m00"])
- # draw the contour and center of the shape on the image
- #cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
- cv2.circle(img, (cX, cY), 2, (255, 0, 0), -1)
- cv2.putText(img, str(i), (cX - 20, cY - 20),
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
- text = "Wszystkich obiektow na zdjeciu: {} ".format(len(totalDots))
- cv2.putText(img, text, (50, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 255, 255), 2)
- if len(percentage) > 0:
- text1 = "Udzial procentowy obiektow: {} %".format(percentage[x])
- cv2.putText(img, text1, (50, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 255, 255), 2)
- cv2.imshow(value, img)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement