Ask Your Question

Revision history [back]

![image description](https://images0.cnblogs.com/blog/508489/201505/041154595639968.jpg)
//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;
}

image description

![image description](https://images0.cnblogs.com/blog/508489/201505/041154595639968.jpg)
//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;
}