c# - 检测自定义形状中的圆圈
问题描述
我需要帮助来检测图像中的圆圈,当我运行代码时,不会搜索圆圈,而是返回的形状是椭圆,抱歉英语不好
输入图像:
结果图片:
我的代码:
public Mat Run(string imgPath , int thresholdValue = 150)
{
var img=Cv2.ImRead(imgPath);
Bitmap bitmap = MatToBitmap(img);
pictureBox1.Image = bitmap;
Mat gray = img.CvtColor(ColorConversionCodes.BGR2GRAY);
ThresholdImg = gray.Threshold(thresholdValue, 255, ThresholdTypes.Binary);
Bitmap threshold = MatToBitmap(ThresholdImg);
pictureBox2.Image = threshold;
//Cv2.ImShow("Threshold", ThresholdImg);
Mat gaussImg = ThresholdImg.GaussianBlur(new OpenCvSharp.Size(5, 5), 0.8);
//Cv2.ImShow("GaussianBlur", gaussImg);
Mat medianImg = ThresholdImg.MedianBlur(5);
//Cv2.ImShow("MedianBlur", medianImg);
Mat kernel = new Mat(15, 15, MatType.CV_8UC1);
Mat DilateImg = ThresholdImg.Dilate(kernel);
Mat binary = DilateImg.Erode(kernel);
//Cv2.ImShow("Dilate & Erode", binary);
Mat element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new OpenCvSharp.Size(5, 5));
Mat openImg = ThresholdImg.MorphologyEx(MorphTypes.Open, element);
//Cv2.ImShow("Dilate & Erode", openImg);
Rect roi = new Rect(0, 0, bitmap.Width, bitmap.Height);
Mat ROIimg = new Mat(openImg, roi);
Cv2.ImShow("ROI Image", ROIimg);
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierachy;
Cv2.FindContours(ROIimg, out contours, out hierachy, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS);
//According to the found contour points, fit the ellipse
for (int i = 0; i < contours.Length; i++)
{
//The fitting function must have at least 5 points, if less than it is not fitting
if (contours[i].Length < 5) continue;
//Ellipse fitting
var rrt = Cv2.FitEllipse(contours[i]);
}
}
如何准确检测圆?对不起,我是新人,我不能发布我的图片
解决方案
推荐阅读
- tsql - MS SQL Server 解析名称以获取第一个和最后一个
- swift - 为什么 CLLocationManager 类是 MapKit 的一部分?
- html - 具有淡入下拉效果的 Angular 和 CSS 动画
- python - Pandas 替换前端重复项
- c++ - asio boost套接字连接被拒绝
- python - 我无法在 tkinter 程序中重定向
- python - 通过在 Python 中组合两个列表来生成新值
- bash - 语法错误:运行 docker exec 命令时未终止的引号字符串
- ruby - Ubuntu 20.10 上的 Ruby 3.0 安装已死
- node.js - 使用 Express Static 提供 SVG 文件