Search code examples
javaopencvconventions

Please can some one help on convert opencv code in to javacv?


I have found some code which recognize circles in particular image and I was able to convert 90% of that code it in to javacv. But unfortunately I couldn't able to convert following lines in to javacv. So please can some one help me to convert this lines into javacv ?

CvSeq circles = cvHoughCircles(gry, mem, CV_HOUGH_GRADIENT, 1, 40.0, 100, 100,0,0);
cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);

for (int i = 0; i < circles->total; i++)
{
 // round the floats to an int
 float* p = (float*)cvGetSeqElem(circles, i);
 cv::Point center(cvRound(p[0]), cvRound(p[1]));
 int radius = cvRound(p[2]);

 // draw the circle center
 cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );

 // draw the circle outline
 cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );

 printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
} 

I just need to know how to convert 5 code lines which inside the for loop. Please be kind enough to share your experience. Thanks.


Solution

  • Well, I'm not going to convert that code for you (I don't know JavaCV) but here's some useful links for you:


    // Draw lines on the canny contour image
    val colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3)
    cvCvtColor(src, colorDst, CV_GRAY2BGR)
    for (i <- 0 until circles.total) {
        val point = new CvPoint3D32f(cvGetSeqElem(circles, i))
        val center = cvPointFrom32f(new CvPoint2D32f(point.x, point.y))
        val radius = math.round(point.z)
        cvCircle(colorDst, center, radius, CV_RGB(255, 0, 0), 1, CV_AA, 0)
        print(point)
    }
    show(colorDst, "Hough Circles")
    

    This is exactly what you're looking for.