how to correctly detect corners of the scen image with perspectiveTransform

asked 2015-04-10 09:14:40 -0500

RB gravatar image

updated 2015-04-10 09:57:11 -0500

In the below code i am tryin to draw lines between the scene's corners "the train image", i followed this tutorial page 367 here, but when i run the code, i receive the below posted image which displays detection of wrong object in the scene image "train image".

please see the image that indicates wronge detection, and the detected area is in red

please let me know what i am missing and how to correct it.

    MatOfPoint2f objPointMat = new MatOfPoint2f();
    MatOfPoint2f scenePointMat = new MatOfPoint2f();
    objPointMat.fromList(objPoint);
    scenePointMat.fromList(scenePoint);

    Mat H = Calib3d.findHomography(objPointMat, scenePointMat, 8, 3);

    Mat objCorners = new Mat(4, 1, CvType.CV_32FC2);
    Mat sceneCorners = new Mat(4, 1, CvType.CV_32FC2);

    objCorners.put(0, 0, new double[] {0, 0});//top left
    objCorners.put(1, 0, new double[] {matFactory.getMatAt(0).cols(), 0});//top right
    objCorners.put(2, 0, new double[] {matFactory.getMatAt(0).cols(), matFactory.getMatAt(0).rows()});//bottom right
    objCorners.put(3, 0, new double[] {0, matFactory.getMatAt(0).rows()});//bottom left.

    Core.perspectiveTransform(objCorners, sceneCorners, H);

    Log.D(TAG, "descriptorMatcher", "objCorners.dump: " + objCorners.dump());//to display the contents of objCorners matrix
    Log.D(TAG, "descriptorMatcher", "sceneCorners.dump: " + sceneCorners.dump());//to display the contents of sceneCorners matrix

    /*matFactory.getMatAt(0) refers to the object image */
     double p10 = sceneCorners.get(0, 0)[0] + matFactory.getMatAt(0).cols();
     double p11 = sceneCorners.get(0, 0)[1] ;

     double p20 = sceneCorners.get(1, 0)[0] + matFactory.getMatAt(0).cols();
     double p21 = sceneCorners.get(1, 0)[1];

     double p30 = sceneCorners.get(2, 0)[0] + matFactory.getMatAt(0).cols();
     double p31 = sceneCorners.get(2, 0)[1];

     double p40 = sceneCorners.get(3, 0)[0] + matFactory.getMatAt(0).cols();
     double p41 = sceneCorners.get(3, 0)[1];

     Point p1 = new Point(p10, p11);
     Point p2 = new Point(p20, p21);
     Point p3 = new Point(p30, p31);
     Point p4 = new Point(p40, p41);

    Core.line(outImg, p1, p2, new Scalar(0, 0, 255), 4);
    Core.line(outImg, p2, p3, new Scalar(0, 0, 255), 4);
    Core.line(outImg, p3, p4, new Scalar(0, 0, 255), 4);
    Core.line(outImg, p4, p1, new Scalar(0, 0, 255), 4);

    MatFactory.writeMat(FilePathUtils.newOutputPath("detectedCorners"), outImg);

OutPut of sceneCorners matrix and objcorners matrix:

Debug: MainClass -> descriptorMatcher: objCorners.dump: [0, 0;
225, 0;
225, 221;
0, 221]

Debug: MainClass -> descriptorMatcher: sceneCorners.dump: [313.53854, 296.17175;
263.7662, 292.51437;
2429.9856, 2136.5327;
309.76358, 91.591682]

image description

edit retag flag offensive close merge delete