You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
alphabot2/python/circul_detect.py

44 lines
1.3 KiB

import cv2
import numpy as np
cam = cv2.VideoCapture(0)
while True:
_, frame = cam.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv = cv2.blur(hsv,(5,5))
mask = cv2.inRange(hsv, (78,154,93),(86,224,255))
(contours, hierarchy) = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contours, -1, (255,0,0), 3, cv2.LINE_AA, hierarchy, 1 )
max_radius = 0
center = (0,0)
for contour in contours:
(x,y),radius = cv2.minEnclosingCircle(contour)
if max_radius < int(radius):
max_radius = int(radius)
center = (int(x),int(y))
frame = cv2.circle(frame,center,max_radius,(0,255,0),2)
S = 3.1415 * max_radius * max_radius
cv2.putText(frame, str(S), (30, 30),cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 10, 10), 2)
if S > 100:
if S > 10000:
cv2.putText(frame, "UP", (30, 60),cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 10, 10), 2)
elif S < 5000:
cv2.putText(frame, "DOWN", (30, 60),cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 10, 10), 2)
#cv2.imshow("Image with opening", mask)
#cv2.imshow("Image with closing", hsv)
cv2.imshow("Image", frame)
k = cv2.waitKey(2)
if k == 27:
cv2.destroyAllWindows()
break