前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >OpenCV图像匹配算法之surf

OpenCV图像匹配算法之surf

原创
作者头像
大师级码师
修改2021-09-22 10:46:21
修改2021-09-22 10:46:21
65100
代码可运行
举报
文章被收录于专栏:大师级码师大师级码师
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
//surf.cpp

#include "stdafx.h"  
#include <cv.hpp>  
#include <highgui.h>  
#include "utils.h"  
#include <iostream>  
using namespace std;  

void surf(char* path1, char* path2, INFO& info, bool show)  
{  
    double t1,t2;  
    t1=cvGetTickCount();  

    initModule_nonfree();  

    Mat img1, img2;  
    img1=imread(path1,0);  
    img2=imread(path2,0);  
    if(img1.data==NULL)  
    {  
        cout<<"The image can not been loaded: "<<path1<<endl;  
        system("pause");  
        exit(-1);  
    }  
    if(img2.data==NULL)  
    {  
        cout<<"The image can not been loaded: "<<path2<<endl;  
        system("pause");  
        exit(-1);  
    }  

    int minHessian=200;  
    SurfFeatureDetector surf_detector(minHessian) ;  
    SurfDescriptorExtractor surf_descriptor ;    
    vector<KeyPoint> kpts1_surf, kpts2_surf;  
    Mat desc1_surf, desc2_surf;  
    Ptr<cv::DescriptorMatcher> matcher_l2 = DescriptorMatcher::create("BruteForce");          //欧氏距离匹配  
    vector<vector<DMatch> > dmatches_surf;  
    vector<Point2f> matches_surf, inliers_surf;  

    surf_detector.detect(img1,kpts1_surf);  
    surf_detector.detect(img2,kpts2_surf);  
    info.n1 = kpts1_surf.size();  
    info.n2 = kpts2_surf.size();  

    surf_descriptor.compute(img1,kpts1_surf,desc1_surf);  
    surf_descriptor.compute(img2,kpts2_surf,desc2_surf);  
    matcher_l2->knnMatch(desc1_surf,desc2_surf,dmatches_surf,2);  
    matches2points_nndr(kpts1_surf,kpts2_surf,dmatches_surf,matches_surf,DRATIO);  
    info.m=matches_surf.size()/2;  
    compute_inliers_ransac(matches_surf,inliers_surf,MIN_H_ERROR,false);  
    info.rm=inliers_surf.size()/2;  

    t2=cvGetTickCount();  
    info.t=(t2-t1)/1000000.0/cvGetTickFrequency();  

    Mat img1_rgb_surf = imread(path1,1);  
    Mat img2_rgb_surf = imread(path2,1);  
    Mat img_com_surf = Mat(Size(img1.cols*2,img1.rows),CV_8UC3);  

    if(show == true)  
    {  
        draw_inliers(img1_rgb_surf,img2_rgb_surf,img_com_surf,inliers_surf,2);  
        imshow("surf",img_com_surf);  
        waitKey(0);  
    }  

    return;  
}  </pre> 
 

 使用
    INFO surf_info;  
    surf(path1,path2,surf_info,false);  
    showInfo(surf_info);    

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档