c# opencv 获取多边形中心点

在C#中使用OpenCV获取多边形的中心点,可以按照以下步骤进行:

首先,你需要找到图像中的轮廓。这可以通过FindContours方法实现:

cs 复制代码
using OpenCvSharp;

Mat src = new Mat("your_image_path", ImreadModes.Grayscale);
Mat dst = new Mat();
Cv2.Threshold(src, dst, 0, 255, ThresholdTypes.BinaryInv);

VectorOfVectorPoint contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(dst, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

对于每个找到的轮廓,你可以使用MinAreaRect2方法来找到最小外接矩形,该矩形的中心点通常被用作多边形的中心点:

cs 复制代码
foreach (VectorPoint contour in contours)
{
    RotatedRect rect = Cv2.MinAreaRect(contour);
    Point2f center = rect.Center;
    
    // center now contains the coordinates of the center point of the polygon
    Console.WriteLine($"Center: ({center.X}, {center.Y})");
}

以上代码首先加载一张图像并进行二值化处理,然后找出图像中的所有轮廓。对于每个轮廓,它计算最小外接矩形,并获取该矩形的中心点作为多边形的中心点。请注意,这种方法假设多边形是凸的,对于凹多边形,结果可能不是预期的几何中心。如果你需要处理凹多边形,可能需要使用更复杂的算法来计算其质心或重心。

相关推荐
廋到被风吹走19 分钟前
【Java】Exception 异常体系解析 从原理到实践
java·开发语言
Pyeako38 分钟前
python网络爬虫
开发语言·爬虫·python·requsets库
diegoXie38 分钟前
【Python】 中的 * 与 **:Packing 与 Unpacking
开发语言·windows·python
齐齐大魔王43 分钟前
OpenCV
人工智能·opencv·计算机视觉
武藤一雄1 小时前
C# 语法糖详解
后端·microsoft·c#·.net
qq_479875431 小时前
C++ 鸭子类型” (Duck Typing)
开发语言·c++
武藤一雄2 小时前
C#:进程/线程/多线程/AppDomain详解
后端·微软·c#·asp.net·.net·wpf·.netcore
勇气要爆发2 小时前
【第一阶段—基础准备】第五章:Python模块和包管理(基础篇)—变形金刚的装备库
开发语言·python
lkbhua莱克瓦242 小时前
Java进阶——IO流
java·开发语言·笔记·学习方法·io流
阿杰同学2 小时前
Java中55种锁,高级面试题,最新面试题
java·开发语言