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);
}
相关推荐
huaqianzkh1 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
ahccqw2 天前
OpenCV 车辆数量统计
人工智能·opencv·计算机视觉
用户3721574261352 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
公子小六2 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技2 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13142 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#
架构师伯子明2 天前
ffmpeg学习4:opencv在音视频领域常用指令
opencv·学习·ffmpeg
Coder_Shenshen3 天前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
寂寞的船3 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
1-1=03 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore