ORB_GMS图像对齐

  1 // OpenCV_Align.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2 
  3 #include <iostream>
  4 
  5 #include <opencv2/opencv.hpp>
  6 #include <opencv2/xfeatures2d.hpp>
  7 #include <opencv2/features2d.hpp>
  8 using namespace std;
  9 using namespace cv;
 10 using namespace cv::xfeatures2d;
 11 
 12 
 13 /**
 14 *
 15 * @param im1 对齐图像
 16 * @param im2 模板图像
 17 * @finalMatches 匹配图像
 18 * @param im1Reg 输出图像
 19 * @param h   
 20 */
 21 void alignImages(Mat &im1, Mat &im2, Mat &finalMatches,Mat &im1Reg, Mat &h)
 22 {
 23     // Convert images to grayscale
 24     Mat im1Gray, im2Gray;
 25     cvtColor(im1, im1Gray, COLOR_BGR2GRAY);
 26     cvtColor(im2, im2Gray, COLOR_BGR2GRAY);
 27 
 28     // Variables to store keypoints and descriptors
 29     vector<KeyPoint> keypoints1, keypoints2;
 30     Mat descriptors1, descriptors2;
 31     
 32   
 33     Ptr<ORB> orb = ORB::create(1000); //最大数目感觉是对特征点计算Harris得分排序,这个数最好大一点,1000-5000
 34     orb->setFastThreshold(20);//Fast角点检测中用来确定候选点和角点的比较数目阈值,这个数最好小一点,5-10左右。太大有可能提取不到几个点,再经过细筛就没了
 35 
 36     orb->detectAndCompute(im1Gray, Mat(), keypoints1, descriptors1);
 37     orb->detectAndCompute(im2Gray, Mat(), keypoints2, descriptors2);
 38 
 39     // Match features. 
 40     vector<DMatch> matches, matchesGMS;
 41 
 42    //FALNN匹配的计算速度会比暴力匹配快些,但是点相对稳定性差些
 43 
 44     Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
 45     matcher->match(descriptors1, descriptors2, matches, Mat());
 46 
 47     cout << "matches: " << matches.size() << endl; 
 48 
 49     matchGMS(im1.size(), im2.size(), keypoints1, keypoints2, matches, matchesGMS);
 50     cout << "matchesGMS: " << matchesGMS.size() << endl;
 51 
 52 
 53 
 54     drawMatches(im1, keypoints1, im2, keypoints2, matchesGMS, finalMatches);
 55     
 56 
 57     vector<Point2f> points1, points2;
 58     for (size_t i = 0; i < matchesGMS.size(); i++)   
 59     {
 60         //queryIdx是对齐图像的描述子和特征点的下标。
 61         points1.push_back(keypoints1[matchesGMS[i].queryIdx].pt);
 62         //queryIdx是是样本图像的描述子和特征点的下标。
 63         points2.push_back(keypoints2[matchesGMS[i].trainIdx].pt);
 64     }
 65 
 66 
 67     // Find homography 
 68     h = findHomography(points1, points2, RANSAC);
 69 
 70     // Use homography to warp image 
 71     warpPerspective(im1, im1Reg, h, im2.size());
 72 }
 73 
 74 
 75 int main()
 76 {
 77     // Read image to be aligned 
 78     string imFilename("D:/222.jpg");
 79     cout << "Reading image to align : " << imFilename << endl;
 80     Mat im = imread(imFilename);
 81 
 82     // Read reference image 
 83     string refFilename("D:/refer.jpg");
 84     cout << "Reading reference image : " << refFilename << endl;
 85     Mat imReference = imread(refFilename);
 86 
 87     // Registered image will be resotred in imReg.
 88     // The estimated homography will be stored in h.
 89     Mat im_Aligned, h, final_Match;
 90 
 91     double detecttime = (double)getTickCount();
 92     // Align images
 93     cout << "Aligning images ..." << endl;
 94     alignImages(im, imReference, final_Match,im_Aligned, h);
 95 
 96 
 97 
 98     //计算检测时间
 99     detecttime = ((double)getTickCount() - detecttime) / getTickFrequency();
100     printf("-----cost time-------:%7.3fs
", detecttime);
101 
102     
103     // Write aligned image to disk.
104     string outFilename("aligned.jpg");
105     cout << "Saving aligned image : " << outFilename << endl;
106     imwrite(outFilename, im_Aligned);
107     imwrite("matches.jpg", final_Match);
108     
109     // Print estimated homography
110     cout << "Estimated homography : 
" << h << endl;
111     system("pause");
112     return 0;
113 }
原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725208.html