【问题标题】:Color detection on HoughCircles using OpenCV使用 OpenCV 在 HoughCircles 上进行颜色检测
【发布时间】:2011-10-13 01:09:29
【问题描述】:

我已经检测到 22 个球,并且正在努力寻找一种方法来对这些圆圈运行颜色检测算法以获取它们的颜色。我正在使用 HoughCircles 检测圆圈,但不知道如何检查这些圆圈是什么颜色? 源代码:

#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 = cvGet2D(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;
}

【问题讨论】:

    标签: opencv colors geometry detection


    【解决方案1】:

    如果每个球都有统一的颜色,你可以检查中心的颜色:

    CvMemStorage* storage = cvCreateMemStorage(0);
    cvSmooth(image, image, CV_GAUSSIAN, 5, 5 );
    CvSeq* results = cvHoughCircles(
    image,
    storage,
    CV_HOUGH_GRADIENT,
    2,
    image->width/10
    );
    for( int i = 0; i < results->total; i++ ) 
    {
    float* p = (float*) cvGetSeqElem( results, i );
    CvPoint center = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
    CvScalar c = cvGet2D(image, center.x, center.y); //color of the center
    }
    

    没有测试过代码,但应该没问题。

    编辑:

    糟糕,我忘记了 Get2D 方法的一个参数,即从中获取颜色的实际图像。已更改为正确的形式。

    【讨论】:

    • 谢谢。我现在已经发布了我的源代码,并包含了您的代码。 cvScalar 出现错误。我应该为此添加一个标题吗?
    • 是CvScalar,cvScalar()就是用来实例化这个类型的对象的。
    • 我已经修改了我的代码。声明的 CvScalar c。然后让 c = cvet2D 等它编译成功,但是当我运行它时出现此错误。 imageTest1.exe 中 0x758d9617 处未处理的异常:Microsoft C++ 异常:内存位置 0x0019eed4 处的 cv::Exception .. 我知道我快到了
    【解决方案2】:

    我们在开源视觉框架中编写了自己的斑点检测库: http://www.simplecv.org

    做你想做的事情的代码很简单:

    img = Image("/path/to/image.png")
    blobs = img.findBlobs()
    circle_blobs = blobs.filter(blobs.isCircle() == True)
    list_of_blobs_colors = circle_blobs.meanColor()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 2021-12-25
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2021-08-29
      • 2013-05-05
      相关资源
      最近更新 更多