如何从openCv中的网络摄像头帧中检测霍夫圈?

时间:2014-08-21 23:40:12

标签: c++ opencv image-processing

我正在尝试使用HoughCircles功能从网络摄像头帧中检测圈子。我使用静态图像尝试了代码并且工作正常但是当我使用我的网络摄像头框架尝试相同的代码时,它没有检测到任何圆圈。我多次测试代码,但它已经死路一条。谁能纠正我的错误。感谢。

#include <opencv2/objdetect/objdetect.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

int main()
{
    Mat frame;
    Mat grayframe;
    VideoCapture cap;
    cap.open(0);

    while(cv::waitKey(27) != 'Esc')
    {
        cap.read(frame);
        cvtColor(frame,grayframe,CV_BGR2GRAY);
        GaussianBlur(grayframe,grayframe,Size(9,9),2,2);
        vector<Vec3f> circles;

        /// Apply the Hough Transform to find the circles
        HoughCircles( grayframe, circles, CV_HOUGH_GRADIENT, 1, grayframe.rows/8, 200, 100, 0, 0 );

        /// Draw the circles detected
        for( size_t i = 0; i < circles.size(); i++ )
        {
            Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
            int radius = cvRound(circles[i][2]);
            // circle center
            circle( frame, center, 3, Scalar(0,255,0), -1, 8, 0 );
            // circle outline
            circle( frame, center, radius, Scalar(0,0,255), 3, 8, 0 );
        }           

        imshow("Hough circles",frame);
    }        
}

0 个答案:

没有答案