在OpenCV 2.4.6中使用Hough变换检测/拟合圆
目标是检测图像中的5个白色圆圈。必须检测圆圈的测试图像如图所示在OpenCV 2.4.6中使用Hough变换检测/拟合圆,opencv,geometry,detection,hough-transform,Opencv,Geometry,Detection,Hough Transform,目标是检测图像中的5个白色圆圈。必须检测圆圈的测试图像如图所示 请在这里下载原始图像 我使用不同的方法对各种圆/椭圆检测方法进行评估。但不知何故,我无法修复我的简单Hough变换代码。它不检测任何圆。我不清楚问题是在于预处理步骤,还是霍夫圆的参数。我已经在论坛上讨论了所有类似的问题,但仍然无法解决这个问题。这是我的密码。在这方面请帮助我 头文件 #ifndef IMGPROCESSOR_H #define IMGPROCESSOR_H // OpenCV Library
请在这里下载原始图像 我使用不同的方法对各种圆/椭圆检测方法进行评估。但不知何故,我无法修复我的简单Hough变换代码。它不检测任何圆。我不清楚问题是在于预处理步骤,还是霍夫圆的参数。我已经在论坛上讨论了所有类似的问题,但仍然无法解决这个问题。这是我的密码。在这方面请帮助我 头文件
#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H
// OpenCV Library
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
class ImgProcessor{
public:
Mat OpImg ;
ImgProcessor();
~ImgProcessor();
//aquire filter methods to image
int Do_Hough(Mat IpImg);
};
#endif /* ImgProcessor_H */
\ifndef IMGPROCESSOR\u H
#定义IMGPROCESSOR\u H
//OpenCV库
#包括
#包括
使用名称空间cv;
使用名称空间std;
类IMG处理器{
公众:
Mat OpImg;
ImgProcessor();
~ImgProcessor();
//一种图像的模糊滤波方法
int Do_Hough(Mat IpImg);
};
#endif/*ImgProcessor\u H*/
源文件
#include "ImgProcessor.h"
#include <opencv2\opencv.hpp>
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\imgproc\imgproc_c.h"
#include <vector>
using namespace cv;
ImgProcessor::ImgProcessor(){
return;
}
ImgProcessor::~ImgProcessor(){
return;
}
//Apply filtering for the input image
int ImgProcessor::Do_Hough(Mat IpImg)
{
//Parameter Initialization________________________________________________________
double sigma_x, sigma_y, thresh=250, max_thresh = 255;
int ksize_w = 5 ;
int ksize_h = 5;
sigma_x = 0.3*((ksize_w-1)*0.5 - 1) + 0.8 ;
sigma_y = 0.3*((ksize_h-1)*0.5 - 1) + 0.8 ;
vector<Vec3f> circles;
//Read the image as a matrix
Mat TempImg;
//resize(IpImg, IpImg ,Size(), 0.5,0.5, INTER_AREA);
//Preprocessing__________________________________________________________
//Perform initial smoothing
GaussianBlur( IpImg, TempImg, Size(ksize_w, ksize_h),2,2);
//perform thresholding
threshold(TempImg,TempImg, thresh,thresh, 0);
//Remove noise by gaussian smoothing
GaussianBlur( TempImg, TempImg, Size(ksize_w, ksize_h),2,2);
/*imshow("Noisefree Image", TempImg);
waitKey(10000);*/
//Obtain edges
Canny(TempImg, TempImg, 255,240 , 3);
imshow("See Edges", TempImg);
waitKey(10000);
//Increase the line thickness
//dilate(TempImg,TempImg,0,Point(-1,-1),3);
//Hough Circle Method______________________________________________________________
// Apply the Hough Transform to find the circles
HoughCircles( TempImg, circles, 3, 1, TempImg.rows/32, 255, 240, 5, 0 );
// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// circle center
circle( IpImg, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( IpImg, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
// Show your results
namedWindow( "Hough Circle Transform", WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform", IpImg );
// waitKey(0);
return 0;
}
int main(int argc, char** argv)
{
ImgProcessor Iclass;
//char* imageName = argv[1];
string imageName = "D:/Projects/test_2707/test_2707/1.bmp";
Mat IpImg = imread( imageName );
cvtColor(IpImg, IpImg,6,CV_8UC1);
Iclass.Do_Hough(IpImg);
/*Iclass.Do_Contours(IpImg);*/
return 0;
}
#包括“ImgProcessor.h”
#包括
#包括“opencv2\imgproc\imgproc.hpp”
#包括“opencv2\imgproc\imgproc\u c.h”
#包括
使用名称空间cv;
ImgProcessor::ImgProcessor(){
返回;
}
ImgProcessor::~ImgProcessor(){
返回;
}
//对输入图像应用过滤
int-ImgProcessor::Do_-Hough(Mat-IpImg)
{
//参数初始化________________________________________________________
双西格玛x,西格玛y,阈值=250,最大阈值=255;
int ksize_w=5;
int ksize_h=5;
sigma_x=0.3*((ksize_w-1)*0.5-1)+0.8;
sigma_y=0.3*((ksize_h-1)*0.5-1)+0.8;
矢量圆;
//以矩阵形式读取图像
Mat TempImg;
//调整大小(IpImg,IpImg,Size(),0.5,0.5,内部区域);
//预处理__________________________________________________________
//执行初始平滑
GaussianBlur(IpImg,TempImg,Size(ksize_w,ksize_h),2,2);
//进行阈值化
阈值(TempImg,TempImg,thresh,thresh,0);
//高斯平滑去除噪声
GaussianBlur(TempImg,TempImg,Size(ksize_w,ksize_h),2,2);
/*imshow(“Noisefree图像”,TempImg);
waitKey(10000)*/
//获得优势
坎尼(坦皮姆,坦皮姆,255240,3);
imshow(“见边”,TempImg);
waitKey(10000);
//增加线的厚度
//扩张(TempImg,TempImg,0,点(-1,-1),3);
//霍夫圆法______________________________________________________________
//应用Hough变换查找圆
HoughCircles(TempImg,circles,3,1,TempImg.rows/32255,240,5,0);
//画出检测到的圆
对于(size_t i=0;i
代码似乎很好,除了:
HoughCircles( TempImg, circles, 3, 1, TempImg.rows/32, 255, 240, 5, 0 );
参数列表中的数字3是否对应于CV_HOUGH_梯度?最好使用定义而不是数字
也许你应该先用一个大圆圈的图像来测试它。一旦确定代码的其余部分正确无误,就可以调整HoughCircles的参数。是的,它确实对应于CV_HOUGH_GRADIENT。。我接受你的建议,今后也会这样做。我还尝试了从谷歌随机获得的其他测试图像,它们似乎都能很好地使用算法。很好,所以我们开始测试参数。搜索的最小半径为5个像素,与调整大小的图像中的像素大小差不多。最大半径0表示没有限制。可能您可以进一步减小最小半径,比如说减小到3,然后重试。我想知道原因是否是圆圈太小,导致累加器缓冲区中的分数较低。您还可以尝试获取轮廓,并使用轮廓长度检测正确大小的圆。