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 settingsconst 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 settingsconst float brisk_hthres = 10.0;const int BRISK_NOC Taves = 6;const Float dratio = 0.8f;//nndr Matching valueconst float min_h_error = 2.50f;//Maximum ERROR in pixels to AC Cept an inliervoid matches2points_nndr (const std::vector<cv::keypoint>& train, const STD ::vector<cv::keypoint>& query, const STD::VECTOR<STD::VECTOR<CV::D match> >&A mp Matches, std::vector<CV::P oint2f>& pmatches, float nndr), void Compute_inliers_ransac (const STD::VECTOR<CV::P oint2f>& Matches, STD::VECTOR<CV::P oint2f>& inliers, float error, b Ool use_fund); void draw_inliers (const cv::mat& IMG1, const cv::mat& IMG2, cv::mat& img_com, C Onst STD::VECTOR<CV::P oint2f>& 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, char* path2, info& info, bo OL show); void Orb (char* path1, char* path2, info& info, bool show); void Brisk (char* path1, char* path2, info& info , bool show), void Freak (char* path1, char* path2, info& INFO, 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 fr Om the first image * @param query vector of keypoints from the second image * @param matches vector of nearest neighbors F or each KeyPoint * @param pmatches Vector of putative matches * @param nndr Nearest neighbor distance ratio value */void m Atches2points_nndr (const std::vector<cv::keypoint>& train, const STD::VECTOR<CV::KEYP oint>& query, const STD::VECTOR<STD::VECTOR<CV::D match> >& matches, STD::VECTOR<CV::P oint2f>& pmatches, 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 Minim Um pixelic error to accept the inlier * @param use_fund Set to True if you want to compute a fundamental matrix */void comp Ute_inliers_ransac (const STD::VECTOR<CV::P oint2f>& matches, STD::VECTOR<CV::P oint2 f>& inliers, float error, 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,points2,cv_ransac,error,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 Images * @param img1 First image * @param img2 Second image * @param img_com image with the inliers * @param ptpairs V Ector of point pairs with the set of Inliers * @param color, the color for each method */void draw_inliers (const CV::MAT&AM P IMG1, const cv::mat& IMG2, cv::mat& img_com, const STD::VECTOR<CV::P oint2f>& ptpairs, I NT 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); The "in" case of the input images don ' t has the same resolution mat Img_aux = Mat (Size (img1.cols,img1.rows), CV_8UC3); Resize (img2,img_aux,size (img1.cols,img1.rows), 0,0,cv_inter_linear); for (int i = 0, i < img_com.rows; i++) {for (int j = 0; J < Img_com.cols; J + +) {if (< 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,point (x1,y1), point (X2,y2), Cv_rgb (255,0,0), 1); } else if (color = = 2) {line (Img_com,point (x1,y1), point (X2,y2), Cv_rgb (0,0,255), 1); }}}void showinfo (info info) {printf ("%-40s%d\n", "the keypoints number of SRC image is:", info.n1);p rintf ("%-40s%d\n", "Th E keypoints number of DST image is: ", info.n2);p rintf ("%-40s%d\n "," The matching number is: ", INFO.M);p rintf ("%-40s%d\n "," The right result number is: ", info.rm);p rintf ("%-40s%.2fs\n "," The Total time is: ", info.t); return;}
Sift.cpp#include "stdafx.h" #include <cv.hpp> #include Use
INFO Sift_info;sift (path1,path2,sift_info,true); Showinfo (Sift_info);
The sift of OPENCV image matching algorithm