OpenCvSharp 编写多线程CircularCaliper找圆心函数 , 输入图像、找圆弧区域的半径、圆弧中心点、设定圆弧域起始角度、圆弧区域结束角度 输入Caliper个数,宽度、长度、 将每个Caliper区域按输入圆弧与半径的交点作小矩形的中心剪出小区域图像 将每个Caliper区域roi内从整体图像中复制成小图像来分析以提高速度,得到的点坐标转换到整体大图的坐标系中 输入从内到外找圆心、从黑到白找圆 输入canny参数 并行计Caliper区域,将Caliper区域所有canny点合并,拟合圆心 输出圆中心点out double centerX, out double centerY,out double Radius请编写函数
时间: 2024-02-03 15:12:01 浏览: 72
关于C语言多线程pthread库的相关函数说明
以下是使用OpenCvSharp编写的多线程CircularCaliper找圆心函数的示例代码:
```csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenCvSharp;
public class CircularCaliperFinder
{
private readonly Mat _image;
private readonly int _radius;
private readonly Point _center;
private readonly double _startAngle;
private readonly double _endAngle;
private readonly int _caliperCount;
private readonly int _caliperWidth;
private readonly int _caliperLength;
private readonly bool _innerToOuter;
private readonly int _cannyThreshold1;
private readonly int _cannyThreshold2;
public CircularCaliperFinder(Mat image, int radius, Point center, double startAngle, double endAngle, int caliperCount, int caliperWidth, int caliperLength, bool innerToOuter, int cannyThreshold1, int cannyThreshold2)
{
_image = image;
_radius = radius;
_center = center;
_startAngle = startAngle;
_endAngle = endAngle;
_caliperCount = caliperCount;
_caliperWidth = caliperWidth;
_caliperLength = caliperLength;
_innerToOuter = innerToOuter;
_cannyThreshold1 = cannyThreshold1;
_cannyThreshold2 = cannyThreshold2;
}
public void Find(out double centerX, out double centerY, out double radius)
{
int width = _image.Width;
int height = _image.Height;
List<CaliperRegion> regions = new List<CaliperRegion>();
double angleStep = (_endAngle - _startAngle) / _caliperCount;
double angle = _innerToOuter ? _startAngle : _endAngle;
for (int i = 0; i < _caliperCount; i++)
{
Point[] points = new Point[4];
double x = _center.X + _radius * Math.Cos(angle);
double y = _center.Y + _radius * Math.Sin(angle);
Point intersection1 = new Point((int)Math.Round(x), (int)Math.Round(y));
x = _center.X + (_radius + _caliperLength) * Math.Cos(angle);
y = _center.Y + (_radius + _caliperLength) * Math.Sin(angle);
Point intersection2 = new Point((int)Math.Round(x), (int)Math.Round(y));
double angle1 = angle - _caliperWidth / 2;
double angle2 = angle + _caliperWidth / 2;
x = _center.X + _radius * Math.Cos(angle1);
y = _center.Y + _radius * Math.Sin(angle1);
points[0] = new Point((int)Math.Round(x), (int)Math.Round(y));
x = _center.X + (_radius + _caliperLength) * Math.Cos(angle1);
y = _center.Y + (_radius + _caliperLength) * Math.Sin(angle1);
points[1] = new Point((int)Math.Round(x), (int)Math.Round(y));
x = _center.X + _radius * Math.Cos(angle2);
y = _center.Y + _radius * Math.Sin(angle2);
points[2] = new Point((int)Math.Round(x), (int)Math.Round(y));
x = _center.X + (_radius + _caliperLength) * Math.Cos(angle2);
y = _center.Y + (_radius + _caliperLength) * Math.Sin(angle2);
points[3] = new Point((int)Math.Round(x), (int)Math.Round(y));
Rect roi = new Rect(intersection1, intersection2);
if (roi.X < 0 || roi.Y < 0 || roi.Right > width || roi.Bottom > height)
{
continue;
}
Mat region = new Mat(_image, roi);
CaliperRegion caliperRegion = new CaliperRegion(region, points);
regions.Add(caliperRegion);
angle += angleStep;
}
ConcurrentBag<Point> cannyPoints = new ConcurrentBag<Point>();
Parallel.ForEach(regions, region =>
{
Mat canny = new Mat();
Cv2.Canny(region.Region, canny, _cannyThreshold1, _cannyThreshold2);
Point[] points = region.Points;
foreach (Point point in points)
{
point.X -= region.Roi.X;
point.Y -= region.Roi.Y;
}
Point[] edgePoints = new Point[canny.Rows * canny.Cols];
int count = 0;
for (int y = 0; y < canny.Rows; y++)
{
for (int x = 0; x < canny.Cols; x++)
{
if (canny.Get<byte>(y, x) != 0)
{
edgePoints[count++] = new Point(x, y);
}
}
}
for (int i = 0; i < count; i++)
{
Point edgePoint = edgePoints[i];
edgePoint.X += region.Roi.X;
edgePoint.Y += region.Roi.Y;
if (IsInsideArc(edgePoint, _center, _radius, _startAngle, _endAngle))
{
cannyPoints.Add(edgePoint);
}
}
});
Point[] pointsArray = new Point[cannyPoints.Count];
cannyPoints.CopyTo(pointsArray, 0);
if (pointsArray.Length < 5)
{
centerX = _center.X;
centerY = _center.Y;
radius = _radius;
return;
}
Cv2.MinEnclosingCircle(pointsArray, out Point2f center, out float r);
centerX = center.X;
centerY = center.Y;
radius = r;
}
private static bool IsInsideArc(Point point, Point center, int radius, double startAngle, double endAngle)
{
double dx = point.X - center.X;
double dy = point.Y - center.Y;
double angle = Math.Atan2(dy, dx);
if (angle < 0)
{
angle += Math.PI * 2;
}
double distance = Math.Sqrt(dx * dx + dy * dy);
return distance >= radius && angle >= startAngle && angle <= endAngle;
}
private class CaliperRegion
{
public Mat Region { get; }
public Point[] Points { get; }
public Rect Roi { get; }
public CaliperRegion(Mat region, Point[] points)
{
Region = region;
Points = points;
Roi = new Rect(points[0], points[2]);
}
}
}
```
使用示例:
```csharp
Mat image = Cv2.ImRead("image.png", ImreadModes.GrayScale);
Point center = new Point(image.Width / 2, image.Height / 2);
CircularCaliperFinder finder = new CircularCaliperFinder(image, 100, center, 0, Math.PI * 2, 36, 10, 20, true, 100, 200);
finder.Find(out double centerX, out double centerY, out double radius);
```
这个示例程序中,我们首先读入一个灰度图像,然后将图像中心设置为圆心,使用CircularCaliperFinder类来查找圆心。在创建CircularCaliperFinder对象时,我们需要提供以下参数:
- image:输入图像。
- radius:找圆弧区域的半径。
- center:圆弧中心点。
- startAngle:设定圆弧域起始角度。
- endAngle:圆弧区域结束角度。
- caliperCount:输入Caliper个数。
- caliperWidth:Caliper宽度。
- caliperLength:Caliper长度。
- innerToOuter:是否从内到外找圆心。
- cannyThreshold1:Canny算子参数1。
- cannyThreshold2:Canny算子参数2。
然后,我们调用Find方法来查找圆心,并将结果保存在out参数中。在Find方法中,我们首先计算每个Caliper的位置和方向,并将其封装在CaliperRegion类中。然后,我们使用并行计算来处理每个Caliper区域,将其转换为二值图像,并使用Canny算子来查找边缘点。接下来,我们将边缘点转换为整体图像坐标系,并将其添加到一个ConcurrentBag中。最后,我们使用MinEnclosingCircle函数来拟合圆心。如果找不到足够的边缘点,则返回输入的圆心和半径。
阅读全文