OpenCV图像匹配算法之sift

OpenCV图像匹配算法之sift,第1张

概述OpenCV图像匹配算法之sift

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

    //utils.h      #ifndef _UTILS_H      #define _UTILS_H            #include <opencv2/opencv.hpp>      #include <opencv2/features2d/features2d.hpp>      #include <opencv2/core/core.hpp>      #include <opencv2/imgproc/imgproc.hpp>      #include <opencv2\nonfree\nonfree.hpp>      using namespace cv;            // ORB settings      const int ORB_MAX_KPTS = 1500;      const float ORB_SCALE_FACTOR = 1.5;      const int ORB_PYRAMID_LEVELS = 3;      const float ORB_EDGE_THRESHolD = 31.0;      const int ORB_FirsT_PYRAMID_LEVEL = 0;      const int ORB_WTA_K = 2;      const int ORB_PATCH_SIZE = 31;            // BRISK settings      const float BRISK_HTHRES = 10.0;      const int BRISK_NOCTAVES = 6;                  const float dratIO = 0.8f;                          // NNDR Matching value      const float MIN_H_ERROR = 2.50f;            // Maximum error in pixels to accept an inlIEr            voID matches2points_nndr(const std::vector<cv::KeyPoint>& train,const std::vector<cv::KeyPoint>& query,const std::vector<std::vector<cv::DMatch> >& matches,std::vector<cv::Point2f>& pmatches,float nndr);      voID compute_inlIErs_ransac(const std::vector<cv::Point2f>& matches,std::vector<cv::Point2f>& inlIErs,float error,bool use_fund);      voID draw_inlIErs(const cv::Mat& img1,const cv::Mat& img2,cv::Mat& img_com,const std::vector<cv::Point2f>& ptpairs,int color);            typedef struct info      {          double t;          int n1;          int n2;          int m;          int rm;      }INFO;            voID sift(char* path1,char* path2,INFO& info,bool show);      voID surf(char* path1,bool show);      voID orb(char* path1,bool show);      voID brisk(char* path1,bool show);      voID freak(char* path1,bool show);      voID showInfo(INFO info);            #endif  

    //utils.cpp      #include "stdafx.h"      #include "utils.h"      #include <iostream>      using namespace std;            /**      * @brIEf This function converts matches to points using nearest neighbor distance      * ratio matching strategy      * @param train Vector of keypoints from the first image      * @param query Vector of keypoints from the second image      * @param matches Vector of nearest neighbors for each keypoint      * @param pmatches Vector of putative matches      * @param nndr Nearest neighbor distance ratio value      */      voID matches2points_nndr(const std::vector<cv::KeyPoint>& train,float nndr) {              float dist1 = 0.0,dist2 = 0.0;        for (size_t i = 0; i < matches.size(); i++) {          DMatch dmatch = matches[i][0];          dist1 = matches[i][0].distance;          dist2 = matches[i][1].distance;                if (dist1 < nndr*dist2) {            pmatches.push_back(train[dmatch.queryIDx].pt);            pmatches.push_back(query[dmatch.trainIDx].pt);          }        }      }            /**      * @brIEf This function computes the set of inlIErs estimating the fundamental matrix      * or a planar homography in a RANSAC procedure      * @param matches Vector of putative matches      * @param inlIErs Vector of inlIErs      * @param error The minimum pixelic error to accept an inlIEr      * @param use_fund Set to true if you want to compute a fundamental matrix      */      voID compute_inlIErs_ransac(const std::vector<cv::Point2f>& matches,bool use_fund) {              vector<Point2f> points1,points2;        Mat H = Mat::zeros(3,3,CV_32F);        int npoints = matches.size()/2;        Mat status = Mat::zeros(npoints,1,CV_8UC1);              for (size_t i = 0; i < matches.size(); i+=2) {          points1.push_back(matches[i]);          points2.push_back(matches[i+1]);        }              if (use_fund == true){          H = findFundamentalMat(points1,points2,CV_FM_RANSAC,error,0.99,status);        }        else {          H = findHomography(points1,CV_RANSAC,status);        }              for (int i = 0; i < npoints; i++) {          if (status.at<unsigned char>(i) == 1) {            inlIErs.push_back(points1[i]);            inlIErs.push_back(points2[i]);          }        }      }            //*******************************************************************************      //*******************************************************************************            /**       * @brIEf This function draws the set of the inlIErs between the two images       * @param img1 First image       * @param img2 Second image       * @param img_com Image with the inlIErs       * @param ptpairs Vector of point pairs with the set of inlIErs       * @param color The color for each method       */      voID draw_inlIErs(const cv::Mat& img1,int color) {              int x1 = 0,y1 = 0,x2 = 0,y2 = 0;        float rows1 = 0.0,cols1 = 0.0;        float rows2 = 0.0,cols2 = 0.0;        float ufactor = 0.0,vfactor = 0.0;              rows1 = img1.rows;        cols1 = img1.cols;        rows2 = img2.rows;        cols2 = img2.cols;        ufactor = (float)(cols1)/(float)(cols2);        vfactor = (float)(rows1)/(float)(rows2);              // This is in case the input images don't have the same resolution        Mat img_aux = Mat(Size(img1.cols,img1.rows),CV_8UC3);        resize(img2,img_aux,Size(img1.cols,CV_INTER_liNEAR);              for (int i = 0; i < img_com.rows; i++) {          for (int j = 0; j < img_com.cols; j++) {            if (j < img1.cols) {              *(img_com.ptr<unsigned char>(i)+3*j) = *(img1.ptr<unsigned char>(i)+3*j);              *(img_com.ptr<unsigned char>(i)+3*j+1) = *(img1.ptr<unsigned char>(i)+3*j+1);              *(img_com.ptr<unsigned char>(i)+3*j+2) = *(img1.ptr<unsigned char>(i)+3*j+2);            }            else {              *(img_com.ptr<unsigned char>(i)+3*j) = *(img_aux.ptr<unsigned char>(i)+3*(j-img_aux.cols));              *(img_com.ptr<unsigned char>(i)+3*j+1) = *(img_aux.ptr<unsigned char>(i)+3*(j-img_aux.cols)+1);              *(img_com.ptr<unsigned char>(i)+3*j+2) = *(img_aux.ptr<unsigned char>(i)+3*(j-img_aux.cols)+2);            }          }        }              for (size_t i = 0; i < ptpairs.size(); i+= 2) {          x1 = (int)(ptpairs[i].x+.5);          y1 = (int)(ptpairs[i].y+.5);          x2 = (int)(ptpairs[i+1].x*ufactor+img1.cols+.5);          y2 = (int)(ptpairs[i+1].y*vfactor+.5);                if (color == 0) {            line(img_com,Point(x1,y1),Point(x2,y2),CV_RGB(255,255,0),1);          }          else if (color == 1) {            line(img_com,1);          }          else if (color == 2) {            line(img_com,CV_RGB(0,255),1);          }        }      }                  voID showInfo(INFO info)      {          printf("%-40s%d\n","The keypoints number of src image is :",info.n1);          printf("%-40s%d\n","The keypoints number of dst image is : ",info.n2);          printf("%-40s%d\n","The matching number is : ",info.m);          printf("%-40s%d\n","The right result number is : ",info.rm);          printf("%-40s%.2fs\n","The total time is : ",info.t);          return ;      }  

    //sift.cpp      #include "stdafx.h"      #include <cv.hpp>      #include <highgui.h>      #include "utils.h"      #include <iostream>      using namespace std;            voID sift(char* path1,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);          }                   Ptr<FeatureDetector> sift_detector = FeatureDetector::create( "SIFT" );          Ptr<DescriptorExtractor> sift_descriptor = DescriptorExtractor::create( "SIFT" );            vector<KeyPoint> kpts1_sift,kpts2_sift;          Mat desc1_sift,desc2_sift;          Ptr<DescriptorMatcher> matcher_l2 = DescriptorMatcher::create("BruteForce");      //欧氏距离匹配          vector<vector<DMatch> > dmatches_sift;          vector<Point2f> matches_sift,inlIErs_sift;                sift_detector->detect(img1,kpts1_sift);          sift_detector->detect(img2,kpts2_sift);          info.n1=kpts1_sift.size();          info.n2=kpts2_sift.size();          sift_descriptor->compute(img1,kpts1_sift,desc1_sift);          sift_descriptor->compute(img2,kpts2_sift,desc2_sift);          matcher_l2->knnMatch(desc1_sift,desc2_sift,dmatches_sift,2);                                     //匹配          matches2points_nndr(kpts1_sift,matches_sift,dratIO);          info.m=matches_sift.size()/2;          compute_inlIErs_ransac(matches_sift,inlIErs_sift,MIN_H_ERROR,false);          info.rm=inlIErs_sift.size()/2;                t2=cvGetTickCount();          info.t=(t2-t1)/1000000.0/cvGetTickFrequency();                Mat img1_rgb_sift = imread(path1,1);          Mat img2_rgb_sift = imread(path2,1);          Mat img_com_sift = Mat(Size(img1.cols*2,CV_8UC3);                if(show == true)          {              draw_inlIErs(img1_rgb_sift,img2_rgb_sift,img_com_sift,2);              imshow("sift",img_com_sift);              waitKey(0);          }                return;      }  

使用
    INFO sift_info;      sift(path1,path2,sift_info,true);      showInfo(sift_info);  

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的OpenCV图像匹配算法之sift全部内容,希望文章能够帮你解决OpenCV图像匹配算法之sift所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1232272.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存