OpenCvSharp 编写多线程CircularCaliper找圆心函数 , 输入图像、找圆扇形区域的最小半径和最大半径、找圆扇形中心点、设定扇形区域起始角度、扇形区域结束角度 找圆扇形区域图像以外的图像Mask处理 输入Caliper个数,宽度、长度、 输入从内到外找圆心、从黑到白找圆 输入canny参数 并行计Caliper区域,将Caliper区域所有canny点合并,拟合圆心 输出圆中心点、半径请编写函数
时间: 2023-07-10 07:07:26 浏览: 52
以下是一个使用OpenCvSharp编写的多线程CircularCaliper找圆心函数的示例代码。这个函数的输入参数包括输入图像、找圆扇形区域的最小半径和最大半径、找圆扇形中心点、设定扇形区域起始角度、扇形区域结束角度、找圆扇形区域图像以外的图像Mask处理、输入Caliper个数、宽度、长度、从内到外或从黑到白找圆、canny参数等。
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace MultiThreadedCircularCaliper
{
class Program
{
static void Main(string[] args)
{
// 读取图像
Mat image = Cv2.ImRead("test.jpg");
// 转换为灰度图像
Mat gray = new Mat();
Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
// 定义最小半径和最大半径
int minRadius = 50;
int maxRadius = 100;
// 定义圆扇形区域中心点、起始角度和结束角度
Point2f center = new Point2f(image.Width / 2, image.Height / 2);
float startAngle = 0;
float endAngle = 360;
// 定义圆扇形区域图像以外的图像Mask处理
Mat mask = new Mat(image.Size(), MatType.CV_8UC1, Scalar.Black);
Cv2.Circle(mask, center, maxRadius + 10, Scalar.White, -1);
// 定义Caliper个数、宽度、长度等
int numCalipers = 360;
int caliperWidth = 10;
int caliperLength = 30;
// 定义从内到外或从黑到白找圆
CaliperDirection caliperDirection = CaliperDirection.OuterToInner;
// 定义canny参数
double cannyThreshold1 = 50;
double cannyThreshold2 = 150;
// 多线程计算Caliper区域
List<CaliperResult> results = ParallelEnumerable.Range(0, numCalipers).AsParallel().Select(i =>
{
// 计算当前Caliper的角度
float angle = (float)(i * (360.0 / numCalipers));
// 创建一个Caliper对象
Caliper caliper = new Caliper(new Point2f(center.X + (float)(maxRadius * Math.Cos(angle * Math.PI / 180)), center.Y + (float)(maxRadius * Math.Sin(angle * Math.PI / 180))),
new Point2f(center.X + (float)(minRadius * Math.Cos(angle * Math.PI / 180)), center.Y + (float)(minRadius * Math.Sin(angle * Math.PI / 180))),
caliperWidth, caliperLength);
// 计算该Caliper区域的所有canny点
Point2f[] cannyPoints = caliper.GetCannyPoints(gray, cannyThreshold1, cannyThreshold2, caliperDirection);
// 拟合圆心
CircleResult circleResult = CircleFitting.FitCircle(cannyPoints);
// 返回结果
return new CaliperResult(i, angle, caliper, circleResult);
}).ToList();
// 在图像上绘制拟合结果
foreach (CaliperResult result in results)
{
if (result.CircleResult != null)
{
Cv2.Circle(image, result.CircleResult.Center, (int)result.CircleResult.Radius, Scalar.Green, 2);
}
}
// 显示图像
Cv2.ImShow("Result", image);
Cv2.WaitKey(0);
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)