OpenCV HoughCircles [英] OpenCV HoughCircles

查看:148
本文介绍了OpenCV HoughCircles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode和c ++

Im using Xcode and c++

我已经从 OpenCV文档:

#include <cv.h>
#include <highgui.h>
#include <math.h>

using namespace cv;

int main(int argc, char** argv)
{
    Mat img, gray;
    if( argc != 2 && !(img=imread(argv[1], 1)).data)
        return -1;
    cvtColor(img, gray, CV_BGR2GRAY);
    // smooth it, otherwise a lot of false circles may be detected
    GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
    vector<Vec3f> circles;
    HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
                 2, gray->rows/4, 200, 100 );
    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]);
         // draw the circle center
         circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
         // draw the circle outline
         circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
    }
    namedWindow( "circles", 1 );
    imshow( "circles", img );
    return 0;
}

然后按如下所示对其进行修改:

then modified it like this:

int main(int argc, char** argv)
{
    VideoCapture cap(0);
    if(!cap.isOpened())
        return -1;
    namedWindow( "circles", 1 );

    Mat img, gray;
    for( ;; )
    {

        cap >> img;
        vector<Vec3f> circles;      
        cvtColor(img, gray, CV_BGR2GRAY);
        GaussianBlur(gray, gray, Size(7,7), 1.5, 1.5);
        HoughCircles(img, circles, CV_HOUGH_GRADIENT, 2, img->rows/4, 200, 100 );
        imshow( "circles", img );
        if(waitKey(30) >= 0) break;
    }

    return 0;
}

在两种情况下我都收到错误:错误:->"的基本操作数具有非指针类型"cv :: Mat" 然后,我将->替换为.仍然出现另一个错误.这与我从文档中复制的代码相同.

I get the error on both cases: error: base operand of '->' has non-pointer type 'cv::Mat' i then replace the -> with . and still get another error. This is the same with the code that i copied from the documentation.

我的理论是,发生这种情况是因为它没有得到图像和东西.但是当我取出HoughCircles代码时,相机运行正常.

My theory is that this happens because its not getting and image or somehting. but when i take the HoughCircles code out, the camera runs fine.

请问有什么想法吗?

推荐答案

都是编译错误吗?使用.访问 img 成员时会遇到什么错误?我确定您在这种情况下不应该使用-> ,因为 img 不是指向Mat的指针.

Are both compiling errors? What error do you get when you use . to access img members? I'm certain that you should not use -> in this case since img is not a pointer to Mat.

根据 API参考手册(您应该对此进行抢劫):

According to the API reference manual (you should take a loot at it):

void HoughCircles(Mat& image, vector<Vec3f>& circles, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0);

您必须传递对象 img circles 的内存地址,而不是传递对象本身.

You must pass the memory address of the objects img and circles instead of passing the objects themselves.

您应该执行以下操作:

HoughCircles(&img, &circles, CV_HOUGH_GRADIENT, 2, img.rows/4, 200, 100 );

这篇关于OpenCV HoughCircles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆