Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import time
- cap = cv2.VideoCapture(0)
- upperOrange = np.array([10,255,255])
- lowerOrange = np.array([0,150,150])
- cnts = []
- while(1):
- ret, img = cap.read()
- k = cv2.waitKey(30) & 0xff
- frame = cv2.resize(img, (0,0), fx=0.25, fy=0.25)
- if k == 27:
- break
- start = time.time()
- hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
- mask = cv2.inRange(hsv, lowerOrange, upperOrange)
- _, cnts, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- if len(cnts) > 0:
- # find the largest contour in the mask, then use
- # it to compute the minimum enclosing circle and
- # centroid
- c = max(cnts, key=cv2.contourArea)
- ((x, y), radius) = cv2.minEnclosingCircle(c)
- M = cv2.moments(c)
- if M["m00"] != 0:
- center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
- # only proceed if the radius meets a minimum size
- if radius > 3:
- # draw the circle and centroid on the frame,
- # then update the list of tracked points
- cv2.circle(frame, (int(x), int(y)), int(radius),
- (0, 255, 255), 2)
- cv2.circle(frame, center, 5, (0, 0, 255), -1)
- end = time.time()
- print(end-start)
- cv2.imshow('frame',frame)
- cap.release()
- cv2.destroyAllWindows()
- cv2.waitKey(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement