如何在MatOfPoint3f中添加新元素或修改新元素?(OpenCV,Java)



我正在使用java的OpenCV api。我正在尝试使用MatOfPoint3f元素。我发现您可以初始化并插入所需的所有数据,只需一次。插入一些数据后,我想添加新数据或修改元素。
我该怎么做?

我必须尝试一下,但当我使用类MatOfPoint3f的.fromArray()或.fromList()方法插入新数据时,已经插入的数据将被清除,只有新数据保留在那里。

我有这个代码

    int i = 2;
    int j = 4;
    double k = 0.0;
    Point3[] point = new Point3[2];
    point[0] = new Point3(i, j, k);
    point[1] = new Point3(2*i, 2*j, 2*k);
    System.out.println("Point3: "+point);

    MatOfPoint3f objectCorners;
    objectCorners = new MatOfPoint3f(point[0]);
    System.out.println("1-objectCorners"+objectCorners.toList());   
    objectCorners.fromArray(point);
    System.out.println("2-objectCorners:"+objectCorners);
    objectCorners.fromArray(point[1]);
    System.out.println("3-objectCorners"+objectCorners.toList());

我在控制台中得到这个结果:

Point3: [Lorg.opencv.core.Point3;@5e222e
1-objectCorners[{2.0, 4.0, 0.0}]
2-objectCorners:Mat [ 2*1*CV_32FC3, isCont=true, isSubmat=false, nativeObj=0x539a320, dataAddr=0x539e310 ]
3-objectCorners[{4.0, 8.0, 0.0}]

很抱歉回答晚了,但仍然可以帮助别人。我使用了带有double[]的put()方法,它成功了。我把y从4.0修改为5.0。

    java.util.List<Point3> pointsList = new ArrayList<Point3>();
    MatOfPoint3f pointsMat = new MatOfPoint3f();
    int i = 2;
    int j = 4;
    double k = 0.0;
    Point3[] point = new Point3[2];
    point[0] = new Point3(i, j, k);
    point[1] = new Point3(2*i, 2*j, 2*k);
    pointsList.add(point[0]);
    pointsList.add(point[1]);
    // MatOfPoint3f from List
    pointsMat.fromList(pointsList);
    // Index of element to change
    int elementIndex = 0;
    System.out.println("Before modifying");
    Point3 readedPoint = pointsMat.toList().get(elementIndex);
    System.out.println(readedPoint);
    // Modify a point
    point[elementIndex].y = 5.0; // Changing y to 5.0 for example
    double[] modified = {point[elementIndex].x, point[elementIndex].y, point[elementIndex].z};
    pointsMat.put(0, elementIndex, modified);
    System.out.println("After modifying");
    readedPoint = pointsMat.toList().get(elementIndex);
    System.out.println(readedPoint);

控制台输出:

Before modifying
{2.0, 4.0, 0.0}
After modifying
{2.0, 5.0, 0.0}

相关内容

  • 没有找到相关文章

最新更新