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

时间: 2023-07-31 21:10:35 浏览: 43
以下是一个使用OpenCvSharp编写的CircularCaliper找圆心函数的示例: ```csharp public static Point2f FindCircleCenter(Mat image, int minRadius, int maxRadius, double startAngle, double endAngle, int caliperCount, int caliperWidth, int caliperLength, bool searchFromInside, bool searchFromBlackToWhite) { // 剪出找圆扇形区域图像 Mat regionOfInterest = new Mat(); Cv2.InRange(image, new Scalar(0), new Scalar(0), regionOfInterest); Cv2.Ellipse(regionOfInterest, new RotatedRect(new Point2f(image.Width / 2, image.Height / 2), new Size2f(maxRadius, maxRadius), 0), new Scalar(255), -1); Cv2.Ellipse(regionOfInterest, new RotatedRect(new Point2f(image.Width / 2, image.Height / 2), new Size2f(minRadius, minRadius), 0), new Scalar(0), -1); Mat regionOfInterestMask = new Mat(); Cv2.InRange(regionOfInterest, new Scalar(1), new Scalar(255), regionOfInterestMask); Mat regionOfInterestImage = new Mat(); image.CopyTo(regionOfInterestImage, regionOfInterestMask); // 计算圆弧角度范围 double arcStartAngle = startAngle * Math.PI / 180; double arcEndAngle = endAngle * Math.PI / 180; if (arcEndAngle < arcStartAngle) { double temp = arcStartAngle; arcStartAngle = arcEndAngle; arcEndAngle = temp; } // 初始化最小距离和最优圆心 double minDistance = double.MaxValue; Point2f bestCenter = new Point2f(); // 计算每个刻度线的角度和长度 double angleStep = (arcEndAngle - arcStartAngle) / caliperCount; int lengthHalf = caliperLength / 2; // 循环遍历所有刻度线 for (int i = 0; i < caliperCount; i++) { // 计算当前刻度线的角度 double angle = arcStartAngle + angleStep * i; // 计算当前刻度线的起始和结束点 Point2f startPoint = new Point2f((float)(bestCenter.X + minRadius * Math.Cos(angle)), (float)(bestCenter.Y + minRadius * Math.Sin(angle))); Point2f endPoint = new Point2f((float)(bestCenter.X + maxRadius * Math.Cos(angle)), (float)(bestCenter.Y + maxRadius * Math.Sin(angle))); // 计算当前刻度线的垂线 Point2f perpendicular = new Point2f((float)Math.Sin(angle), (float)-Math.Cos(angle)); // 初始化当前刻度线的最小距离和最优位置 double minDistanceForCaliper = double.MaxValue; Point2f bestPositionForCaliper = new Point2f(); // 循环遍历所有位置 for (int j = searchFromInside ? maxRadius : minRadius; searchFromInside ? j >= minRadius : j <= maxRadius; j += searchFromInside ? -1 : 1) { // 计算当前位置的坐标 Point2f position = new Point2f((float)(bestCenter.X + j * Math.Cos(angle)), (float)(bestCenter.Y + j * Math.Sin(angle))); // 计算当前位置的刻度线端点 Point2f caliperStartPoint = new Point2f(position.X - perpendicular.X * lengthHalf, position.Y - perpendicular.Y * lengthHalf); Point2f caliperEndPoint = new Point2f(position.X + perpendicular.X * lengthHalf, position.Y + perpendicular.Y * lengthHalf); // 计算当前位置的灰度值 Scalar color = regionOfInterestImage.At<byte>((int)position.Y, (int)position.X); // 判断是否需要从黑到白或从白到黑查找 if ((searchFromBlackToWhite && color.Val[0] > 128) || (!searchFromBlackToWhite && color.Val[0] < 128)) { // 计算当前位置的距离 double distance = Math.Abs(caliperWidth - Cv2.PointPolygonTest(new Point2f[] { caliperStartPoint, caliperEndPoint }, new Point2f[] { startPoint, endPoint }, true)); // 更新最小距离和最优位置 if (distance < minDistanceForCaliper) { minDistanceForCaliper = distance; bestPositionForCaliper = position; } } } // 更新最小距离和最优圆心 if (minDistanceForCaliper < minDistance) { minDistance = minDistanceForCaliper; bestCenter = bestPositionForCaliper; } } // 返回最优圆心 return bestCenter; } ``` 此函数接受输入图像、找圆扇形区域的最小半径和最大半径、设定扇形区域起始角度、扇形区域结束角度、Caliper个数、宽度、长度、输入从内到外找圆心、从黑到白找圆和中心圆可设定等参数,并返回找到的圆心。

相关推荐

最新推荐

recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

MobaXterm 工具

MobaXterm 工具
recommend-type

grpcio-1.48.0-cp37-cp37m-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

扁平风格PPT可修改ppt下载(11).zip

扁平风格PPT可修改ppt下载(11).zip
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

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

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