OpenCV中图像拼接代码
一、数据结构和数据的基本定义
1、【OpenCV】KeyPoint数据结构
【OpenCV】KeyPoint数据结构
讲解了KeyPoint数据结构的细节信心
2、OpenCV 之 特征匹配
OpenCV 之 特征匹配
讲解了特征匹配的两种方法:暴力匹配 (Brute force matching) 和 最近邻匹配 (Nearest Neighbors matching)
3、Opencv Surf算子中keyPoints,描述子Mat矩阵,配对向量DMatch里都包含了哪些好玩的东东?
Opencv Surf算子中keyPoints,描述子Mat矩阵,配对向量DMatch里都包含了哪些好玩的东东?
说明了keyPoints、描述子Mat矩阵和配对向量DMatch的一些结构性的内容。
二、具体的代码
对匹配点进行显示
Mat preGrayFrame;
Mat curGrayFrame;
cvtColor(preFrame, preGrayFrame, COLOR_BGR2GRAY, 1);
cvtColor(curFrame, curGrayFrame, COLOR_BGR2GRAY, 1);
imshow("preFrame", preFrame);
waitKey(1);
Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(); //提取特征点
//Ptr<SurfDescriptorExtractor> extractor = SurfDescriptorExtractor::create();
Ptr<BFMatcher> matcher = BFMatcher::create(); //这里填写匹配方式,Brute-force matcher create method.暴力匹配,它们都继承自 DescriptorMatcher,是基于特征描述符距离的匹配,根据描述符的不同,距离可以是 欧氏距离,也可以是 汉明距
vector<KeyPoint> preKeyPoints, curKeyPoints; //存储特征点(关键点)
Mat preDescriptors, curDescriptors; //存储
//检测关键点并计算其周围区域的描述符(descriptors),这个描述符用于特征点的匹配,然后将它们扭曲成圆圈。
detector->detectAndCompute(preGrayFrame, Mat(), preKeyPoints, preDescriptors);
detector->detectAndCompute(curGrayFrame, Mat(), curKeyPoints, curDescriptors);
//输出检测到的关键点的大小和描述符
cout << "Total keypoints: " << preKeyPoints.size() << endl;
cout << "Total keypoints: " << preKeyPoints.size() << endl;
cout << "preDescriptors: " << preDescriptors.size() << endl;
imshow("preDescriptors", preDescriptors);
imshow("curDescriptors", curDescriptors);
waitKey(1);
//通过两阶段优化matches
vector<DMatch> matches, goodMatches1, goodMatches2; //存储匹配的特征对(配对向量)
matcher->match(preDescriptors, curDescriptors, matches); //前后两帧的特征点进行配对
//显示前一帧和当前帧的匹配图像
cout << "play" << endl;
Mat first_match;
drawMatches(preFrame, preKeyPoints, curFrame, curKeyPoints, matches, first_match);
imshow("first_match ", first_match);
waitKey(1);
cout << "over" << endl;
注意:其中preFrame和curFrame必须是原始图像(三通道图像)或者其他规定的格式。