Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Strange epipolar lines and 3d reconstruction [OpenCV for Java]

Hi,
I'm working on my 3d reconstruction project and I'm having problems with obtaining proper epipolar lines.
Key points
Epilines
My main concern is that they not intersect keypoints.
I'm using SURF feature detector (java_3d_reconstruction\src\KeyPairsDetector.java)

private void detectKeyPoints(MatOfKeyPoint keyPoints1, MatOfKeyPoint keyPoints2) {
    FeatureDetector surfDetector = FeatureDetector.create(FeatureDetector.SURF);
    surfDetector.detect(getImg1(), keyPoints1);
    surfDetector.detect(getImg2(), keyPoints2);
}

than FLANNBASED matcher (java_3d_reconstruction\src\KeyPairsDetector.java)

DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
        ArrayList<MatOfDMatch> matches = new ArrayList<MatOfDMatch>();
        descriptorMatcher.knnMatch(descriptors1, descriptors2, matches, 1);

and I'm filtering good matches using mask obtained by findHomography with RANSAC (java_3d_reconstruction\src\KeyPairsDetector.java)

Mat mask = new Mat();
        Calib3d.findHomography(srcPoints, dstPoints, Calib3d.RANSAC, 5, mask);

        List<MatOfDMatch> goodMatches = getInliers(mask, matches);

Then I'm calculating fundamental matrix using 8 point algorithm (java_3d_reconstruction\src\EpipolarGeometry.java):

private void computeFundamentalMatrix() {
        fundamentalMatrix  = Calib3d.findFundamentalMat(srcPoints, dstPoints, Calib3d.FM_8POINT, 0.0, 0.0);
    }

computing epipolar lines (java_3d_reconstruction\src\EpipolarGeometry.java):

private void computeEpiLines() {
        epilinesSrc = new Mat();
        epilinesDst = new Mat();

        Calib3d.computeCorrespondEpilines(srcPoints, 1, fundamentalMatrix, epilinesDst);
        Calib3d.computeCorrespondEpilines(dstPoints, 2, fundamentalMatrix, epilinesSrc);
    }

and drawing them (java_3d_reconstruction\src\EpipolarGeometry.java):

public Mat drawEpiLines(Mat outImg) {

        int epiLinesCount = epilinesSrc.rows();

        double a, b, c;

        for (int line = 0; line < epiLinesCount; line++) {
            a = epilinesSrc.get(line, 0)[0];
            b = epilinesSrc.get(line, 0)[1];
            c = epilinesSrc.get(line, 0)[2];

            int x0 = 0;
            int y0 = (int) (-(c + a * x0) / b);
            int x1 = outImg.cols() / 2;
            int y1 = (int) (-(c + a * x1) / b);

            Point p1 = new Point(x0, y0);
            Point p2 = new Point(x1, y1);
            Scalar color = new Scalar(255, 255, 255);
            Core.line(outImg, p1, p2, color);

        }

        for (int line = 0; line < epiLinesCount; line++) {
            a = epilinesDst.get(line, 0)[0];
            b = epilinesDst.get(line, 0)[1];
            c = epilinesDst.get(line, 0)[2];

            int x0 = outImg.cols() / 2;
            int y0 = (int) (-(c + a * x0) / b);
            int x1 = outImg.cols();
            int y1 = (int) (-(c + a * x1) / b);

            Point p1 = new Point(x0, y0);
            Point p2 = new Point(x1, y1);
            Scalar color = new Scalar(255, 255, 255);
            Core.line(outImg, p1, p2, color);

        }
        return outImg;
    }

The main code:

public class Main {
    static {
        System.loadLibrary("opencv_java2410");
    }
    public static void main(String[] args) {
        System.loadLibrary("opencv_java2410");


        Mat img1 = Highgui.imread("images\\SylvainJpg\\S01.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
        Mat img2 = Highgui.imread("images\\SylvainJpg\\S02.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);

        KeyPairsDetector keyPairsDetector = new KeyPairsDetector(img1,img2);
        EpipolarGeometry epipolarGeometry = new EpipolarGeometry(keyPairsDetector.srcSortedGoodPoints,
                keyPairsDetector.dstSortedGoodPoints);

        Mat matchedImg = keyPairsDetector.drawMatchesAndKeyPoints();
        epipolarGeometry.drawEpiLines(matchedImg);

        Highgui.imwrite("images\\SylvainJpg\\S00c.jpg", matchedImg);

    }
}

Whole code is available here GitHub repo

Could you please help me work out what is wrong here?