Search code examples
javaopencvdoublegetter-setter

Add values to MatOfPoint variable in Java


I'm trying to convert a double[][] into a MatOfPoint of OpenCV. pointsOrdered is a double[4][2] with coordinates of four points in it. I've tried with:

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

but sourceMat values stay the same. I'm trying to add values one by one as I haven't found other options.

What can I do? Is there a simple way to access and modify a MatOfPoint variable values?


Solution

  • org.opencv.core.MatOfPoint expects org.opencv.core.Point objects but it stores Point's property values (x,y) not Point object's itself

    if you convert your double[][] pointsOrdered array to ArrayList<Point>

    ArrayList<Point> pointsOrdered = new ArrayList<Point>();
    pointsOrdered.add(new Point(xVal, yVal));
    ...
    

    then you can create MatOfPoint from this ArrayList<Point>

    MatOfPoint sourceMat = new MatOfPoint();
    sourceMat.fromList(pointsOrdered);
    //your sourceMat is Ready.