0

我正在使用 ORB 和 Bruteforce 来识别 android 和 C++ 的对象。但是,在两种语言中使用相同的流程,结果似乎不一致或不同。例如,使用 C++,它给出 21 个匹配项,而在 android 中有 15 个匹配项。顺便说一句,我在两个测试中都使用了两个相同的图像。

我的 C++ 实现:

Mat img_1 = imread( "Object.jpg");
Mat img_2 = imread( "Scene.jpg");

if( !img_1.data || !img_2.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }

//-- Step 1: Detect the keypoints using ORB Detector
OrbFeatureDetector detector;

std::vector<KeyPoint> keypoints_1, keypoints_2;

detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 );

//-- Step 2: Calculate descriptors (feature vectors)
OrbDescriptorExtractor extractor;

Mat descriptors_1, descriptors_2;

extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 );

//-- Step 3: Matching descriptor vectors using BF matcher
BFMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );

double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}

printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );

//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
//-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
//-- small)
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;

for( int i = 0; i < descriptors_1.rows; i++ )
{ if( matches[i].distance <= 3*min_dist)
{ good_matches.push_back( matches[i]); }
}

对于Java:

       String path = Environment.getExternalStorageDirectory().getAbsolutePath();  

       Bitmap objectbmp = BitmapFactory.decodeFile(path+"/Sample/Object.jpg");
       Bitmap scenebmp = BitmapFactory.decodeFile(path+"/Sample/Scene.jpg");

        Mat object = new Mat(); //from the database
        Mat scene = new Mat(); //user's input image

        // convert bitmap to MAT
        Utils.bitmapToMat(objectbmp, object);
        Utils.bitmapToMat(scenebmp, scene);

        //Feature Detection
        FeatureDetector orbDetector = FeatureDetector.create(FeatureDetector.ORB);
        DescriptorExtractor orbextractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

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

        Mat descriptors_object = new Mat();
        Mat descriptors_scene = new Mat();

        //Getting the keypoints
        orbDetector.detect( object, keypoints_object );
        orbDetector.detect( scene, keypoints_scene );

        //Compute descriptors
        orbextractor.compute( object, keypoints_object, descriptors_object );
        orbextractor.compute( scene, keypoints_scene, descriptors_scene );

        //Match with Brute Force
        MatOfDMatch matches = new MatOfDMatch();
        DescriptorMatcher matcher;
        matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
        matcher.match( descriptors_object, descriptors_scene, matches );

        double max_dist = 0;
        double min_dist = 100;

        List<DMatch> matchesList = matches.toList();

        //-- Quick calculation of max and min distances between keypoints
          for( int i = 0; i < descriptors_object.rows(); i++ )
          { double dist = matchesList.get(i).distance;
            if( dist < min_dist ) min_dist = dist;
            if( dist > max_dist ) max_dist = dist;
          }

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

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

注意:我正在计算从相同输入中给出不同结果的好的匹配。

4

0 回答 0