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

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

相关推荐
我是唐青枫1 天前
C#.NET 索引器完全解析:语法、场景与最佳实践
c#·.net
代码or搬砖1 天前
String字符串
android·java·开发语言
leo__5201 天前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
Macbethad1 天前
自动化测试技术报告
开发语言·lua
不会画画的画师1 天前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_928411561 天前
12.22 wxml语法
开发语言·前端·javascript
5980354151 天前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL1 天前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者1 天前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言
froginwe111 天前
jQuery UI 实例
开发语言