2014-03-03 65 views
0

我對python和OpenCV很陌生。我想要做的是在視頻的一部分中找到一個圓圈,使用hough圓圈並跟蹤該圓圈移動穿過攝像頭的視野。我可以在靜態圖像上使用輪圈。這是我迄今爲止所做的,但我不認爲它接近正在工作。如何在視頻中使用圓圈檢測

import cv2 
import numpy as np 

img = cv2.VideoCapture(0) 

circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20, 
         param1=200,param2=100,minRadius=0,maxRadius=0) 

circles = np.uint16(np.around(circles)) 
    for i in circles[0,:]: 
    # draw the outer circle 
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) 
    # draw the center of the circle 
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) 

cv2.imshow('detected circles',img) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 

回答

1

對於視頻分析,你必須不斷從你的攝像頭(在這種情況下)讀取圖像。所以你需要一個循環,如OpenCV的教程所示。

import cv2 
import numpy as np 

cap = cv2.VideoCapture(0) 

while(1): 

    #read frame from capture 
    img = cap.read() 

    ############################## 
    # here do the whole stuff with circles and your actual image 
    ############################## 

    cv2.imshow('show image', img) 

    #exit condition to leave the loop 
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
      break 

cv2.destroyAllWindows() 
cap.release() 
相關問題