Evgeny Ekim
Evgeny Ekim

Reputation: 351

Getting circle vector from OpenCV Mat in Android?

I can't get the circle vectors values from the OpenCV Mat in Android. I want to use this function:

HoughCircles(Mat image, Mat circles, int method, double dp, double minDist)

And then I want to show the circles that were found. Where I'm stuck is how to use the circles parameter in this function.

So, the question is: how can I get numbers of 3-element vectors and values of every element in vector from Mat type of OpenCV in Android?

Upvotes: 1

Views: 3588

Answers (3)

acheroncaptain
acheroncaptain

Reputation: 348

Once you have your circles Mat

for (int i = 0; i < circles.cols(); i++)
{
    double vCircle[] = circles.get(0,i);
    double x = vCircle[0];
    double y = vCircle[1];
    double radius = vCircle[2];
}

Upvotes: 2

mevatron
mevatron

Reputation: 14011

Ideally you would want to use a vector<Vec3f> list to process the circles like this:

vector<Vec3f> circles;

// do HoughCircles...

for(size_t i = 0; i < circles.size(); i++)
{
    Vec3f circle = circles[i];
    Point2f center(circle[0] /* x */, circle[1] /* y */);
    float radius = circle[2];

    // use the circle...
}

EDIT : I tried the code just using a Mat, and it appears that the circle parameters are stored as a 1xN matrix with elements of type CV_32FC3, and where N is the number of circles detected. So, each column contains the (x, y, radius) vector you need.

Here is a sample I wrote in C++ showing this:

Mat circleImage = imread("circle.png", 0);
Mat circleDisp;

cvtColor(circleImage, circleDisp, CV_GRAY2RGB);

Mat circles;
HoughCircles(circleImage, circles, CV_HOUGH_GRADIENT, 2, circleImage.rows >> 2, 200, 100);

for( size_t i = 0; i < circles.cols; i++ )
{
    Vec3f vCircle = circles.at<Vec3f>(i);

    Point center(cvRound(vCircle[0]), cvRound(vCircle[1]));
    int radius = cvRound(vCircle[2]);
    // draw the circle center
    circle( circleDisp, center, 3, Scalar(0,255,0), -1, 8, 0 );
    // draw the circle outline
    circle( circleDisp, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
namedWindow( "circles", 1 );
imshow( "circles", circleDisp );
waitKey();

Hope that helps!

Upvotes: 1

Sam
Sam

Reputation: 20058

Just cast your Mat to vector:

HoughCircles(Mat image, Mat circles, int method, double dp, double minDist);

vector<Vec3f> myCircles = (vector<Vec3f>)circles;

Or, simpler

HoughCircles(Mat image, vector<Vec3f>& circles, 
          int method, double dp, double minDist);

Note

This is only true for OpenCV 2.3.1.

Upvotes: 0

Related Questions