Ask Your Question
0

Java findHomography transform image point to ground plane point

asked 2015-01-17 06:58:36 -0500

Will Stewart gravatar image

Objective: Take a point (or set of points) from a camera perspective view and translate it/them to the respective ground plane points.

Approach: Used findHomography to obtain a homography mat using 7 points (in case RANSAC eliminated some). Plan to use perspectiveTransform().

Issue: Cannot interpret homography mat, nor understand how to continue (I don't know C, so cannot follow the example code). It appears 5 of the 9 elements are null, which I take to mean that the homography is faulty, though have no way of interpreting the results, so a made a number of print statements.

import org.opencv.calib3d.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.*;

import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;

final public class HomographyTest {

    public static void main(String[] args) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // needed by OpenCV

        String rfileoutput = "/home/will/dev/Homog.jpg";
        String ofileoutput = "/home/will/dev/HomogOutput.jpg";

        Point SEShedCornerDst = new Point(49, 74);
        Point CloseForsythiaDst = new Point(41, 41);
        Point CornerHazelDst = new Point(111, 157);
        Point FarForsythiaDst = new Point(175, 21);
        Point FirstLiquidAmberDst = new Point(235, 164);
        Point SecondLiquidAmberDst = new Point(282, 721);
        Point ThirdLiquidAmberDst = new Point(317, 544);

        Point SEShedCornerSrc = new Point(30, 231);
        Point CloseForsythiaSrc = new Point(160, 290);
        Point CornerHazelSrc = new Point(50, 125);
        Point FarForsythiaSrc = new Point(628, 146);
        Point FirstLiquidAmberSrc = new Point(299, 64);
        Point SecondLiquidAmberSrc = new Point(146, 37);
        Point ThirdLiquidAmberSrc = new Point(48,34);

        Point [] srcArray = new Point[7];
        srcArray[0] = SEShedCornerSrc;
        srcArray[1] = CloseForsythiaSrc;
        srcArray[2] = CornerHazelSrc;
        srcArray[3] = FarForsythiaSrc;
        srcArray[4] = FirstLiquidAmberSrc;
        srcArray[5] = SecondLiquidAmberSrc;
        srcArray[6] = ThirdLiquidAmberSrc;

        Mat OutputMat = new Mat();
        LinkedList<Point> dstArray = new LinkedList<Point>();

        dstArray.add(SEShedCornerDst);
        dstArray.add(CloseForsythiaDst);        
        dstArray.add(CornerHazelDst);
        dstArray.add(FarForsythiaDst);
        dstArray.add(FirstLiquidAmberDst);
        dstArray.add(SecondLiquidAmberDst);
        dstArray.add(ThirdLiquidAmberDst);

        MatOfPoint2f dst = new MatOfPoint2f();
        dst.fromList(dstArray);

        MatOfPoint2f src = new MatOfPoint2f();
        src.fromArray(srcArray);

        Mat Homog;


        Homog = Calib3d.findHomography(src, dst, Calib3d.RANSAC, 10, OutputMat);

        System.out.println("Columns = " + Homog.cols());
        System.out.println("Rows = " + Homog.rows());
        System.out.println("Width = " + Homog.width());
        System.out.println("Dims = " + Homog.dims());

        for (int i=1; i<= Homog.cols();i++){
            for (int j=1; j<=Homog.rows();j++){
                System.out.println("Row, column " + i + "," + j + " = " + Homog.get(j, i));
            }
            System.out.println();
        }
        System.out.println(Homog.toString());
        System.out.println(OutputMat.toString());
        Highgui.imwrite(rfileoutput, Homog);
        Highgui.imwrite(ofileoutput, OutputMat);
    }
}

The output;

Columns = 3
Rows = 3
Width = 3
Dims = 2
Row, column 1,1 = [D@674f1c67
Row, column 1,2 = [D@7ad1e32d
Row, column 1,3 = null

Row, column 2,1 = [D@6999de59
Row, column 2,2 = [D@74d4db38
Row, column 2,3 = null

Row, column 3,1 = null
Row, column 3,2 = null
Row, column 3,3 = null

Mat [ 3*3*CV_64FC1, isCont=true, isSubmat=false, nativeObj=0x7f744016bb50, dataAddr=0x7f744016b940 ]
Mat [ 7*1*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7f7440166fe0, dataAddr=0x7f744016b9b0 ]

Am I on the ... (more)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-29 06:56:20 -0500

ddantas gravatar image

What's up?

Thanks for the code. It helped me a lot. Let me help you a bit.

Java arrays start with zero, that's why some places appear to be null.

Homog and OutputMat are C native, so you can't read them before converting like this:

        double[]   java_homog = new double[Homog.cols() * Homog.rows()];
        byte[]     java_mask  = new byte[OutputMat.rows()];
        Homog.get(0, 0, java_homog);
        OutputMat.get(0, 0, java_mask);
edit flag offensive delete link more

Comments

Thanks, @ddantas

Will Stewart gravatar imageWill Stewart ( 2017-12-18 10:19:13 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2015-01-17 06:58:36 -0500

Seen: 433 times

Last updated: Jan 17 '15