OpenCV:cvHoughCircles使用中出错

时间:2012-05-18 13:20:40

标签: c opencv image-processing hough-transform opencvdotnet

我正在使用cvHoughCircles在下图中找到两个白色椭圆:

enter image description here

我首先使用阈值来定位白色区域,然后使用Hough变换。但输出结果不正确,如下所示:

enter image description here

我无法理解发生了什么?为什么它检测到3个圆圈以及为什么只有一个被正确检测?有什么建议?

以下是我的代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <math.h> 
#include <ctype.h>
#include <stdlib.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include<conio.h>
#include<malloc.h>



using namespace cv;
using namespace std;
int main( ) {
IplImage* image = cvLoadImage( 
"testing.bmp",
  CV_LOAD_IMAGE_GRAYSCALE
);

IplImage* src = cvLoadImage("testing.bmp");
CvMemStorage* storage = cvCreateMemStorage(0);


cvThreshold( src, src,  200, 255, CV_THRESH_BINARY );

CvSeq* results = cvHoughCircles( 
image, 
 storage, 
 CV_HOUGH_GRADIENT, 
 3, 
 image->width/10 
 ); 

 for( int i = 0; i < results->total; i++ ) 
 {
 float* p = (float*) cvGetSeqElem( results, i );
 CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
 cvCircle( 
  src,
  pt, 
  cvRound( p[2] ),
  CV_RGB(0xff,0,0) 
);
}
cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);
} 

编辑:

由于我没有用Hough变换得到满意的结果,我愿意采取其他方式。我可以假设图中的每个白色斑点具有相同的大小(大小已知),并且斑点之间的距离也是已知的。是否有一种非平凡的方式我可以找到一条垂直线(一条切线)触及左侧白色斑点的左侧?一旦我知道这个切线,我就会知道边界位置,然后我会在x =(这个位置+半径(已知))绘制一个圆,y =这个位置。我可以使用一些非平凡的方法找到这样的x和y坐标吗?

通过以下更改解决:

cvThreshold(image, image,  220, 255, CV_THRESH_BINARY );

cvCanny(image, image, 255, 255, 3);


cvNamedWindow( "edge", 1 );
cvShowImage( "edge", image);
cvWaitKey(0);

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* results = cvHoughCircles( 
             image, 
             storage, 
             CV_HOUGH_GRADIENT, 
             4, 
             image->width/4, 100,100,0,50); 

这是输出:

enter image description here

2 个答案:

答案 0 :(得分:3)

It's all about the parameters

IplImage* src = cvLoadImage(argv[1]);
if (!src)
{
    cout << "Failed: unable to load image " << argv[1] << endl;
    return -1;
}

//IplImage* image = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
IplImage* image = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, 1);
cvCvtColor(src, image, CV_RGB2GRAY);

cvThreshold(image, image,  220, 255, CV_THRESH_BINARY );
//  cvNamedWindow( "thres", 1 );
//  cvShowImage( "thres", image);
//  cvWaitKey(0);

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* results = cvHoughCircles( 
                 image, 
                 storage, 
                 CV_HOUGH_GRADIENT, 
                 4, 
                 image->width/3); 

std::cout << "> " << results->total << std::endl;

for( int i = 0; i < results->total; i++ ) 
{
    float* p = (float*) cvGetSeqElem( results, i );
    CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
    cvCircle(src,
             pt, 
             cvRound( p[2] ),
             CV_RGB(0xff,0,0));
}

cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);

如果你做了一些实验,你最终会发现with different parameters you get different results

答案 1 :(得分:2)

您应该使用边缘检测图像作为输入,而不是阈值。 其次,霍夫圈不适用于椭圆形,除非它们非常靠近圆圈。我建议阅读Generalized Hough Transform并将其用于省略号。