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

时间: 2024-02-01 20:14:23 浏览: 29
以下是使用OpenCvSharp编写的多线程CircularCaliper找圆心函数的示例代码: ```csharp public static void FindCircleCenter(Mat image, int minRadius, int maxRadius, out Point center, out int radius, Point centerGuess = default(Point), double startAngle = 0, double endAngle = 360, int cannyThreshold1 = 100, int cannyThreshold2 = 200, int numCalipers = 20, int caliperWidth = 10, int caliperLength = 100, bool fromInside = true) { // Convert the image to grayscale Mat grayImage = new Mat(); Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY); // Apply canny edge detection to the gray image Mat edges = new Mat(); Cv2.Canny(grayImage, edges, cannyThreshold1, cannyThreshold2); // Create a mask for the circular sector Mat mask = new Mat(image.Size(), MatType.CV_8UC1, Scalar.All(0)); Cv2.Circle(mask, centerGuess != default(Point) ? centerGuess : new Point(image.Width / 2, image.Height / 2), (minRadius + maxRadius) / 2, Scalar.All(255), -1); Mat sectorMask = new Mat(image.Size(), MatType.CV_8UC1, Scalar.All(0)); Cv2.Ellipse(sectorMask, centerGuess != default(Point) ? centerGuess : new Point(image.Width / 2, image.Height / 2), new Size((maxRadius - minRadius) / 2, (maxRadius - minRadius) / 2), 0, startAngle, endAngle, Scalar.All(255), -1); mask &= sectorMask; // Find the contour of the mask Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(mask, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone); // Split the contour into segments List<Point[]> segments = new List<Point[]>(); int numSegments = (int)Math.Round((endAngle - startAngle) / 360 * contours[0].Length); for (int i = 0; i < numSegments; i++) { int startIndex = (int)Math.Round((double)i / numSegments * contours[0].Length); int endIndex = (int)Math.Round((double)(i + 1) / numSegments * contours[0].Length); if (endIndex == startIndex) { endIndex++; } if (endIndex > contours[0].Length) { endIndex -= contours[0].Length; } if (startIndex < endIndex) { segments.Add(contours[0].Skip(startIndex).Take(endIndex - startIndex).ToArray()); } else { segments.Add(contours[0].Skip(startIndex).Concat(contours[0].Take(endIndex)).ToArray()); } } // Create a list of caliper regions List<Rect[]> caliperRegions = new List<Rect[]>(); for (int i = 0; i < segments.Count; i++) { Point[] segment = segments[i]; Point[] nextSegment = segments[(i + 1) % segments.Count]; Point centerPoint = new Point(segment.Sum(p => p.X) / segment.Length, segment.Sum(p => p.Y) / segment.Length); double angle = Math.Atan2(nextSegment[0].Y - segment[segment.Length - 1].Y, nextSegment[0].X - segment[segment.Length - 1].X); double normalAngle = angle + Math.PI / 2; Point[] caliperPoints = new Point[numCalipers * 2]; for (int j = 0; j < numCalipers; j++) { double caliperAngle = normalAngle + j * 2 * Math.PI / numCalipers; Point caliperStart = new Point(centerPoint.X + (int)Math.Round(caliperWidth / 2 * Math.Cos(caliperAngle)), centerPoint.Y + (int)Math.Round(caliperWidth / 2 * Math.Sin(caliperAngle))); Point caliperEnd = new Point(centerPoint.X - (int)Math.Round(caliperWidth / 2 * Math.Cos(caliperAngle)), centerPoint.Y - (int)Math.Round(caliperWidth / 2 * Math.Sin(caliperAngle))); caliperPoints[j * 2] = caliperStart; caliperPoints[j * 2 + 1] = caliperEnd; } Point[] hull = Cv2.ConvexHull(caliperPoints); Rect boundingRect = Cv2.BoundingRect(hull); Rect[] calipers = new Rect[numCalipers]; int caliperOffset = (int)Math.Round(caliperLength / 2 * (fromInside ? -1 : 1)); for (int j = 0; j < numCalipers; j++) { double caliperAngle = normalAngle + j * 2 * Math.PI / numCalipers; Point caliperStart = new Point(centerPoint.X + (int)Math.Round(caliperWidth / 2 * Math.Cos(caliperAngle)), centerPoint.Y + (int)Math.Round(caliperWidth / 2 * Math.Sin(caliperAngle))); Point caliperEnd = new Point(centerPoint.X - (int)Math.Round(caliperWidth / 2 * Math.Cos(caliperAngle)), centerPoint.Y - (int)Math.Round(caliperWidth / 2 * Math.Sin(caliperAngle))); Point caliperOffsetStart = new Point(caliperStart.X + (int)Math.Round(caliperOffset * Math.Cos(caliperAngle)), caliperStart.Y + (int)Math.Round(caliperOffset * Math.Sin(caliperAngle))); Point caliperOffsetEnd = new Point(caliperEnd.X + (int)Math.Round(caliperOffset * Math.Cos(caliperAngle)), caliperEnd.Y + (int)Math.Round(caliperOffset * Math.Sin(caliperAngle))); calipers[j] = new Rect(caliperOffsetStart.X, caliperOffsetStart.Y, caliperOffsetEnd.X - caliperOffsetStart.X, caliperOffsetEnd.Y - caliperOffsetStart.Y); } caliperRegions.Add(calipers); } // Analyze each caliper region in parallel ConcurrentBag<Point>[] cannyPoints = new ConcurrentBag<Point>[caliperRegions.Sum(r => r.Length)]; Parallel.For(0, caliperRegions.Count, i => { Rect[] calipers = caliperRegions[i]; Point[] segment = segments[i]; Mat regionImage = edges.SubMat(new Rect(boundingRect.X + segment.Min(p => p.X) - caliperLength / 2, boundingRect.Y + segment.Min(p => p.Y) - caliperLength / 2, boundingRect.Width + caliperLength, boundingRect.Height + caliperLength)); for (int j = 0; j < calipers.Length; j++) { Rect caliper = calipers[j]; Mat caliperRegion = regionImage.SubMat(caliper); Point[] caliperPoints = Cv2.FindNonZero(caliperRegion).Select(p => new Point(p.X + caliper.X, p.Y + caliper.Y)).ToArray(); cannyPoints[i * numCalipers + j] = new ConcurrentBag<Point>(caliperPoints); } }); // Combine all canny points List<Point> allCannyPoints = new List<Point>(); foreach (ConcurrentBag<Point> bag in cannyPoints) { allCannyPoints.AddRange(bag); } // Fit a circle to the canny points Cv2.FitCircle(allCannyPoints.ToArray(), out center, out radius); // Convert the center point to the original image coordinates center.X += boundingRect.X - (image.Width - grayImage.Width) / 2; center.Y += boundingRect.Y - (image.Height - grayImage.Height) / 2; } ``` 可以使用以下代码调用上述函数: ```csharp Point center; int radius; FindCircleCenter(image, minRadius, maxRadius, out center, out radius, centerGuess, startAngle, endAngle, cannyThreshold1, cannyThreshold2, numCalipers, caliperWidth, caliperLength, fromInside); ```

相关推荐

最新推荐

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

Java多线程之多线程异常捕捉

在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己把自己的checked exception处理掉,通过此篇文章给大家分享Java多线程之多线程异常捕捉,需要的朋友可以参考下
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柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

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