基于EmguCV(C#、VS2015)的HoughCircles圆形检测存在5-10%误差求助
Hey there! Let's work through that circle detection accuracy problem you're having with EmguCV, C#, and Visual Studio 2015. Given your specific scenario—black circles (~80px radius) on slightly dusty white paper with no overlapping circles—here are targeted, actionable tweaks to cut down that 5-10% error rate:
1. Refine Preprocessing: Threshold First, Denoise Second
Your current denoising step might be blurring circle edges or not handling dust effectively. Since you're dealing with high-contrast black-on-white content, adaptive thresholding will crush most dust specks while preserving circle edges, then mild denoising cleans up any remaining noise:
Mat mat = new Mat(height, width, DepthType.Cv8U, 1, ptrImg, width); // Step 1: Adaptive threshold to isolate circles from dust/background Mat thresholded = new Mat(); CvInvoke.AdaptiveThreshold(mat, thresholded, 255, AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, 11, 2); // Step 2: Mild denoising to smooth edges without blurring CvInvoke.FastNlMeansDenoising(thresholded, thresholded, 10);
Adaptive thresholding adapts to local lighting variations (if any) and turns faint dust specks white (matching the background), while keeping your black circles sharp. Lowering the denoising strength from 20 to 10 preserves the edge gradient the Hough detector needs.
2. Tighten Hough Circles Parameters to Match Your Circle Size
Your existing Hough parameters are close, but we can narrow them to eliminate false positives and improve precision:
var circles = CvInvoke.HoughCircles( thresholded, HoughType.Gradient, 1.5, // Balances accuracy/speed (1.0 = full resolution, slower) 170.0, // minDist > 2*radius (160px) to avoid duplicate detections 60, // Lower Canny high threshold (low threshold = 30) for sharp edges 70, // Higher accumulator threshold to filter dust-related false hits 75, // Narrow minRadius to your target range 85 // Narrow maxRadius to exclude irrelevant detections );
minDist=170: Ensures we don't detect the same circle multiple times (safe since you have no adjacent circles)param1=60: Perfect for high-contrast black-on-white edges—Canny will capture clean, clear circle perimetersparam2=70: Raises the bar for valid circles, filtering out tiny dust specks that might trigger false accumulations75-85px radius range: Tightens detection to your known circle size, eliminating any outliers
3. Add Morphological Cleaning to Erase Residual Dust
Even after thresholding, tiny dust specks might remain. A morphological opening operation erases these small artifacts without affecting your 80px circles:
// Create a small 3x3 structuring element Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1)); // Apply opening (erosion + dilation) to remove small dark specks CvInvoke.MorphologyEx(thresholded, thresholded, MorphOp.Open, kernel);
This step cleans up the image further, making the Hough detector's job much simpler.
4. Post-Process Detections for a Final Safety Net
Add a quick validation step to filter any remaining outliers that slipped through preprocessing:
if (circles != null) { List<CircleF> validCircles = new List<CircleF>(); foreach (var circle in circles) { // Keep only circles within a tight radius range (adjust as needed) if (circle.Radius >= 78 && circle.Radius <= 82) { // Optional: Verify pixel intensity along the circle's circumference to confirm it's a solid black circle validCircles.Add(circle); } } return validCircles.ToArray(); } return null;
This final check ensures only circles matching your exact size make it through.
内容的提问来源于stack exchange,提问作者Anthony L. Gershman

