Mat转成List<Point2f> cornersList
时间: 2023-07-09 16:12:41 浏览: 49
您可以使用以下代码将 `Mat` 类型的角点坐标转换为 `List<Point2f>` 类型:
```csharp
// 创建一个 List<Point2f> 类型的 cornersList 变量
List<Point2f> cornersList = new List<Point2f>();
// 将 Mat 类型的角点坐标转换为 Point2f 类型,并添加到 cornersList 中
for (int i = 0; i < cornersMat.Rows; i++)
{
Point2f point = new Point2f(cornersMat.At<float>(i, 0), cornersMat.At<float>(i, 1));
cornersList.Add(point);
}
```
其中,`cornersMat` 是包含角点坐标的 `Mat` 类型变量。上述代码中,通过遍历每一行的数据,将第一列和第二列分别转换为 `float` 类型的坐标值,并创建一个 `Point2f` 类型的点,最后将点添加到 `cornersList` 中。
转换完成后,您就可以将 `cornersList` 作为 `IEnumerable<Point2f>` 类型的参数传递给其他 OpenCV 函数。
相关问题
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 了。
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` 的转换。