未检测到图像中的多个圆圈

发布于 2024-12-09 12:00:01 字数 1633 浏览 0 评论 0原文

在此处输入图像描述 我的代码很简单。我试图检测 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;
}

enter image description here My code is straightforward. i am trying to detect 22 balls but ionly getting a few. I think it 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-12-16 12:00:01

我相信问题来自于您的 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 个球,也可能水平检测到)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文