[OpenCV] sift demo
2024-09-03 16:09:35
运行环境:vs2012+opencv320
sift 需要的头文件为 <opencv2/xfeatures2d.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp> using namespace cv;
using namespace std; bool refineMatchesWithHomography(
const std::vector<cv::KeyPoint>& queryKeypoints,
const std::vector<cv::KeyPoint>& trainKeypoints,
float reprojectionThreshold,
std::vector<cv::DMatch>& matches,
cv::Mat& homography)
{
const int minNumberMatchesAllowed = 8; if (matches.size() < minNumberMatchesAllowed)
return false; // Prepare data for cv::findHomography
std::vector<cv::Point2f> srcPoints(matches.size());
std::vector<cv::Point2f> dstPoints(matches.size()); for (size_t i = 0; i < matches.size(); i++) {
srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;
dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;
} // Find homography matrix and get inliers mask
std::vector<unsigned char> inliersMask(srcPoints.size());
homography = cv::findHomography(srcPoints, dstPoints, CV_FM_RANSAC,reprojectionThreshold, inliersMask); std::vector<cv::DMatch> inliers;
for (size_t i = 0; i < inliersMask.size(); i++) {
if (inliersMask[i])
inliers.push_back(matches[i]);
} matches.swap(inliers);
return matches.size() > minNumberMatchesAllowed;
} bool comp(vector<DMatch>& a,vector<DMatch>& b)
{
return a[0].distance/a[1].distance < b[0].distance/b[1].distance;
} void main()
{
Ptr<xfeatures2d::SIFT>feature=xfeatures2d::SIFT::create(); Mat input1 = imread("sift_img\\16.png",1);
Mat input2 = imread("sift_img\\11.png",1); vector<KeyPoint>kp1,kp2;
Mat des1,des2;
Mat output1,output2; feature->detectAndCompute(input1,cv::noArray(),kp1,des1);
drawKeypoints(input1,kp1,output1); feature->detectAndCompute(input2,cv::noArray(),kp2,des2);
drawKeypoints(input2,kp2,output2); vector<DMatch>matches;
vector<vector<DMatch> >Dmatches;
Ptr<cv::DescriptorMatcher> matcher_knn = new BFMatcher();
Ptr<cv::DescriptorMatcher> matcher = new BFMatcher(NORM_L2,true);
matcher->match(des1,des2,matches); matcher_knn->knnMatch(des1,des2,Dmatches,2);
sort(Dmatches.begin(),Dmatches.end(),comp); vector<DMatch> good;
for(int i=0;i<Dmatches.size();i++){
if(Dmatches[i][0].distance < 0.75*Dmatches[i][1].distance)
good.push_back(Dmatches[i][0]);
} Mat imResultOri;
drawMatches(output1, kp1, output2, kp2, matches, imResultOri,CV_RGB(0,255,0), CV_RGB(0,255,0)); Mat matHomo;
refineMatchesWithHomography(kp1, kp2, 3, matches, matHomo);
cout << "[Info] Homography T : " << endl << matHomo << endl; Mat imResult;
drawMatches(output1, kp1, output2, kp2, matches, imResult,CV_RGB(0,255,0), CV_RGB(0,255,0)); Mat Mgood;
drawMatches(output1, kp1, output2, kp2, good, Mgood,CV_RGB(0,255,0), CV_RGB(0,255,0)); imshow("ransc",imResult);
imshow("knn_match",Mgood);
waitKey(0); return;
}
最新文章
- C# 使用 StructLayoutAttribute 时 C# /C++ 内存空间分配与成员对齐问题
- Scrum 1.0
- 【leetcode】Best Time to Buy and Sell Stock II
- codeforces B.Maximum Absurdity 解题报告
- NSArray block用法
- IIS6.0禁止用户下载txt文件
- 源码生成deb包
- ajax后台处理返回json值
- jquery判断节点是否存在
- webpack2+node+react+babel实现热加载(hmr)
- 彻底区分html的attribute与dom的property
- LR多分类推广 - Softmax回归*
- DOTween-Ease缓动函数
- [C#]时间格式和字符串的相互转换
- 安装python3后使用pip和pip3的区别是什么?
- 准备开发一个运行在Android上的JavaME模拟器
- 初识elasticsearch_2(查询和整合springboot)
- 检测Sql Server服务器SQL语句执行情况
- java 的关键字 native
- PHP接收post请求,不是空数组就是没值,怎么办!
热门文章
- POJ3336 Making the Grade
- js里url里有特殊字符(如&;)情况,后台request.getParameter(";url";)里&;变成&
- 安装包设计-------安装(QT)---------知识总结
- SpringMVC配置 事务管理
- c++ 使用类生成随机数
- 为ubuntu安装powerline记录
- kafka学习汇总系列(一)kafka概述
- Linux设备驱动程序 之 tasklet
- vmware安装Linux
- CSS — 隐藏滚动条,依旧可以滚动