下面是一个用Python 2.7.12和OpenCV3.2.0测试的可用代码。在import numpy as np
import cv2
capture = cv2.VideoCapture("001.mp4")
#capture = cv2.VideoCapture(0)
while capture.isOpened():
# grab the current frame and initialize the status text
grabbed, frame = capture.read()
if frame is not None:
# convert the frame to grayscale, blur it, and detect circles
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray,5)
circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,1,20, \
param1=50,param2=30,minRadius=0,maxRadius=0)
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
#circles = np.uint16(np.around(circles[0,:]))
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(frame, (x, y), r, (255, 0, 255), 2)
# show the frame and record if a key is pressed
cv2.imshow("Frame", frame)
# if the 'q' key is pressed, stop the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
主要的变化是在for (x, y, r) in circles:循环中获取并绘制圆。添加cv2.medianBlur()后,视频播放有点慢。通过cv2.HoughCircles()检测,它甚至进一步减慢了速度。在
这是用圆圈播放的视频截图。假设您可能需要修改cv2.HoughCircles()函数参数和圆检索来满足您的需求。在
希望这能有所帮助。在