OpenCvSharp 输入一张图,边缘阈值、投影方向 编写找边沿点 static public Mat FindEdges(Mat srcImage, double edgeThreshold, ref List<Point2f> zeroPList, int eumProjection_Direction)
时间: 2024-02-13 12:02:37 浏览: 31
好的,这是一个关于使用 OpenCvSharp 查找边缘点的问题。我会尽力回答。
首先,你需要导入以下命名空间:
```csharp
using OpenCvSharp;
using System.Collections.Generic;
```
然后,你可以使用以下代码来查找边缘点:
```csharp
static public Mat FindEdges(Mat srcImage, double edgeThreshold, ref List<Point2f> zeroPList, int eumProjection_Direction)
{
Mat grayImage = new Mat();
Cv2.CvtColor(srcImage, grayImage, ColorConversionCodes.BGR2GRAY);
Mat edges = new Mat();
Cv2.Canny(grayImage, edges, edgeThreshold, edgeThreshold * 3);
var edgePoints = new List<Point>();
for (int y = 0; y < edges.Rows; y++)
{
for (int x = 0; x < edges.Cols; x++)
{
if (edges.Get<byte>(y, x) != 0)
{
edgePoints.Add(new Point(x, y));
}
}
}
var zeroPoints = new List<Point2f>();
if (eumProjection_Direction == 0)
{
foreach (var point in edgePoints)
{
zeroPoints.Add(new Point2f(point.X, 0));
}
}
else if (eumProjection_Direction == 1)
{
foreach (var point in edgePoints)
{
zeroPoints.Add(new Point2f(0, point.Y));
}
}
zeroPList = zeroPoints;
return edges;
}
```
这个函数接受一个源图像 `srcImage`,一个边缘阈值 `edgeThreshold`,一个引用类型的空的 `List<Point2f>` 对象 `zeroPList` 和一个投影方向的枚举值 `eumProjection_Direction`。
函数首先将源图像转换为灰度图像,然后使用 `Canny` 函数查找边缘。接着,函数遍历边缘图像中的每一个像素,如果该像素的值不为零,就将其添加到一个 `List<Point>` 对象中。最后,根据投影方向将边缘点转换为 `Point2f` 类型的坐标,并将它们添加到 `zeroPoints` 列表中。
注意,这个函数返回的是边缘图像 `edges`,而不是边缘点列表。如果你需要边缘点列表,你可以使用传入的 `zeroPList` 参数获取它们。
希望我的回答能够帮助到你!