使用 Hough 变换检测圆
Detecting Circle Using Hough Transform
我正在尝试使用霍夫变换来检测圆。
使用我当前的代码,我可以检测到下面的代码
但我想在我检测到的圆圈内找到黑洞。
然而,改变 houghcircle 方法的参数对我没有帮助。实际上它发现了不存在的圈子。
我也试过裁剪我找到的圆圈并在这个新部分上做另一个霍夫变换它也没有帮助我。
这是我的代码
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp" // needs imgproc, imgcodecs & highgui
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src, circleroi;
/// Read the image
src = imread( "/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/delikli/gainfull.jpg", 2 );
/// Convert it to gray
// cvtColor( src, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src, src, Size(3, 3), 2, 2 );
// adaptiveThreshold(src,src,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,14);
vector<Vec3f> circles,circlessmall;
// Canny( src, src, 50 , 70, 3 );
/// Apply the Hough Transform to find the circles
HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][4]));
int radius = cvRound(circles[i][5]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
circleroi = src(Rect(center.x - radius, // ROI x-offset, left coordinate
center.y - radius, // ROI y-offset, top coordinate
2*radius, // ROI width
2*radius));
// imshow( "Hough Circle Transform Demo", circleroi );
}
resize(src, src, Size(src.cols/2, src.rows/2));
// threshold( circleroi, circleroi, 50, 255,CV_THRESH_BINARY );
// cout<<circleroi<<endl;
imshow("asd",src);
// imwrite("/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/cikti/deliksiz.jpg",circleroi);
waitKey(0);
return 0;
}
更新:由于hough在里面使用了canny,所以我手动使用canny来查看它是否找到了圆。
这里有精明的结果
Canny(src,src, 100, 200,3);
谢谢
您正在设置 HoughCircles
参数之一 minDist = src.rows/8
,该参数相当大。 docs 说明:
minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
该方法无法 return 它找到的圆和您想要的圆,因为它们具有几乎相同的中心(在 src.rows/8
范围内),只是大小不同。如果将 maxRadius
设置为 30 左右的值以排除较大的圆,是否得到所需的较小圆?
我正在尝试使用霍夫变换来检测圆。
使用我当前的代码,我可以检测到下面的代码
但我想在我检测到的圆圈内找到黑洞。 然而,改变 houghcircle 方法的参数对我没有帮助。实际上它发现了不存在的圈子。
我也试过裁剪我找到的圆圈并在这个新部分上做另一个霍夫变换它也没有帮助我。
这是我的代码
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp" // needs imgproc, imgcodecs & highgui
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src, circleroi;
/// Read the image
src = imread( "/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/delikli/gainfull.jpg", 2 );
/// Convert it to gray
// cvtColor( src, src_gray, CV_BGR2GRAY );
/// Reduce the noise so we avoid false circle detection
GaussianBlur( src, src, Size(3, 3), 2, 2 );
// adaptiveThreshold(src,src,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,14);
vector<Vec3f> circles,circlessmall;
// Canny( src, src, 50 , 70, 3 );
/// Apply the Hough Transform to find the circles
HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][4]));
int radius = cvRound(circles[i][5]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
circleroi = src(Rect(center.x - radius, // ROI x-offset, left coordinate
center.y - radius, // ROI y-offset, top coordinate
2*radius, // ROI width
2*radius));
// imshow( "Hough Circle Transform Demo", circleroi );
}
resize(src, src, Size(src.cols/2, src.rows/2));
// threshold( circleroi, circleroi, 50, 255,CV_THRESH_BINARY );
// cout<<circleroi<<endl;
imshow("asd",src);
// imwrite("/Users/Rodrane/Documents/XCODE/test/mkedenemeleri/alev/cikti/deliksiz.jpg",circleroi);
waitKey(0);
return 0;
}
更新:由于hough在里面使用了canny,所以我手动使用canny来查看它是否找到了圆。
这里有精明的结果
Canny(src,src, 100, 200,3);
谢谢
您正在设置 HoughCircles
参数之一 minDist = src.rows/8
,该参数相当大。 docs 说明:
minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
该方法无法 return 它找到的圆和您想要的圆,因为它们具有几乎相同的中心(在 src.rows/8
范围内),只是大小不同。如果将 maxRadius
设置为 30 左右的值以排除较大的圆,是否得到所需的较小圆?