OpenCV:cvHoughCircles使用中的错误 [英] OpenCV: Error in cvHoughCircles usage

查看:435
本文介绍了OpenCV:cvHoughCircles使用中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using cvHoughCircles to find the two white ovals in the following image:

我首先使用阈值来定位白色区域,然后使用霍夫变换.但是输出不正确,如下所示:

I first used thresholding for locating the white regions and then used Hough Transforms. But the output is not coming correct as shown below:

我无法理解正在发生什么?为什么要检测3个圆,为什么只能正确检测一个?有什么建议?

I am not able to understand what is happening? Why it is detecting 3 circles and why only one is being detected correctly? Any suggestions?

下面是我的代码:

#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坐标吗?

Since I am not get satisfactory results with Hough Transform, I am willing to go for some other way. I can assume that each white blob in the figure is of equal size (size is known)and also the distance between the blob is known. Is there a non-trivial way I can find a vertical line (a tangent) touching the left side of left white blob? Once I know this tangent, I get an idea of the boundary location, then I will draw a circle at x=(this location + radius(which is known)), y= this location. Can I find such x and y coordinates using some non-trivial ways?

已解决,方法如下:

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); 

以下是输出:

推荐答案

您应该使用边缘检测图像作为输入,而不是阈值. 其次,霍夫圆不适用于椭圆,除非它们非常接近圆.我建议阅读有关通用霍夫变换并将其实现为椭圆的方法.

You should use edge detected image as input, not the thresholded. Secondly, Hough circles will not work for elipses, unless they are very close to circles. I recommend reading about Generalized Hough Transform and implementing it for ellipses.

这篇关于OpenCV:cvHoughCircles使用中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆