开发者

Not detecting multiple circles in Image

开发者 https://www.devze.com 2023-04-11 20:33 出处:网络
My code is straightforward.i am trying to detect 22 balls but ionly getting a few. I think i开发者_如何学Pythont has something to do with the CvSeq* circles = cvHoughCirclesCan anyone help me please

Not detecting multiple circles in Image

My code is straightforward. i am trying to detect 22 balls but ionly getting a few. I think i开发者_如何学Pythont has something to do with the CvSeq* circles = cvHoughCircles Can anyone help me please and thank you!

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

int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage("C:\\Users\\Nathan\\Desktop\\SnookerPic.png");


    IplImage* gray = cvCreateImage

(cvGetSize(img), IPL_DEPTH_8U, 1);
CvMemStorage* storage = cvCreateMemStorage(0);

cvCvtColor(img, gray, CV_BGR2GRAY);

// This is done so as to prevent a lot of false circles from being detected
cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7);

IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(gray, canny, 50, 100, 3);

CvSeq* circles = cvHoughCircles(gray, storage, 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);
}


cvNamedWindow("circles", 1);
cvNamedWindow("Image", 1);
cvShowImage("circles", rgbcanny);
cvShowImage("Image", img);

cvSaveImage("out.png", rgbcanny);
cvWaitKey(0);

return 0;
}


I believe that the problem comes from your cvHoughCircles parameters:

CvSeq* cvHoughCircles(CvArr* image, CvMemStorage* circleStorage, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0 )

minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.

You are using maybe a too large minDist (in your case max 2-3 balls will be detected vertically and probably also horizontally).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号