Ask Your Question
0

likely position of Feature Matching.

asked 2018-06-19 01:15:09 -0600

I am using Brute Force Matcher with L2 norm. Referring this link https://docs.opencv.org/2.4/doc/tutor...

After the process, I get below image as output

image description

What is the likely position of the object suggested by the feature matching?

I don't understand how to choose the likely position using this image :(

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
2

answered 2018-06-19 01:45:26 -0600

berak gravatar image

to retrieve the position of your matched object, you need some further steps :

  • filter the matches for outliers
  • extract the 2d point locations from the keypoints
  • apply findHomography() on the matched 2d points to get a transformation matrix between your query and the scene image
  • apply perspectiveTransform on the boundingbox of the query object, to see, where it is located in the scene image.
edit flag offensive delete link more
0

answered 2018-06-22 06:57:09 -0600

updated 2018-06-22 06:59:04 -0600

image description

//used surf
//
#include "stdafx.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{

    Mat img_1 ;
    Mat img_2 ;
    Mat img_raw_1 = imread("c1.bmp");
    Mat img_raw_2 = imread("c3.bmp");
    cvtColor(img_raw_1,img_1,CV_BGR2GRAY);
    cvtColor(img_raw_2,img_2,CV_BGR2GRAY);
    //-- Step 1: 使用SURF识别出特征点
    int minHessian = 400;
    SurfFeatureDetector detector( minHessian );
    std::vector<KeyPoint> keypoints_1, keypoints_2;
    detector.detect( img_1, keypoints_1 );
    detector.detect( img_2, keypoints_2 );
    //-- Step 2: 描述SURF特征
    SurfDescriptorExtractor extractor;
    Mat descriptors_1, descriptors_2;
    extractor.compute( img_1, keypoints_1, descriptors_1 );
    extractor.compute( img_2, keypoints_2, descriptors_2 );
    //-- Step 3: 匹配
    FlannBasedMatcher matcher;//BFMatcher为强制匹配
    std::vector< DMatch > matches;
    matcher.match( descriptors_1, descriptors_2, matches );
    //取最大最小距离
    double max_dist = 0; double min_dist = 100;
    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;
    }
    std::vector< DMatch > good_matches;
    for( int i = 0; i < descriptors_1.rows; i++ )
    { 
        if( matches[i].distance <= 3*min_dist )//这里的阈值选择了3倍的min_dist
        { 
            good_matches.push_back( matches[i]); 
        }
    }
    //-- Localize the object from img_1 in img_2
    std::vector<Point2f> obj;
    std::vector<Point2f> scene;
    for( int i = 0; i < (int)good_matches.size(); i++ )
    {    
        //这里采用“帧向拼接图像中添加的方法”,因此左边的是scene,右边的是obj
        scene.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
        obj.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
    }
    //直接调用ransac,计算单应矩阵
    Mat H = findHomography( obj, scene, CV_RANSAC );
    //图像对准
    Mat result;
    warpPerspective(img_raw_2,result,H,Size(2*img_2.cols,img_2.rows));
    Mat half(result,cv::Rect(0,0,img_2.cols,img_2.rows));
    img_raw_1.copyTo(half);
    imshow("result",result);
    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

One small point out is this example is FLANN based and my question is related to Brute Force Matcher with L2 norm :) Thanks anyway :)

MenukaIshan gravatar imageMenukaIshan ( 2018-06-22 22:54:41 -0600 )edit

Question Tools

Stats

Asked: 2018-06-19 01:15:09 -0600

Seen: 844 times

Last updated: Jun 22 '18