我正在尝试将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];
}
但CCD_ 5值保持不变。我正在尝试一个接一个地添加值,因为我没有找到其他选项。
我能做什么?有没有一种简单的方法可以访问和修改MatOfPoint
变量值?
org.opencv.core.MatOfPoint
需要org.opencv.core.Point
对象,但它存储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.
这里有一个过于复杂的示例答案,展示了可以做些什么,将建议的列表/数组/数组列表的使用(选择你的毒药)与真正的错误和导致问题的原因结合起来。"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]));
}