视频中分割出圆形
import cv2
import numpy as np
cap = cv2.VideoCapture('video/video.avi')
low_range = np.array([0, 123, 100])
high_range = np.array([5, 255, 255])
lastX = 0
lastY = 0
deltaX = 0
deltaY = 0
while True:
ret, frame = cap.read()
hue_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
threshold_img = cv2.inRange(hue_image, low_range, high_range)
image, contour, hierarchy = cv2.findContours(threshold_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if contour:
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 1, 100, \
param1=20, param2=10, minRadius=10, maxRadius=20)
if circles is not None:
circle = circles[0][0]
x = circle[0]
y = circle[1]
radius = circle[2]
center = (x, y)
deltaX = int(x) - lastX
deltaY = int(y) - lastY
lastX = int(x)
lastY = int(y)
if radius > 5 and lastX != 0 and lastY != 0 and np.abs(deltaX - lastX) > 5 and np.abs(deltaY - lastY) > 5 :
img = cv2.circle(frame, center, radius, (0, 255, 0), 2)
img = cv2.line(frame, (lastX, lastY), (lastX + deltaX, lastY + deltaY), (0, 255, 0), 2)
cv2.imshow('video2', frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()