def track_obj(low_hsv, high_hsv):
# Open the "default" camera
vc = cv2.VideoCapture(0)
# Check if we succeeded in opening camera feed
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
# Display captured frames in a new window
cv2.namedWindow("Camera Video Feed")
# Display filtered object frame in a new window
cv2.namedWindow("Tracking")
result = frame
while rval:
cv2.imshow("Camera Video Feed", frame)
cv2.imshow("Tracking", result)
rval, frame = vc.read()
# Convert to HSV space
frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Filter out components with values in selected range
# Threshold HSV image
mask = cv2.inRange(frameHSV, low_hsv, high_hsv)
result = cv2.bitwise_and(frame, frame, mask = mask)
# Wait for ESC key press
key = cv2.waitKey(20)
if key == 27: # User pressed ESC key
break
# Destroy window
cv2.destroyWindow("Camera Video Feed")
# Close VideoCapture feed -- Important!
vc.release()
评论列表
文章目录