C#图像处理实例1:opencvsharp获取轮廓凸包

在OpenCvSharp中,你可以使用Cv2.ApproxPolyDP函数来获取轮廓的凸包。这个函数使用Douglas-Peucker算法来近似轮廓。

以下是一个简单的例子,展示如何使用OpenCvSharp获取轮廓的凸包:

csharp 复制代码
            Mat src = Cv2.ImRead(@"保存图像\2.jpg", ImreadModes.Color);
            Mat dst = src.Clone();
            //Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);
            // 使用Canny边缘检测算法检测边缘

            //Cv2.FindContours(edges, out contours, out hierarchy, RetrievalModes.List, ContourApproximationModes.ApproxSimple);
            //Mat img = Cv2.ImRead("dft.png", ImreadModes.Grayscale);
            Mat edages = new Mat();
            Cv2.Canny(src, edages, 60, 170, 3, true);
            OpenCvSharp.Point[][] contours = null;
            HierarchyIndex[] hierarchy = null;
            Cv2.FindContours(edages, out contours, out hierarchy, RetrievalModes.List, ContourApproximationModes.ApproxNone);
            // 遍历所有轮廓
            for (int i = 0; i < contours.Length; i++)
            {
                // 计算轮廓的凸包
                Point[] convexHull = Cv2.ApproxPolyDP(contours[i], Cv2.ArcLength(contours[i], true) * 0.05, true);

                // 绘制凸包
                Cv2.DrawContours(src, new[] { convexHull }, -1, Scalar.Red, 2);
            }
            Cv2.ImShow("src", src);
            Cv2.WaitKey(0);
}
相关推荐
宝桥南山21 小时前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=01 天前
C# 中 Lambda、Linq 与 Expression
c#·linq
zhanghaha13141 天前
C# 系列教程_7 判断语句(分支)保姆级精讲
开发语言·c#
zhanghaha13141 天前
C# 系列教程_8 循环语句 保姆级精讲(面向新手)
开发语言·c#
rockey6271 天前
AScript之LINQ扩展
c#·.net·script·动态脚本
1-1=02 天前
C# 委托与事件
开发语言·c#
陈嘿萌2 天前
ECCV 2026 | CausalDrive:澳门大学×小米汽车,面向自动驾驶的实时因果世界模型
图像处理·人工智能·深度学习·计算机视觉·自动驾驶·学术研究与理论基础
1-1=02 天前
C# 特性(Attribute)
c#·.net·.netcore
楚山之石2 天前
【MRM】CAPLEX 对比剂灌注 MRI 开源术语库:ISMRM OSIPI 共识报告
图像处理·人工智能·学习·mri
原子延迟3 天前
批量压缩视频会变大?HandBrake等3款(2026)
图像处理·人工智能·计算机视觉·音视频