Last active
March 30, 2019 08:42
-
-
Save jun1217/e8d5f4aa24b5fb05f847f2ed7733123f to your computer and use it in GitHub Desktop.
opencv_match_sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //原文:https://blog.csdn.net/zilanpotou182/article/details/68061929 | |
| #include <iostream> | |
| #include <stdio.h> | |
| #include "opencv2/core.hpp" | |
| #include "opencv2/core/utility.hpp" | |
| #include "opencv2/core/ocl.hpp" | |
| #include "opencv2/imgcodecs.hpp" | |
| #include "opencv2/highgui.hpp" | |
| #include "opencv2/features2d.hpp" | |
| #include "opencv2/calib3d.hpp" | |
| #include "opencv2/imgproc.hpp" | |
| #include"opencv2/flann.hpp" | |
| #include"opencv2/xfeatures2d.hpp" | |
| #include"opencv2/ml.hpp" | |
| using namespace cv; | |
| using namespace std; | |
| using namespace cv::xfeatures2d; | |
| using namespace cv::ml; | |
| int main() | |
| { | |
| Mat a = imread("3.png", 0);//左右相机的图像来源于MiddleBury图片库 | |
| Mat b = imread("4.png", 0); | |
| Ptr<SURF> surf; //创建方式和OpenCV2中的不一样,并且要加上命名空间xfreatures2d | |
| surf = SURF::create(800); | |
| BFMatcher matcher; //实例化一个暴力匹配器 | |
| Mat c, d; | |
| vector<KeyPoint>key1, key2; | |
| vector<DMatch> matches; //DMatch是用来描述匹配好的一对特征点的类,包含这两个点之间的相关信息 | |
| //比如左图有个特征m,它和右图的特征点n最匹配,这个DMatch就记录它俩最匹配,并且还记录m和n的 | |
| //特征向量的距离和其他信息,这个距离在后面用来做筛选 | |
| surf->detectAndCompute(a, Mat(), key1, c);//输入图像,输入掩码,输入特征点,输出Mat,存放所有特征点的描述向量 | |
| surf->detectAndCompute(b, Mat(), key2, d);//这个Mat行数为特征点的个数,列数为每个特征向量的尺寸,SURF是64(维) | |
| matcher.match(c, d, matches); //匹配,数据来源是特征向量,结果存放在DMatch类型里面 | |
| //sort函数对数据进行升序排列 | |
| sort(matches.begin(), matches.end()); //筛选匹配点,根据match里面特征对的距离从小到大排序 | |
| vector< DMatch > good_matches; | |
| int ptsPairs = std::min(50, (int)(matches.size() * 0.15)); | |
| cout << ptsPairs << endl; | |
| for (int i = 0; i < ptsPairs; i++) | |
| { | |
| good_matches.push_back(matches[i]);//距离最小的50个压入新的DMatch | |
| } | |
| Mat outimg; //drawMatches这个函数直接画出摆在一起的图 | |
| drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); //绘制匹配点 | |
| imshow("桌面", outimg); | |
| cvWaitKey(0); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment