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

时间: 2023-07-10 17:07:51 浏览: 55
以下是基于OpenCvSharp编写的多线程CircularCaliper找圆心函数的代码: ``` using System; using System.Collections.Generic; using System.Threading.Tasks; using OpenCvSharp; public static class CircularCaliperFinder { public static (Point2d center, double radius) FindCircleCenter(Mat image, int minRadius, int maxRadius, Point centerPoint, double startAngle, double endAngle, int caliperCount, int caliperWidth, int caliperLength, bool findFromInside, bool findFromBlackToWhite, double cannyThreshold1, double cannyThreshold2) { // 剪出找圆扇形区域图像 Rect roi = new Rect(centerPoint.X - maxRadius, centerPoint.Y - maxRadius, maxRadius * 2, maxRadius * 2); Mat roiImage = new Mat(image, roi); // 计算每个Caliper的角度 double caliperAngle = (endAngle - startAngle) / caliperCount; // 并行计算每个Caliper的结果 List<Task<Circle>> tasks = new List<Task<Circle>>(); for (int i = 0; i < caliperCount; i++) { double angle = startAngle + caliperAngle * i; tasks.Add(Task.Run(() => FindCircleCenterInCaliper(roiImage, minRadius, maxRadius, centerPoint, angle, caliperWidth, caliperLength, findFromInside, findFromBlackToWhite, cannyThreshold1, cannyThreshold2))); } Task.WaitAll(tasks.ToArray()); // 合并所有Caliper的结果 List<Point> points = new List<Point>(); foreach (Task<Circle> task in tasks) { if (task.Result != null) { points.AddRange(task.Result.Points); } } // 拟合圆心 if (points.Count >= 3) { Point2f[] pointArray = points.ToArray(); Point2f center; float radius; Cv2.MinEnclosingCircle(pointArray, out center, out radius); return (new Point2d(center.X + roi.X, center.Y + roi.Y), radius); } else { return (new Point2d(-1, -1), -1); } } private static Circle FindCircleCenterInCaliper(Mat image, int minRadius, int maxRadius, Point centerPoint, double angle, int caliperWidth, int caliperLength, bool findFromInside, bool findFromBlackToWhite, double cannyThreshold1, double cannyThreshold2) { double x = centerPoint.X + Math.Cos(angle * Math.PI / 180) * minRadius; double y = centerPoint.Y + Math.Sin(angle * Math.PI / 180) * minRadius; Point startPoint = new Point((int)x, (int)y); x = centerPoint.X + Math.Cos(angle * Math.PI / 180) * maxRadius; y = centerPoint.Y + Math.Sin(angle * Math.PI / 180) * maxRadius; Point endPoint = new Point((int)x, (int)y); // 计算Caliper在图像上的矩形区域 Point2f[] rectPoints = new Point2f[4]; double halfWidth = caliperWidth / 2.0; double halfLength = caliperLength / 2.0; rectPoints[0] = new Point2f((float)(startPoint.X + halfWidth * Math.Sin(angle * Math.PI / 180)), (float)(startPoint.Y - halfWidth * Math.Cos(angle * Math.PI / 180))); rectPoints[1] = new Point2f((float)(startPoint.X - halfWidth * Math.Sin(angle * Math.PI / 180)), (float)(startPoint.Y + halfWidth * Math.Cos(angle * Math.PI / 180))); rectPoints[2] = new Point2f((float)(endPoint.X - halfWidth * Math.Sin(angle * Math.PI / 180)), (float)(endPoint.Y + halfWidth * Math.Cos(angle * Math.PI / 180))); rectPoints[3] = new Point2f((float)(endPoint.X + halfWidth * Math.Sin(angle * Math.PI / 180)), (float)(endPoint.Y - halfWidth * Math.Cos(angle * Math.PI / 180))); RotatedRect rect = Cv2.MinAreaRect(rectPoints); // 获取Caliper的像素值 Mat caliperImage = new Mat(image, rect.BoundingRect()); Mat grayImage = new Mat(); Cv2.CvtColor(caliperImage, grayImage, ColorConversionCodes.BGR2GRAY); Mat binaryImage = new Mat(); if (findFromBlackToWhite) { Cv2.Threshold(grayImage, binaryImage, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); } else { Cv2.Threshold(grayImage, binaryImage, 255, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu); } // 计算Canny边缘 Mat cannyImage = new Mat(); Cv2.Canny(binaryImage, cannyImage, cannyThreshold1, cannyThreshold2); // 获取所有的Canny点 List<Point> points = new List<Point>(); for (int i = 0; i < cannyImage.Rows; i++) { for (int j = 0; j < cannyImage.Cols; j++) { if (cannyImage.At<byte>(i, j) == 255) { Point point = new Point(j, i); if (rect.BoundingRect().Contains(point)) { points.Add(point); } } } } // 从所有的Canny点中寻找圆心 if (points.Count >= 3) { Point2f[] pointArray = points.ToArray(); Point2f center; float radius; Cv2.MinEnclosingCircle(pointArray, out center, out radius); return new Circle(center, radius, points); } else { return null; } } } public class Circle { public Point2f Center { get; set; } public float Radius { get; set; } public List<Point> Points { get; set; } public Circle(Point2f center, float radius, List<Point> points) { Center = center; Radius = radius; Points = points; } } ``` 这个函数的参数比较多,需要具体解释一下: * `image`:输入图像。 * `minRadius`:找圆扇形区域的最小半径。 * `maxRadius`:找圆扇形区域的最大半径。 * `centerPoint`:找圆扇形中心点。 * `startAngle`:设定扇形区域起始角度。 * `endAngle`:扇形区域结束角度。 * `caliperCount`:输入Caliper个数。 * `caliperWidth`:每个Caliper的宽度。 * `caliperLength`:每个Caliper的长度。 * `findFromInside`:是否从内到外找圆心。 * `findFromBlackToWhite`:是否从黑到白找圆。 * `cannyThreshold1`:Canny边缘检测参数1。 * `cannyThreshold2`:Canny边缘检测参数2。 这个函数通过并行计算每个Caliper的结果,然后将所有的Canny点合并,最后拟合圆心。返回的结果是圆中心点和半径。如果找不到合适的圆心,则返回(-1, -1)和-1。

相关推荐

最新推荐

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

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

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