OpenCvSharp4.6 中Point[] approxPoints 转换成InputArray
时间: 2023-12-16 07:04:01 浏览: 33
在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`对象,即可完成转换。
相关问题
OpenCvSharp4.6 中Point[] approxPoints 转换成InputArray,没有Mat.OfPointArray(approxPoints);的方法
如果没有`Mat.OfPointArray()`方法,可以使用以下代码将`Point[]`转换为`InputArray`:
```csharp
using OpenCvSharp;
Point[] approxPoints = {...}; // 假设已经定义好了approxPoints数组
// 创建Mat对象
Mat inputMat = new Mat(approxPoints.Length, 1, MatType.CV_32SC2);
// 将Point数组中的元素复制到Mat对象中
for (int i = 0; i < approxPoints.Length; i++)
{
inputMat.Set(i, 0, approxPoints[i].X, approxPoints[i].Y);
}
// 将Mat对象转换为InputArray
InputArray inputArray = inputMat;
```
这里首先创建一个`Mat`对象,大小为`approxPoints.Length x 1`,类型为`CV_32SC2`。然后使用`Set()`方法将`Point`数组中的元素复制到`Mat`对象中。最后将`Mat`对象直接赋值给`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 了。