0

我想检测图像中的圆圈。这是我尝试检测此图像中圆圈数量的代码:在此处输入图像描述

 #include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>

/** @function main */
int main(int argc, char** argv)
{
  //load image from directory
    IplImage* gray = cvLoadImage("comb.png",0);


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

    //covert to grayscale
    cvCvtColor(gray, gray, CV_RGB2GRAY);

    // 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(gray),IPL_DEPTH_8U,1);
    //IplImage* rgbcanny = cvCreateImage(cvGetSize(gray),IPL_DEPTH_8U,3);
  cvCanny(gray, canny, 10, 20, 3);

    //detect circles
    CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, gray->width/10);

    if(circles->total>0)
    printf("circles found: %d\n",circles->total);
    else
    printf("circles not found");


  return 0;
}

但出现以下错误:- OpenCV 错误:cvCvtColor 中的断言失败(dst.data == dst0.data),文件 /OpenCV/OpenCV-2.4.3/modules/imgproc/src/color.cpp,第 3811 行终止后调用抛出 'cv::Exception' 的实例 what(): /OpenCV/OpenCV-2.4.3/modules/imgproc/src/color.cpp:3811: error: (-215) dst.data == dst0.data in函数 cvCvtColor

请告诉我程序有什么问题。我使用的是 ubuntu linux。

4

1 回答 1

0

gray以灰度图像打开(cvLoadImage("comb.png",0),“0”表示“灰度”)。然后您尝试将此灰色帧从 RGB 转换为灰色,当然这无法正常工作,因为您的图像已经是灰色的。

只需尝试注释掉以下行:

//covert to grayscale
cvCvtColor(gray, gray, CV_RGB2GRAY);

顺便说一句,你应该使用

IplImage* gray = cvLoadImage("comb.png", CV_LOAD_IMAGE_GRAYSCALE);

代替

IplImage* gray = cvLoadImage("comb.png", 0);

那更清楚了。

于 2013-10-26T22:37:59.607 回答