接上一篇图片匹配教程
和上次ORB对比一下
上次的效果
本次的效果
上次的效果
本次的效果
可以说比上次好的多得多
测试环境
win10
visual studio 2019
opencv-3.4.3
opencv_contrib-3.4.3
cmake 3.20.3
备注
sift是有专利的算法, cmake编译时需要勾选 OPENCV_ENABLE_NONFREE, 才能使用有专利的算法
opencv源码编译可参考该视频教程:
vs2019+opencv3.4.6+contrib3.4.6源码编译
测试源码
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/xfeatures2d/nonfree.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat trainImage = imread("D:/script/myGit/autojs-flann-feature-matching/图片素材/game001.png"), trainImage_gray;
//imshow("trainImage", trainImage);
cvtColor(trainImage, trainImage_gray, COLOR_BGR2GRAY);
vector<KeyPoint>train_keyPoint;
Mat trainDescription;
//SiftFeatureDetector featureDetector;
//featureDetector.detect(trainImage_gray, train_keyPoint);
Ptr<xfeatures2d::SiftFeatureDetector> featureDetector = xfeatures2d::SiftFeatureDetector::create();
featureDetector->detect(trainImage_gray, train_keyPoint);
Ptr<xfeatures2d::SiftDescriptorExtractor> featureExtractor = xfeatures2d::SiftDescriptorExtractor::create();
featureExtractor->compute(trainImage_gray, train_keyPoint, trainDescription);
BFMatcher matcher;
vector<Mat>train_desc_collection(1, trainDescription);
matcher.add(train_desc_collection);
matcher.train();
Mat captureImage, captureImage_gray;
captureImage = imread("D:/script/myGit/autojs-flann-feature-matching/图片素材/game001_女生头像.png");
cvtColor(captureImage, captureImage_gray, COLOR_BGR2GRAY);
vector<KeyPoint>test_keyPoint;
Mat testDescriptor;
featureDetector->detect(captureImage_gray, test_keyPoint);
featureExtractor->compute(captureImage_gray, test_keyPoint, testDescriptor);
vector<vector<DMatch>>matches;
matcher.knnMatch(testDescriptor, matches, 2);
vector<DMatch>goodMatches;
for (unsigned int i = 0; i < matches.size(); i++)
{
if (matches[i][0].distance < 0.6 * matches[i][1].distance)
goodMatches.push_back(matches[i][0]);
}
Mat dstImage;
drawMatches(captureImage, test_keyPoint, trainImage, train_keyPoint, goodMatches, dstImage);
imshow("dstImage", dstImage);
cv::waitKey(0);
return 0;
}
参考文章
源代码(《OpenCV3编程入门》综合示例:SIFT配合暴力匹配进行关键点描述和提取)
Visual Studio 版本 与 MSVC 编译器 版本
Visual Studio 2019 重新生成 OpenCV 解决方案报 LNK2019 无法解析的外部符号 __imp__Py_NegativeRefcount、__imp_Py_RefTotal
win10下用 Visual Studio 2019 重新生成 OpenCV 解决方案报 LNK1104 无法打开文件 “python38_d.lib”
CMake编译OpenCV4.0时opencv_ffmpeg.dll等下载失败的解决思路总结
OpenCV - Windows(win10)编译opencv + opencv_contrib
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途
bilibili
微信公众号 牙叔教程
QQ群
747748653