【问题标题】:Not detecting multiple circles in Image未检测到图像中的多个圆圈
【发布时间】:2011-12-04 17:52:39
【问题描述】:

我的代码很简单。我正在尝试检测 22 个球,但只得到了几个。我认为这与CvSeq* circles = cvHoughCircles有关,谁能帮助我,谢谢!

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

【问题讨论】:

    标签: opencv geometry detection


    【解决方案1】:

    我相信问题出在你的 cvHoughCircles 参数上:

    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 – 检测到的圆的中心之间的最小距离。如果参数太小,除了一个真实的圆圈外,可能还会错误地检测到多个相邻圆圈。如果太大,可能会漏掉一些圆圈。

    您使用的 minDist 可能太大(在您的情况下,最多可以垂直检测 2-3 个球,也可能水平检测)。

    【讨论】:

    • 是的,我调整了代码,结果在图片中。它有帮助,但是仍然没有检测到很多球?还有更多信息吗?谢谢
    • 它应该比球的直径小一点,所以如果图像中的球不会改变大小,那么您可以使用检测到的值。
    猜你喜欢
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    相关资源
    最近更新 更多