import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame=cap.read()
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img, 5)
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,
1,410,param1=100,param2=30,minRadius=15,maxRadius=25)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow("HoughCirlces", frame)
if cv2.waitKey(97) == ord('a'):
break
cv2.destroyAllWindows()
cap.release()
中minRadius,與maxRadius限制太苛刻,一旦未捕獲到圓,circles為空,後面運算會出錯。
改為minRadius=0,maxRadius=0