我使用的是 Visual Studio 2015、OpenCV.3 和 EmguCV.3。
我的代码如下所示,结果如图所示。我知道问题是HoughCircles函数的输入值,但不知道哪些输入适合这张图片。我很感激任何帮助。
Image<Gray, byte> OriginalImage = new Image<Gray, byte>(Openfile.FileName);
Image<Gray, byte> ResizedImage = OriginalImage.Resize(OriginalImage.Width / 2, OriginalImage.Height / 2, Emgu.CV.CvEnum.Inter.Cubic);
//********** Convert Image to Binary
Image<Gray, byte> smoothImg =
ResizedImage.SmoothGaussian(5);
smoothImg._Erode(5);
smoothImg._Dilate(5);
Image<Gray, byte> BinaryImage =
smoothImg.ThresholdBinary(new Gray(20), new Gray(255));
//********** Find Circles
Image<Rgb, byte> ROIImgScaledCircles = ROIImgScaled.Convert<Rgb, byte>();
CircleF[] circles = smoothImg.HoughCircles(
new Gray(180),//cannyThreshold
new Gray(60),//circleAccumulatorThreshold
2.0, //dp:Resolution of the accumulator used to detect centers of the circles
10.0, //min distance
10, //min radius
128 //max radius
)[0]; //Get the circles from the first channel
foreach (CircleF cir in circles)
{
ROIImgScaledCircles.Draw(cir, new Rgb(235, 20, 30), 1);
}
pbxCircles.Image = ROIImgScaledCircles.ToBitmap();
原图:
成立圈子:
最佳答案
使用完整的形状,您可能会发现检测边缘然后找到轮廓更容易。下面是一个例子:
Image<Bgr, byte> original = new Image<Bgr, byte>(@"E:\Downloads\original.jpg");
UMat grayscale = new UMat();
UMat pyrdown = new UMat();
UMat canny = new UMat();
double cannyThreshold = 128;
CvInvoke.CvtColor(original, grayscale, ColorConversion.Bgr2Gray);
// remove noise and run edge detection
CvInvoke.PyrDown(grayscale, pyrdown);
CvInvoke.PyrUp(pyrdown, grayscale);
CvInvoke.Canny(grayscale, canny, cannyThreshold, cannyThreshold * 2);
Image<Bgr, byte> result = original.Copy();
// find and draw circles
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(canny, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
//CvInvoke.DrawContours(result, contours, -1, new MCvScalar(0, 0, 255));
for (int i = 0; i < contours.Size; i++)
{
Ellipse ellipse = new Ellipse(CvInvoke.FitEllipse(contours[i]));
result.Draw(ellipse, new Bgr(Color.Red), 1);
}
result.Save(@"E:\Downloads\circles.jpg");
结果如下,从左到右:
关于visual-studio - HoughCircles 在 OpenCV 中无法正确检测圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987875/