List<Point2f>转InputArray
时间: 2023-08-10 08:03:42 浏览: 63
要将一个 `List<Point2f>` 转换为 `InputArray`,可以使用以下代码:
```java
List<Point2f> points = new ArrayList<Point2f>();
// 添加一些点到列表中
MatOfPoint2f matOfPoints = new MatOfPoint2f();
matOfPoints.fromList(points);
InputArray inputArray = matOfPoints; // 自动类型转换
```
在上面的代码中,我们首先创建了一个 `List<Point2f>`,并向其中添加了一些点。然后,我们使用 `MatOfPoint2f.fromList()` 方法将这些点转换为一个 `MatOfPoint2f` 对象。最后,我们将 `matOfPoints` 对象赋值给一个 `InputArray` 对象,这样就完成了从 `List<Point2f>` 到 `InputArray` 的转换。
相关问题
List<Point2f>转InputArray opencvsharp没有MatOfPoint2f方法
如果你使用的是 OpenCVSharp 库,可以使用以下代码将一个 `List<Point2f>` 转换为 `InputArray`:
```csharp
using OpenCvSharp;
// 创建一个 List<Point2f>
List<Point2f> points = new List<Point2f>();
// 添加一些点到列表中
// 将 List<Point2f> 转换为 Point2f[]
Point2f[] pointsArray = points.ToArray();
// 创建一个 Mat 对象
Mat mat = new Mat(pointsArray.Length, 1, MatType.CV_32FC2);
// 将 Point2f[] 复制到 Mat 中
mat.SetArray(0, 0, pointsArray);
// 将 Mat 转换为 InputArray
InputArray inputArray = mat;
```
在上面的代码中,我们首先创建了一个 `List<Point2f>`,并向其中添加了一些点。然后,我们将 `List<Point2f>` 转换为 `Point2f[]` 数组,并创建一个 `Mat` 对象,该对象的行数为点的数量,列数为1,类型为 `CV_32FC2`,即32位浮点型的2通道矩阵。接下来,我们使用 `mat.SetArray()` 方法将 `Point2f[]` 复制到 `Mat` 中。最后,我们将 `Mat` 对象转换为 `InputArray`,这样就完成了从 `List<Point2f>` 到 `InputArray` 的转换。
OpenCvSharp4.6 (List<Point2f> points,转换成 InputArray
同样地,要将 List<Point2f> 转换成 InputArray,可以使用 MatOfPoint2f 类。具体的实现代码如下:
```csharp
List<Point2f> points = new List<Point2f>();
// 假设 points 中已经有一些数据
MatOfPoint2f mat = new MatOfPoint2f();
mat.FromList(points);
InputArray inputArray = mat;
```
但需要注意的是,OpenCvSharp4.6 中的 MatOfPoint2f 类已经不再提供 FromList 方法,所以需要使用其构造函数来初始化 MatOfPoint2f 对象。具体实现代码如下:
```csharp
List<Point2f> points = new List<Point2f>();
// 假设 points 中已经有一些数据
MatOfPoint2f mat = new MatOfPoint2f(points.ToArray());
InputArray inputArray = mat;
```
这样就可以将 List<Point2f> 转换成 InputArray 了。