使用python 2.7.11和opencv 3.0.0在实时视频源中查找圆圈

时间:2016-05-23 18:24:51

标签: python opencv hough-transform

我需要帮助从我的网络摄像头中查找实时视频中的圈子。我只需要来自python的反馈,即已检测到或未检测到圆圈。另外,以像素为单位查找圆的大小以获得更好的检测效果的最佳方法是什么。我的代码到目前为止

import cv2
import numpy as np
import sys

cap = cv2.VideoCapture(0)

while(True):

    ret, frame = cap.read()


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray,5)
    cimg = frame.copy()
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 200, 100, 100, 200)
    if circles == 1:
        print('Circle true')
    else:
        print('No circle')
    cv2.imshow('video',gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

有它!

import cv2
import numpy as np
import sys
cap = cv2.VideoCapture(0)
while(True):
    gray = cv2.medianBlur(cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY),5)
    cirles=cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 10)# ret=[[Xpos,Ypos,Radius],...]
    if cirles!=None:print "Circle There !"
    cv2.imshow('video',gray)
    if cv2.waitKey(1)==27:# esc Key
        break
cap.release()
cv2.destroyAllWindows()