【问题标题】:Identify color of a detected circle in opencv识别opencv中检测到的圆圈的颜色
【发布时间】:2011-10-18 00:35:49
【问题描述】:

您将如何使用 opencv 中的 cvHoughcircles 识别每个检测到的圆圈的颜色? 我添加了显示我当前正在尝试的代码,但出现错误:imageTest1.exe 中 0x758d9617 处的未处理异常:Microsoft C++ 异常:内存位置 0x0019eed4 处的 cv::Exception

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

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


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

    //covert to grayscale
    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);

    //detect circles
    CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 35.0, 75, 60,0,0);
    cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);

    //draw all detected circles
    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]);
cvScalar c;
         c = cvGet2D(img, center.x, center.y);//colour of circle

         // 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 );

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

    //create window
    cvNamedWindow("circles", 1);
    cvNamedWindow("SnookerImage", 1);
    //show image in window
    cvShowImage("circles", rgbcanny);
    cvShowImage("SnookerImage", img);

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

    return 0;
}

【问题讨论】:

  • 您可以在这里找到相同的问题:stackoverflow.com/questions/7748350/…
  • 谢谢。我添加了 cvScalar 代码,但现在内存位置出现错误??
  • 你能发布你的代码和错误吗?也许我们可以提供更多这样的帮助。
  • 我已经发布了我的代码和错误。希望能帮到你
  • 嗯,我看到的唯一错误是 cvScalar c,它应该是 CvScalar c。但这应该会产生编译错误。您应该调试它并查看它在哪里崩溃。如果您将图片上传到某个服务器并传递链接,也许我稍后可以查看。

标签: colors opencv geometry


【解决方案1】:

您尝试获取颜色的当前圆的坐标似乎超出了范围。在尝试获取之前,您应该检查每个点是否在图像范围内(例如 center.x > 0 && center.x 0 && center.y

【讨论】:

  • 谢谢。我用整数更改了坐标,它运行了,所以一定是这样。我不知道为什么中心会超出范围,因为我在绘制检测到的圆圈后得到的图像显示了图像内的圆圈中心
猜你喜欢
  • 1970-01-01
  • 2011-03-13
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多