使用findHomography时,Android opencv发生断言错误

我正在开发Android版Opencv的Android应用程序。 我使用ORB检测器和bruteforce匹配器从输入帧中的图像中找到一个特征。 这很好(我猜)接下来我想围绕输入框架中找到的功能绘制一个框。 但是这样做错了,在findHomography调用时出错。 但我不知道为什么,在LogCat中几帧之后说下面的断言错误:

12-03 15:34:42.690:E / AndroidRuntime(22063):CvException [org.opencv.core.CvException:/home/reports/ci/slave_desktop/50-SDK/opencv/modules/calib3d/src/fundam.cpp :235:error:(-215)count> = 4 int cvFindHomography(const CvMat *,const CvMat *,CvMat *,int,double,CvMat *)

Android opencv代码如下(用新代码编辑):

TemplateImageTemp = new Mat();
            InputImageTemp = new Mat();
            InputImage = new Mat();
            TemplateImage = new Mat();

            // input frame has RGBA format
            InputImage = inputFrame.rgba();

            File ImagePath = new File(Environment.getExternalStorageDirectory(), "lena.png");

            TemplateImage = Highgui.imread(ImagePath.getAbsolutePath()); 

            Log.i(TAG, ImagePath.getAbsolutePath());

            if(TemplateImage.empty()){
                 Log.i(TAG, "========================= Empty ========================");
            }else
            {
                 Log.i(TAG, "========================= Loaded! ========================");
            }

            Imgproc.cvtColor(InputImage, InputImageTemp, Imgproc.COLOR_RGBA2RGB);
            Imgproc.cvtColor(TemplateImage ,TemplateImageTemp , Imgproc.COLOR_RGBA2RGB);

            // ORB detector and matcher
            FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
            DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
            DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);

            MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
            MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

            detector.detect(InputImageTemp, keypoints_object);
            detector.detect(TemplateImageTemp, keypoints_scene);

            Mat descriptor_object = new Mat();
            Mat descriptor_scene = new Mat() ;

            extractor.compute(InputImageTemp, keypoints_object, descriptor_object);
            extractor.compute(TemplateImageTemp, keypoints_scene, descriptor_scene);

            MatOfDMatch matches = new MatOfDMatch();

            matcher.match(descriptor_object, descriptor_scene, matches);
            List<DMatch> matchesList = matches.toList();

            Double max_dist = 0.0;
            Double min_dist = 100.0;

            for(int i = 0; i < descriptor_object.rows(); i++){
                Double dist = (double) matchesList.get(i).distance;
                if(dist < min_dist) min_dist = dist;
                if(dist > max_dist) max_dist = dist;
            }

            System.out.println("-- Max dist : " + max_dist);
            System.out.println("-- Min dist : " + min_dist);    

            LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
            MatOfDMatch gm = new MatOfDMatch();

            for(int i = 0; i < descriptor_object.rows(); i++){
                if(matchesList.get(i).distance < 3*min_dist){
                    good_matches.addLast(matchesList.get(i));
                }
            }

            gm.fromList(good_matches);


            TempImage = InputImageTemp;
            System.out.println("==== 1 ====");

            LinkedList<Point> objList = new LinkedList<Point>();
            LinkedList<Point> sceneList = new LinkedList<Point>();

            List<KeyPoint> keypoints_objectList = keypoints_object.toList();
            List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();

            for(int i = 0; i<good_matches.size(); i++){
                objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
                sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
            }


            MatOfPoint2f obj = new MatOfPoint2f();
            obj.fromList(objList);

            MatOfPoint2f scene = new MatOfPoint2f();
            scene.fromList(sceneList);

            Mat H = Calib3d.findHomography(obj, scene);   

            Mat obj_corners = new Mat(4,1,CvType.CV_32FC2);
            Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

            System.out.println("==== 4 ====");

            obj_corners.put(0, 0, new double[] {0,0});
            obj_corners.put(1, 0, new double[] {TemplateImage.cols(),0});
            obj_corners.put(2, 0, new double[] {TemplateImage.cols(),TemplateImage.rows()});
            obj_corners.put(3, 0, new double[] {0,TemplateImage.rows()});      

            System.out.println("==== 5 ====");

            Core.perspectiveTransform(obj_corners,scene_corners, H);         

            System.out.println("==== 6 ====");
            Core.line(TempImage, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
            Core.line(TempImage, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
            Core.line(TempImage, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
            Core.line(TempImage, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);


            System.out.print("Number of good matches: ");
            System.out.println (good_matches.size());

            OutputImage = TempImage;

            System.out.println("==== 8 ====");      


            System.out.print("Cols image out: ");
            System.out.println (OutputImage.cols());
            System.out.print("Rows image out: ");
            System.out.println (OutputImage.rows());
            System.out.print("Type image out: ");
            System.out.println (OutputImage.type());

            break;

有人有想法或建议吗? 所有反馈都欢迎!


您必须检查objscene列表是否包含4个或更多元素,否则findHomography将失败。 4分是估计单应性所需的最小值。

if(obj.size()>=4 && scene.size()>=4){
    Mat H = Calib3d.findHomography(obj, scene);  
}
链接地址: http://www.djcxy.com/p/35409.html

上一篇: Assertion error with Android opencv when using findHomography

下一篇: OpenCV and connecting a webcam?