xcode OpenCV HoughCircles

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3387960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:35:23  来源:igfitidea点击:

OpenCV HoughCircles

c++xcodeopencv

提问by Harry

Im using Xcode and c++

我使用 Xcode 和 C++

I have copied the HoughCircles code from the OpenCV documentation:

我已经从OpenCV 文档中复制了 HoughCircles 代码:

#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;
}

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.

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

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.

我的理论是,发生这种情况是因为它没有得到图像或其他东西。但是当我取出 HoughCircles 代码时,相机运行良好。

Any ideas please??

请问有什么想法吗??

采纳答案by karlphillip

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

两者都是编译错误吗?使用. 访问img成员?我敢肯定,你应该使用- >因为在这种情况下,IMG是不是一个指针垫。

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

根据API 参考手册(您应该仔细阅读):

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);

You must pass the memory address of the objects imgand circlesinstead of passing the objects themselves.

您必须传递对象imgcircles的内存地址,而不是传递对象本身。

You should do something like:

你应该做这样的事情:

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

回答by DigitalMonk

First, you must notpass the address of imgand circles. HoughCircles takes references, not pointers. If you attempt to pass &imgand &circles, you will receive new and exciting type errors. Your original code is correct in that regard.

首先,您不能传递imgand的地址circles。HoughCircles 接受引用,而不是指针。如果您尝试通过&imgand &circles,您将收到令人兴奋的新类型错误。在这方面,您的原始代码是正确的。

Ifthe function took pointers, it's declaration would have been:

如果函数接受指针,它的声明应该是:

void HoughCircles(Mat * image, vector<Vec3f> * circles, ...);

(Which it isn't -- I only mention it for anybody else that is confused by the &syntax in C++ function declarations)

(事实并非如此——我只为那些&对 C++ 函数声明中的语法感到困惑的人提到它)

Second, the ->won't work because Matis an object, not a pointer. I'm only mildly surprised that the original documentation is wrong. OpenCV's C++ interface (which you're using, if you're using cv::Mat) is kind of new, and stuff is still transitioning over. This was probably copy/pasted from the C API documentation and that ->was missed.

其次,它->不起作用,因为它Mat是一个对象,而不是一个指针。我只是有点惊讶原始文档是错误的。OpenCV 的 C++ 接口(您正在使用,如果您正在使用cv::Mat)是一种新的东西,并且仍在过渡。这可能是从 C API 文档中复制/粘贴的,但->被遗漏了。

img.rowsshould work (and does work for me). The error you provided for the img.rowscase is not a full error, so I'm not sure why it is a complaint.

img.rows应该工作(并且对我有用)。您为img.rows案例提供的错误不是一个完整的错误,所以我不确定为什么这是一个投诉。

The only thing I'm seeing right now is that the dpparameter is a double, and you're passing an int. Any C++ compiler I've ever used will do that upconversion for you, but if your compiler is in full paranoia mode, you might try changing 2 to 2.0. (I really, really doubt that this is the problem, but without more details on the error, I don't see why you're getting compile-time errors)

我现在唯一看到的是dp参数是 a double,而您正在传递int. 我曾经使用过的任何 C++ 编译器都会为您进行上转换,但是如果您的编译器处于完全偏执模式,您可以尝试将 2 更改为 2.0。(我真的,真的怀疑这是问题所在,但没有关于错误的更多详细信息,我不明白为什么会出现编译时错误)

At runtime, however, it's going to explode because you're passing HoughCircles your original BGR-based imginstead of passing the single channel gray.

但是,在运行时,它会爆炸,因为您正在传递基于原始 BGR 的 HoughCirclesimg而不是传递单个 channel gray