colors - 识别opencv中检测到的圆圈的颜色

标签 colors opencv geometry

您将如何使用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;
}

最佳答案

您试图获取颜色的当前圆的坐标看起来超出范围。在获取之前,应检查每个点是否在图像范围内(类似于center.x> 0 && center.x 0 && center.y

关于colors - 识别opencv中检测到的圆圈的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7801203/

相关文章:

search - 找到一组图像中色彩最丰富的图像

c++ - 安装 opencv 时出错,我正在使用 lion 和 xcode4.4

c# - 线程导致应用程序在 iOS 上滞后,但在 Android 上不会

geometry - 给定 2 点,我如何画一条与两点形成的线成直角的线?

algorithm - 如何将构成凸包的半空间转换为一组极值点?

css - 按钮、 rails 中的文本颜色

java - 在 Windows 中更改 org.eclipse.swt.widgets 背景颜色

opencv - 旋转矩形 - ANGLE 返回 - Opencv

c++ - cos 和 sin 没有提供预期的输出

R:ggplot 在 2x2 阶乘实验中填充一个因子