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})");
}

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

相关推荐
集成显卡7 小时前
Rust实战七 |基于带 colored 颜色文字控制台的批量文件删除工具
开发语言·后端·rust
比昨天多敲两行8 小时前
linux 线程概念与控制
java·开发语言·jvm
huaweichenai8 小时前
php 根据每个类型的抽签范围实现抽签功能
开发语言·php
codeejun9 小时前
每日一Go-73、云原生成本优化 —— 资源限制 & 指标驱动扩容
开发语言·云原生·golang
就叫_这个吧10 小时前
Java注解、元注解、自定义注解定义及应用
java·开发语言·注解
Sam_Deep_Thinking10 小时前
聊聊Java中的of
java·开发语言·架构
rockey62711 小时前
AScript之事件处理脚本
c#·.net·script·动态脚本
小小de风呀13 小时前
de风——【从零开始学C++】(十一):list的基本使用和模拟实现
开发语言·c++·list
三行数学13 小时前
Matlab之父克利夫·莫勒尔逝世
开发语言·matlab
陌路2013 小时前
C++高级进阶--夯实进阶基础(1)
开发语言·c++