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

时间:2014-04-24 14:41:57

标签: java opencv

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

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

我有这段代码

    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}]

1 个答案:

答案 0 :(得分:0)

对于迟到的回答感到抱歉,但仍然可以帮助某人。 我使用方法put()与double [],它工作。 我将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}