tags:

views:

178

answers:

2

Hi there

Im using Xcode and c++

I have copied the HoughCircles code from the OpenCV documentation:

#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.

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.

Any ideas please??

A: 

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.

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

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

You should do something like:

HoughCircles(&img, &circles, CV_HOUGH_GRADIENT, 2, img.rows/4, 200, 100 );
karlphillip
Harry
I edited my answer to provide better information for you.
karlphillip
A: 

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

If the 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)

Second, the -> won't work because Mat is 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.

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

The only thing I'm seeing right now is that the dp parameter 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)

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

DigitalMonk