opencvsharp4.6编写c#代码 LineSegmentPoint[] lp转换成 Point[] edgePoints
时间: 2023-12-16 21:06:14 浏览: 27
可以使用 `PointCollection` 类来将 `LineSegmentPoint` 数组转换为 `Point` 数组。以下是示例代码:
```csharp
using OpenCvSharp;
using System.Collections.Generic;
using System.Linq;
// 假设 LineSegmentPoint[] lp 是已经定义并初始化的数组
// 将 LineSegmentPoint[] 转换为 Point[] 数组
List<Point> pointList = new List<Point>();
foreach (LineSegmentPoint lsp in lp)
{
pointList.Add(lsp.P1);
pointList.Add(lsp.P2);
}
Point[] edgePoints = pointList.ToArray();
```
首先,我们创建一个 `List<Point>` 类型的对象 `pointList`,然后使用 `foreach` 循环遍历 `LineSegmentPoint[] lp` 数组中的每个元素,将其包含的两个点 `P1` 和 `P2` 添加到 `pointList` 中。最后,我们使用 `ToArray` 方法将 `pointList` 转换为 `Point[]` 数组并赋值给 `edgePoints` 变量。
相关问题
基于opencvsharp4.6编写c#代码 LineIterator it 转换为 Point[] pointsOnLine
将LineIterator it转换为Point[] pointsOnLine的代码如下:
```
LineIterator it = new LineIterator(image, startPoint, endPoint);
List<Point> points = new List<Point>();
while (!it.IsEnd)
{
points.Add(it.Pos());
it.Next();
}
Point[] pointsOnLine = points.ToArray();
```
这段代码首先定义了一个LineIterator对象it,然后通过循环遍历该对象的每个像素点,并将其添加到一个List<Point>对象中。最后,将List<Point>对象转换为Point[]数组,得到了所有位于直线上的像素点的坐标值。
OpenCvSharp4.6 中Point[] approxPoints 转换成InputArray
在OpenCvSharp4.6中,可以使用`Mat.OfPointArray()`方法将`Point[]`转换为`Mat`对象,然后再将`Mat`对象转换为`InputArray`。具体实现如下:
```csharp
using OpenCvSharp;
Point[] approxPoints = {...}; // 假设已经定义好了approxPoints数组
// 将Point数组转换为Mat对象
Mat inputMat = Mat.OfPointArray(approxPoints);
// 将Mat对象转换为InputArray
InputArray inputArray = inputMat;
```
这里使用了`Mat.OfPointArray()`方法将`Point[]`数组转换为`Mat`对象。然后将`Mat`对象直接赋值给`InputArray`对象,即可完成转换。