糟糕的结果findHomography

时间:2017-09-08 11:51:03

标签: android opencv camera opencv4android

我正在使用opencv4android 3.2来查找我使用findHomography方法的已知对象,当我捕获图像时,对象距离相机更远,它给了我一个好结果,但是当我捕捉到距离相机较远的物体时,我的效果很差。

  • 要查找的对象<​​/ li>

the object card to detect taken from the camera

  • 结果

the result of homography

这是我的代码

       // Matches current frame's descriptors to template's
            descriptorMatcher.match(descriptors, templateDescriptors, 
            matches);

            List<DMatch> matchesList = matches.toList();
            double maxDistance = 0;
            double minDistance = 100;
            int rowCount = descriptors.rows();
            for (int i = 0; i < rowCount; i++) {
                double dist = matchesList.get(i).distance;
                if (dist < minDistance) minDistance = dist;
                if (dist > maxDistance) maxDistance = dist;
            }

       List<DMatch> goodMatchesList = new ArrayList<>();


        double upperBound = 3 * minDistance;
            for (int i = 0; i < rowCount; i++) {
                if (matchesList.get(i).distance <= upperBound) {
                    goodMatchesList.add(matchesList.get(i));
                }
            }


            // Iterate through good matches and put the 2D points of the 
            object (template) and frame (scene) into a list
            List<KeyPoint> objKpList = new ArrayList<>();
            List<KeyPoint> sceneKpList = new ArrayList<>();
            objKpList = templateKeypoints.toList();
            sceneKpList = keypoints.toList();
            LinkedList<Point> objList = new LinkedList<>();
            LinkedList<Point> sceneList = new LinkedList<>();
            for (int i = 0; i < goodMatchesList.size(); i++) {

     objList.addLast(objKpList.get(goodMatchesList.get(i).trainIdx).pt);
     sceneList.addLast(sceneKpList.get(goodMatchesList.get(i).queryIdx).pt);
   }

            MatOfPoint2f obj = new MatOfPoint2f();
            MatOfPoint2f scene = new MatOfPoint2f();

            obj.fromList(objList);
            scene.fromList(sceneList);

            // Calculate the homography
            Mat H = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, 3);

          ...

       try {
                Core.perspectiveTransform(objCorners, sceneCorners, H);
                Point p1 = new Point(sceneCorners.get(0, 0));
                Point p2 = new Point(sceneCorners.get(1, 0));
                Point p3 = new Point(sceneCorners.get(2, 0));
                Point p4 = new Point(sceneCorners.get(3, 0));

                final List<Point> source = new ArrayList<Point>();
                    source.add(p1);
                    source.add(p2);
                    source.add(p3);
                    source.add(p4);
               Mat startM = Converters.vector_Point2f_to_Mat(source);
               flip(startM, startM, -1);
               Mat result = warp(rgba, startM);

               Bitmap bmppp = Bitmap.createBitmap(result.width(), 
               result.height(), Bitmap.Config.ARGB_8888);
               Utils.matToBitmap(result, bmppp);
               keypoints.release();
               SaveImage(result);

            } catch (CvException e) {
                e.printStackTrace();

                Log.e(TAG, "perspectiveTransform returned an assertion 
                failed error.");
                return false;
                }

为什么我没有得到好结果findHomography?

感谢。

这是用于查找卡片对象enter image description here

的模板

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。听起来很傻,但是您是否尝试过将 srcPoints 与 destinationPoints 切换?

我正在使用单应性来控制机器人的导航。我原来有:

M, mask = cv2.findHomography(robotPoints, visionpoints)

然而,通过切换机器人点和视觉点,它起作用了。我正在反向映射平面。

M, mask = cv2.findHomography(visionPoints,robotPoints)