OpenCvSharp 编写CircularCaliper找圆心函数 , 输入图像、找圆扇形区域的最小半径和最大半径、设定扇形区域起始角度、扇形区域结束角度 先剪出找圆扇形区域图像 输入Caliper个数,宽度、长度、 输入从内到外找圆心、从黑到白找圆 并行计算遍历所有刻度线 中心圆可设定请编写函数

时间: 2023-07-31 10:10:11 浏览: 54
以下是一个示例函数,可以用 OpenCvSharp 实现 CircularCaliper 找圆心的功能: ```csharp using System; using OpenCvSharp; public static class CircularCaliper { public static Point2f FindCircleCenter(Mat image, float minRadius, float maxRadius, float startAngle, float endAngle, int numCalipers, int caliperWidth, int caliperLength, bool fromInside = true, bool darkToLight = true) { // 剪出扇形区域图像 Point2f center = new Point2f(image.Width / 2f, image.Height / 2f); float radius = (minRadius + maxRadius) / 2f; float startRadians = startAngle * (float)Math.PI / 180f; float endRadians = endAngle * (float)Math.PI / 180f; float halfAngle = (endRadians - startRadians) / 2f; float startRadians2 = startRadians - halfAngle; float endRadians2 = endRadians - halfAngle; Point2f pt1 = new Point2f((float)Math.Cos(startRadians2), (float)Math.Sin(startRadians2)) * radius + center; Point2f pt2 = new Point2f((float)Math.Cos(endRadians2), (float)Math.Sin(endRadians2)) * radius + center; Point2f pt3 = new Point2f((float)Math.Cos(endRadians2), (float)Math.Sin(endRadians2)) * maxRadius + center; Point2f pt4 = new Point2f((float)Math.Cos(startRadians2), (float)Math.Sin(startRadians2)) * maxRadius + center; using (Mat roiMask = new Mat(image.Size(), MatType.CV_8UC1, Scalar.Black)) { Point[] roiPoints = new Point[] { pt1, pt2, pt3, pt4 }.ToRectPolygon(); roiMask.FillPoly(new Point[][] { roiPoints }, Scalar.White); Mat roiImage = new Mat(image.Size(), image.Type(), Scalar.Black); image.CopyTo(roiImage, roiMask); image = roiImage; } // 计算刻度线位置 float stepAngle = (endAngle - startAngle) / (numCalipers - 1); float startAngle2 = startAngle - halfAngle; float endAngle2 = endAngle - halfAngle; Point2f[] caliperPoints = new Point2f[numCalipers * 2]; for (int i = 0; i < numCalipers; i++) { float angle = startAngle2 + stepAngle * i; Point2f pt = new Point2f((float)Math.Cos(angle), (float)Math.Sin(angle)) * radius + center; Point2f pt2 = pt + new Point2f((float)Math.Cos(angle + (float)Math.PI / 2f), (float)Math.Sin(angle + (float)Math.PI / 2f)) * caliperWidth; Point2f pt3 = pt + new Point2f((float)Math.Cos(angle), (float)Math.Sin(angle)) * caliperLength * 2f; caliperPoints[i * 2] = pt2; caliperPoints[i * 2 + 1] = pt3; } // 遍历刻度线计算得分 float bestScore = float.MaxValue; Point2f bestCenter = new Point2f(-1f, -1f); for (float x = 0f; x < image.Width; x += 1f) { for (float y = 0f; y < image.Height; y += 1f) { Point2f pt = new Point2f(x, y); float score = CalculateScore(image, pt, caliperPoints, fromInside, darkToLight); if (score < bestScore) { bestScore = score; bestCenter = pt; } } } return bestCenter; } private static float CalculateScore(Mat image, Point2f center, Point2f[] caliperPoints, bool fromInside, bool darkToLight) { float score = 0f; for (int i = 0; i < caliperPoints.Length / 2; i++) { Point2f pt1 = caliperPoints[i * 2]; Point2f pt2 = caliperPoints[i * 2 + 1]; float distance = (pt1 - pt2).Length(); Point2f direction = (pt1 - pt2).Normalized(); Point2f pt3 = center + direction * distance * (fromInside ? -1f : 1f); LineSegmentPoint line = new LineSegmentPoint(pt1.ToPoint(), pt2.ToPoint()); float value1 = GetValue(image, pt1, pt2, darkToLight); float value2 = GetValue(image, pt3, pt3 + direction, darkToLight); float diff = Math.Abs(value1 - value2); float weight = distance; score += diff * weight; } return score; } private static float GetValue(Mat image, Point2f pt1, Point2f pt2, bool darkToLight) { float value = 0f; LineSegmentPoint line = new LineSegmentPoint(pt1.ToPoint(), pt2.ToPoint()); foreach (Point pt in line.GetPoints()) { if (pt.X >= 0 && pt.X < image.Width && pt.Y >= 0 && pt.Y < image.Height) { value += image.At<byte>(pt.Y, pt.X); } } value /= line.Length; return darkToLight ? (255f - value) : value; } } ``` 这个函数的输入参数包括: - image:输入图像。 - minRadius:找圆的最小半径。 - maxRadius:找圆的最大半径。 - startAngle:扇形区域的起始角度。 - endAngle:扇形区域的结束角度。 - numCalipers:刻度线数量。 - caliperWidth:刻度线宽度。 - caliperLength:刻度线长度。 - fromInside:是否从内到外找圆心。 - darkToLight:是否从黑到白找圆心。 输出为找到的圆心的坐标。

相关推荐

最新推荐

recommend-type

基于SpringBoot框架仿stackOverflow网站后台开发.zip

基于springboot的java毕业&课程设计
recommend-type

基于SpringBoot洗衣店管理系统.zip

基于springboot的java毕业&课程设计
recommend-type

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip

【优化覆盖】算术算法求解传感器覆盖优化问题【含Matlab源码 2436期】.zip
recommend-type

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip

【优化覆盖】蜣螂算法DBO求解无线传感器WSN覆盖优化问题【含Matlab源码 3567期】.zip
recommend-type

FusionCompute修改VRM节点IP地址

FusionCompute修改VRM节点IP地址 该任务指导工程师对VRM节点的IP地址、主机的管理IP地址进行修改。 执行该任务时应注意: • 建议同时修改VRM和主机的管理IP。如果修改了VRM的IP,会导致本地PC与VRM的连接短暂中断。 • 修改前应已完成网络规划,并在FusionCompute中确认VRM节点运行正常,所有主机运行正常(无处于异常或维护状态的主机)。 • 如果跨网段修改IP地址时,则应注意在完成所有节点IP地址的修改后,在相应的汇聚交换机进行配置,保证修改后的主机IP地址、VRM节点及本地PC之间能进行正常通信。相关交换机配置命令,请参考交换机配置样例。 • 如果跨网段修改管理IP地址,同时涉及修改管理VLAN,请先修改管理平面VLAN,待修改完成,且各节点与VRM网络通信正常后,再进行修改VRM IP地址和主机IP地址的操作。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。