0

i am using opencv java library and i am doing some image matching and object detection by computing the homography matrix and the perspecive transformation. at run time i the system crash and i rceive the below posted error message and i do not know what does it mean and how to solve it because as you see it below, it says Assertion failed (count >= 4) in cvFindHomography and it is not clear

please have a look at the code below that shows how i compute the homography matrix

code:

MatOfPoint2f objPointMat = new MatOfPoint2f();
                MatOfPoint2f scenePointMat = new MatOfPoint2f();

                objPointMat.fromList(objPoint);
                scenePointMat.fromList(scenePoint);

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

                if (H != null) {
                    if (!H.empty()) {
                        double det = Core.determinant(H);

                        if (det > 0.09) {
                            this.validHMatrix = true;
                            Log.D(TAG, "descriptorMatcher", this.token+"_valid HMatrix, det(H): "+det);
                        } else {
                            this.validHMatrix = false;
                            Log.D(TAG, "descriptorMatcher", this.token+"_invalid HMatrix, det(H): "+det);
                        }

                        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[] {this.obj.cols(), 0});//top right
                        objCorners.put(2, 0, new double[] {this.obj.cols(), this.obj.rows()});//bottom right
                        objCorners.put(3, 0, new double[] {0, this.obj.rows()});//bottom left.

                        Core.perspectiveTransform(objCorners, sceneCorners, H);// the values inside sceneCorners matrix changes after this line as it is what this function returns. 

                        if (!sceneCorners.empty()) {
                            double p10 = sceneCorners.get(0, 0)[0] + this.obj.cols();
                            double p11 = sceneCorners.get(0, 0)[1];// + matFactory.getMatAt(0).cols();

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

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

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

                            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(goodMatchesImage, p1, p2, new Scalar(0, 0, 255), 4);
                            Core.line(goodMatchesImage, p2, p3, new Scalar(0, 255, 0), 4);
                            Core.line(goodMatchesImage, p3, p4, new Scalar(255, 0, 0), 4);
                            Core.line(goodMatchesImage, p4, p1, new Scalar(255, 255, 255), 4);

console error:

    OpenCV Error: Assertion failed (count >= 4) in cvFindHomography, file ..\..\..\..\opencv\modules\calib3d\src\fundam.cpp, line 235
    java.util.concurrent.ExecutionException: CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\calib3d\src\fundam.cpp:235: error: (-215) count >= 4 in function cvFindHomography
    ]
        at java.util.concurrent.CompletableFuture.reportGet(Unknown Source)
        at java.util.concurrent.CompletableFuture.get(Unknown Source)
        at com.example.foa17_nolog.Performance.compSIFT(Performance.java:116)
        at com.example.foa17_nolog.Performance.compRep(Performance.java:86)
        at com.example.foa17_nolog.Performance.<init>(Performance.java:60)
        at com.example.foa17_nolog.MainClass$PerfMeasure.get(MainClass.java:216)
        at com.example.foa17_nolog.MainClass$PerfMeasure.get(MainClass.java:1)
        at java.util.concurrent.CompletableFuture$AsyncSupply.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

4

To detect a homography, you need to give the function at least 4 points that are "good". What is happening is that the image you are giving to the function does not have at least 4 good points to calculate the homography from, and that is why you are getting the error. As a result, to solve the error you would either have to find a way to get more good points or write an if statement that only calls findHomography when the image has 4 good points.

ksivakumar
  • 481
  • 2
  • 5
  • 19
  • do you mean that i ahve to check the size of the "objPointMat" ? and if it is greaterthan 1X4 then it should be fine? – Amrmsmb Jul 02 '15 at 14:03
  • 1
    If 'objPoint' is a vector of Point2f or some kind of list, you can just check the size of that list or vector and if it is equal or greater than 4, it should be fine. – ksivakumar Jul 02 '15 at 14:06
  • i have one more question please, is there any relation between the size of the object and the size of the scene? because i ahve a scne of 300x300 and i cropped from it 50x50 region, and when i made the matching, i found that the "objPoinList.size and scenPointList.size < 4" ... – Amrmsmb Jul 02 '15 at 15:13
  • I don't believe so. Check out this link (though it is for C++, the idea is the same). Also, the documentation just asks that the images be of a certain type of channel, it doesn't specify that you need the same size of image. That being said, one reason you may not be getting 4 points consistently could be because of this different size issue. http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html – ksivakumar Jul 02 '15 at 17:12