ImageMatching with SURF :
// ImageMatchwithSURF.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include //cv::BruteForceMatcher使用
int main()
{
//获取image1特征点向量
cv::Mat image1 = cv::imread("../../aTestImage/church01.jpg", 0);
std::vector keypoints1; //特征点向量
cv::SurfFeatureDetector surf(2500.); //SURF特征检测器
surf.detect(image1, keypoints1);
//获取image2特征点向量
cv::Mat image2 = cv::imread("../../aTestImage/church03.jpg", 0);
std::vector keypoints2; //特征点向量
//cv::SurfFeatureDetector surf(2500.);
surf.detect(image2, keypoints2);
//获取image1的SURF描述子
cv::SurfDescriptorExtractor surfDesc; //构造SURF描述子提取器
cv::Mat descriptors1;
surfDesc.compute(image1, keypoints1, descriptors1); //提取SURF描述子
//获取image2的SURF描述子
//cv::SurfDescriptorExtractor surfDesc; //构造SURF描述子提取器
cv::Mat descriptors2;
surfDesc.compute(image2, keypoints2, descriptors2); //提取SURF描述子
//进行匹配
cv::BruteForceMatcher matcher; //构造匹配器
std::vector matches; //存储 匹配后的 匹配描述子
matcher.match(descriptors1, descriptors2, matches);
//筛选距离最近的25个匹配结果
std::nth_element(matches.begin(), matches.begin() + 24, matches.end()); //
matches.erase(matches.begin() + 25, matches.end()); //删除其它匹配结果
//生成匹配图像
cv::Mat imageMatches;
cv::drawMatches(
image1, keypoints1,
image2, keypoints2,
matches, imageMatches, cv::Scalar(255, 255, 255));
cv::namedWindow("imageMatches");
cv::imshow("imageMatches", imageMatches);
cv::waitKey(0);
return 0;
}
ImageMatching with SIFT:
// ImageMatchwithSIFT.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include //cv::BruteForceMatcher使用
int main()
{
//获取image1特征点向量
cv::Mat image1 = cv::imread("../../aTestImage/church01.jpg", 0);
使用SIFT(Scale-Invarient Feature Transform)提取特征点
std::vector keypoints1; //特征点向量
cv::SiftFeatureDetector sift(0.03, 3.); //SIFT特征检测器 (0.03, 3.)初始化值不合适时无结果
sift.detect(image1, keypoints1);
//获取image2特征点向量
cv::Mat image2 = cv::imread("../../aTestImage/church03.jpg", 0);
std::vector keypoints2; //特征点向量
cv::SiftFeatureDetector sift2(0.03, 3.);
sift2.detect(image2, keypoints2);
//获取image1的SIFT描述子
cv::SiftDescriptorExtractor siftDesc; //构造SIFT描述子提取器
cv::Mat descriptors1;
siftDesc.compute(image1, keypoints1, descriptors1); //提取SIFT描述子
//获取image2的SURF描述子
cv::SiftDescriptorExtractor siftDesc2; //构造SIFT描述子提取器
cv::Mat descriptors2;
siftDesc2.compute(image2, keypoints2, descriptors2); //提取SIFT描述子
//进行匹配
cv::BruteForceMatcher matcher; //构造匹配器
std::vector matches; //存储 匹配后的 匹配描述子
matcher.match(descriptors1, descriptors2, matches);
//筛选距离最近的25个匹配结果
std::nth_element(matches.begin(), matches.begin() + 34, matches.end()); //
matches.erase(matches.begin() + 35, matches.end()); //删除其它匹配结果
//生成匹配图像
cv::Mat imageMatches;
cv::drawMatches(
image1, keypoints1,
image2, keypoints2,
matches, imageMatches, cv::Scalar(255, 255, 255));
cv::namedWindow("imageMatches");
cv::imshow("imageMatches", imageMatches);
cv::waitKey(0);
return 0;
}
Remark: