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

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

https://stackoverflow.com/questions/7801203/

相关文章:

opencv - 使用openCV GMM类进行对象建模

visual-c++ - OpenCV Connected Component标签颜色?

android - 使用opencv在android中抓取连续帧

opencv - GLSL和OpenCV

ubuntu - 在 Ubuntu 中编译基于 OpenCV 和 ROS 的项目

c# - Windows 7 x32下C#中dllimport的问题

opencv - 无法使用 OpenCV 从 FireWire cam 捕获图像

opencv - Linux上的OpenCV编译:如何向其提供特定的zlib lib?

python-3.x - 队列的等价arg 'durable'

c# - 如何申请cvsobel + emgucv