OpenCvSharp4.5 编写多线程CircularCaliper找圆心函数 , 输入图像、找圆扇形区域的最小半径和最大半径、找圆扇形中心点、设定扇形区域起始角度、扇形区域结束角度 复制出扇形区域内图像作为找圆分析的图像 输入Caliper个数,宽度、长度、 将每个Caliper区域roi内从整体图像中复制成小图像来分析以提高速度,得到的点坐标转换到整体大图的坐标系中 输入从内到外找圆心、从黑到白找圆 输入canny参数 并行计Caliper区域,将Caliper区域所有canny点合并,拟合圆心 输出圆中心点、半径请编写函数

时间: 2024-02-01 21:14:38 浏览: 25
以下是一个可能的实现,其中使用了OpenCvSharp4.5的一些函数和类: ```C# using System; using System.Collections.Generic; using System.Threading.Tasks; using OpenCvSharp; public class CircularCaliper { private readonly Mat _image; private readonly int _minRadius; private readonly int _maxRadius; private readonly Point2f _center; private readonly double _startAngle; private readonly double _endAngle; private readonly int _caliperCount; private readonly int _caliperWidth; private readonly int _caliperLength; private readonly int _cannyThreshold1; private readonly int _cannyThreshold2; private readonly bool _findInnerCircle; private readonly bool _findBlackToWhite; public CircularCaliper(Mat image, int minRadius, int maxRadius, Point2f center, double startAngle, double endAngle, int caliperCount, int caliperWidth, int caliperLength, int cannyThreshold1, int cannyThreshold2, bool findInnerCircle, bool findBlackToWhite) { _image = image; _minRadius = minRadius; _maxRadius = maxRadius; _center = center; _startAngle = startAngle; _endAngle = endAngle; _caliperCount = caliperCount; _caliperWidth = caliperWidth; _caliperLength = caliperLength; _cannyThreshold1 = cannyThreshold1; _cannyThreshold2 = cannyThreshold2; _findInnerCircle = findInnerCircle; _findBlackToWhite = findBlackToWhite; } public (Point2f center, float radius) FindCircleCenter() { var caliperAngles = DistributeCaliperAngles(); var cannyPoints = new List<Point>[caliperAngles.Length]; // Parallel loop over caliper angles Parallel.For(0, caliperAngles.Length, i => { var angle = caliperAngles[i]; var roi = CreateRoi(angle); var roiImage = new Mat(_image, roi); var canny = roiImage.Canny(_cannyThreshold1, _cannyThreshold2); cannyPoints[i] = new List<Point>(); for (var j = 0; j < _caliperCount; j++) { var caliper = CreateCaliper(angle, j); var caliperImage = new Mat(canny, caliper); var points = new Mat(); Cv2.FindNonZero(caliperImage, points); foreach (var point in points.ToEnumerable()) { cannyPoints[i].Add(point + caliper.TopLeft); } } }); // Merge all canny points var mergedPoints = new List<Point>(); foreach (var points in cannyPoints) { mergedPoints.AddRange(points); } // Fit a circle to the canny points var center = new Point2f(); var radius = new float[1]; Cv2.MinEnclosingCircle(mergedPoints.ToArray(), out center, out radius); // If we're looking for the inner circle, flip the sign of the radius if (_findInnerCircle) { radius[0] *= -1; } return (center + _center, radius[0]); } private double[] DistributeCaliperAngles() { var angles = new double[_caliperCount]; for (var i = 0; i < _caliperCount; i++) { var angle = _startAngle + i * (_endAngle - _startAngle) / (_caliperCount - 1); angles[i] = angle; } return angles; } private Rect CreateRoi(double angle) { var radius = _minRadius + (_maxRadius - _minRadius) * (angle - _startAngle) / (_endAngle - _startAngle); var size = new Size(_caliperLength, (int)(2 * radius)); var point = new Point((int)(_center.X - radius + 0.5), (int)(_center.Y - radius + 0.5)); return new Rect(point, size); } private Rect CreateCaliper(double angle, int index) { var radius = _minRadius + (_maxRadius - _minRadius) * (angle - _startAngle) / (_endAngle - _startAngle); var halfWidth = _caliperWidth / 2.0; var x = (int)(_center.X + radius * Math.Cos(angle * Math.PI / 180.0) - halfWidth * Math.Sin(angle * Math.PI / 180.0) + 0.5); var y = (int)(_center.Y + radius * Math.Sin(angle * Math.PI / 180.0) + halfWidth * Math.Cos(angle * Math.PI / 180.0) + 0.5); var length = (int)(_caliperLength + 2 * halfWidth); var width = _caliperWidth; // Flip the caliper if we're looking for a black-to-white transition if (_findBlackToWhite && index % 2 == 1) { angle += 180; } var rect = new Rect(x, y, length, width); // Crop the rect to fit inside the ROI var roi = CreateRoi(angle); var intersection = Rect.Intersect(rect, roi); return new Rect(intersection.Left - x, intersection.Top - y, intersection.Width, intersection.Height); } } ``` 使用示例: ```C# var image = new Mat("input.png", ImreadModes.Grayscale); var center = new Point2f(image.Width / 2.0f, image.Height / 2.0f); var caliper = new CircularCaliper(image, 50, 150, center, 0, 360, 24, 10, 50, 50, 100, false, false); var (circleCenter, circleRadius) = caliper.FindCircleCenter(); Console.WriteLine($"Center: ({circleCenter.X}, {circleCenter.Y}), Radius: {circleRadius}"); ```

相关推荐

最新推荐

recommend-type

Java多线程之死锁的出现和解决方法

本篇文章主要介绍了Java多线程之死锁的出现和解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

浅谈PyQt5中异步刷新UI和Python多线程总结

今天小编就为大家分享一篇浅谈PyQt5中异步刷新UI和Python多线程总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例

今天小编就为大家分享一篇python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python多线程接口案例

项目为某内控公司要求并发测试,编写多线程访问接口,并生成Excel报告的脚本,记录基本步骤。 若有需要写UI自动化,接口自动化,多线程,服务器测试定时脚本等等,可联系本工具熊。 分五步操作实现50个或更多用户...
recommend-type

C#多线程处理多个队列数据的方法

主要介绍了C#多线程处理多个队列数据的方法,涉及C#线程与队列的相关操作技巧,需要的朋友可以参考下
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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