0
目標是檢測圖像中的5個白色圓圈。其中圓圈必須被檢測到的測試圖像是這裏顯示的圖像640x480
請下載原始圖像在這裏,1280x1024在OpenCV 2.4.6中使用霍夫變換檢測/擬合圓
我正在使用不同的方法來展示各種圓/橢圓檢測方法的評估。但不知何故,我無法修復我簡單的Hough變換代碼。它不檢測任何圈子。我不清楚問題是否與預處理步驟或HoughCircle的參數有關。我已經通過論壇中所有類似的問題,但仍然無法解決問題。這是我的代碼。請幫我在這方面..
頭文件
#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 */
源文件
#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;
}
是的,它確實對應於** CV_HOUGH_GRADIENT ** ..我接受你的建議,並會在這裏做同樣的事情。我也嘗試過從谷歌隨機獲得的其他測試圖像,並且他們似乎都能很好地使用該算法.. – Robionic
好,所以我們只需要測試參數。您要搜索的最小半徑是5個像素,這與調整大小的圖像中的像素大小差不多。最大半徑0應該表示沒有限制。 – Totoro
也許你可以進一步減小最小半徑,比如說3,然後再試一次。我想知道原因是圓圈的大小,導致累加器緩衝區的分數較低。您也可以嘗試獲取輪廓,並使用輪廓長度來檢測正確尺寸的圓圈。 – Totoro