将值添加到 Java 中的 MatOfPoint 变量
Add values to MatOfPoint variable in Java
我正在尝试将 double[][]
转换为 OpenCV
的 MatOfPoint
。 pointsOrdered 是一个 double[4][2]
,里面有四个点的坐标。
我试过:
MatOfPoint sourceMat = new MatOfPoint();
for (int idx = 0; idx < 4; idx++) {
(sourceMat.get(idx, 0))[0] = pointsOrdered[idx][0];
(sourceMat.get(idx, 0))[1] = pointsOrdered[idx][1];
}
但 sourceMat
值保持不变。
我正在尝试一个一个地添加值,因为我还没有找到其他选项。
我能做什么?有没有简单的方法来访问和修改 MatOfPoint
变量值?
org.opencv.core.MatOfPoint
需要 org.opencv.core.Point
个对象,但它存储点的 属性 值 (x,y)
而不是 Point
对象本身
如果将 double[][] pointsOrdered
数组转换为 ArrayList<Point>
ArrayList<Point> pointsOrdered = new ArrayList<Point>();
pointsOrdered.add(new Point(xVal, yVal));
...
然后你可以从这个 ArrayList<Point>
创建 MatOfPoint
MatOfPoint sourceMat = new MatOfPoint();
sourceMat.fromList(pointsOrdered);
//your sourceMat is Ready.
这里有一个过于复杂的示例答案,用于说明将建议的 lists/arrays/array 列表(选择你的毒药)与真正错误并导致问题的地方结合起来可以做什么。 "Mat get" 方法和 "Mat put" 方法混淆了 - 用法颠倒了。
MatOfPoint sourceMat=new MatOfPoint();
sourceMat.fromArray( // a recommended way to initialize at compile time if you know the values
new Point(151., 249.),
new Point(105., 272.),
new Point(102., 318.),
new Point(138., 337.));
// A way to change existing data or put new data if need be if you set the size of the MatOfPoints first
double[] corner=new double[(int)(sourceMat.channels())];// 2 channels for a point
for (int idx = 0; idx<sourceMat.rows(); idx++) {
System.out.println((corner[0]=sourceMat.get(idx, 0)[0]) + ", " + (corner[1]=sourceMat.get(idx, 0)[1]));
corner[0]+=12.; // show some change can be made to existing point.x
corner[1]+=8.; // show some change can be made to existing point.y
sourceMat.put(idx, 0, corner);
System.out.println((corner[0]=(float)sourceMat.get(idx, 0)[0]) + ", " + (corner[1]=(float)sourceMat.get(idx, 0)[1]));
}
我正在尝试将 double[][]
转换为 OpenCV
的 MatOfPoint
。 pointsOrdered 是一个 double[4][2]
,里面有四个点的坐标。
我试过:
MatOfPoint sourceMat = new MatOfPoint();
for (int idx = 0; idx < 4; idx++) {
(sourceMat.get(idx, 0))[0] = pointsOrdered[idx][0];
(sourceMat.get(idx, 0))[1] = pointsOrdered[idx][1];
}
但 sourceMat
值保持不变。
我正在尝试一个一个地添加值,因为我还没有找到其他选项。
我能做什么?有没有简单的方法来访问和修改 MatOfPoint
变量值?
org.opencv.core.MatOfPoint
需要 org.opencv.core.Point
个对象,但它存储点的 属性 值 (x,y)
而不是 Point
对象本身
如果将 double[][] pointsOrdered
数组转换为 ArrayList<Point>
ArrayList<Point> pointsOrdered = new ArrayList<Point>();
pointsOrdered.add(new Point(xVal, yVal));
...
然后你可以从这个 ArrayList<Point>
MatOfPoint
MatOfPoint sourceMat = new MatOfPoint();
sourceMat.fromList(pointsOrdered);
//your sourceMat is Ready.
这里有一个过于复杂的示例答案,用于说明将建议的 lists/arrays/array 列表(选择你的毒药)与真正错误并导致问题的地方结合起来可以做什么。 "Mat get" 方法和 "Mat put" 方法混淆了 - 用法颠倒了。
MatOfPoint sourceMat=new MatOfPoint();
sourceMat.fromArray( // a recommended way to initialize at compile time if you know the values
new Point(151., 249.),
new Point(105., 272.),
new Point(102., 318.),
new Point(138., 337.));
// A way to change existing data or put new data if need be if you set the size of the MatOfPoints first
double[] corner=new double[(int)(sourceMat.channels())];// 2 channels for a point
for (int idx = 0; idx<sourceMat.rows(); idx++) {
System.out.println((corner[0]=sourceMat.get(idx, 0)[0]) + ", " + (corner[1]=sourceMat.get(idx, 0)[1]));
corner[0]+=12.; // show some change can be made to existing point.x
corner[1]+=8.; // show some change can be made to existing point.y
sourceMat.put(idx, 0, corner);
System.out.println((corner[0]=(float)sourceMat.get(idx, 0)[0]) + ", " + (corner[1]=(float)sourceMat.get(idx, 0)[1]));
}