List<Point2f>转换成Point2f[]
时间: 2023-12-16 07:06:14 浏览: 42
你可以使用Java语言中的toArray()方法将List<Point2f>转换为Point2f数组。具体实现如下:
```
List<Point2f> pointList = new ArrayList<>();
// 假设 pointList 中已经添加了一些 Point2f 对象
// 将 pointList 转换为 Point2f 数组
Point2f[] pointArray = pointList.toArray(new Point2f[pointList.size()]);
```
这样,pointArray 就是 List<Point2f> pointList 的对应数组形式。
相关问题
Mat转成List<Point2f> cornersList
您可以使用以下代码将 `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 函数。
List<Point2f>转InputArray
要将一个 `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` 的转换。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)