資源簡介
SIFT特征點匹配的代碼和實驗報告,需要2008的VS和openCV庫。如果VS版本高的話需要下載一個2008版本的guill.gll文件。修改代碼中視頻路徑即可實現(xiàn)特征點匹配。有實驗報告和用例。
代碼片段和文件信息
#include?“opencv2/opencv.hpp“
#include?“opencv2/nonfree/nonfree.hpp“//與SIFT相關(guān)
#include?“opencv2/legacy/legacy.hpp“//與匹配器相關(guān)
#include?
using?namespace?std;
using?namespace?cv;
const?int?N?=?1e6?+?5;
Mat?srcImg2;//比對的圖片
Mat?myframe[N];
int?cnt;
void?sift_ransac(Mat?img01?Mat?img02)
{
//SIFT特征檢測
SiftFeatureDetector?detector;????????//定義特點點檢測器
vector?keypoint01?keypoint02;//定義兩個容器存放特征點
detector.detect(img01?keypoint01);
detector.detect(img02?keypoint02);
//在兩幅圖中畫出檢測到的特征點
Mat?out_img01;
Mat?out_img02;
drawKeypoints(img01?keypoint01?out_img01);
drawKeypoints(img02?keypoint02?out_img02);
//提取特征點的特征向量(128維)
SiftDescriptorExtractor?extractor;
Mat?descriptor01?descriptor02;
extractor.compute(img01?keypoint01?descriptor01);
extractor.compute(img02?keypoint02?descriptor02);
//匹配特征點,主要計算兩個特征點特征向量的歐式距離,距離小于某個閾值則認(rèn)為匹配
BruteForceMatcher>?matcher;
vector?matches;
Mat?img_matches;
if?(descriptor02.cols?==?0?&&?descriptor02.rows?==?0)
return;
matcher.match(descriptor01?descriptor02?matches);
drawMatches(img01?keypoint01?img02?keypoint02?matches?img_matches);
//RANSAC?消除誤匹配特征點?主要分為三個部分:
//根據(jù)matches將特征點對齊將坐標(biāo)轉(zhuǎn)換為float類型
vector?R_keypoint01?R_keypoint02;
for?(size_t?i?=?0;?i {
R_keypoint01.push_back(keypoint01[matches[i].queryIdx]);
R_keypoint02.push_back(keypoint02[matches[i].trainIdx]);
}
//坐標(biāo)轉(zhuǎn)換
vectorp01?p02;
for?(size_t?i?=?0;?i {
p01.push_back(R_keypoint01[i].pt);
p02.push_back(R_keypoint02[i].pt);
}
//利用基礎(chǔ)矩陣剔除誤匹配點
vector?RansacStatus;
Mat?Fundamental?=?findFundamentalMat(p01?p02?RansacStatus?FM_RANSAC);
vector?RR_keypoint01?RR_keypoint02;
vector?RR_matches;????????????//重新定義RR_keypoint?和RR_matches來存儲新的關(guān)鍵點和匹配矩陣
int?index?=?0;
for?(size_t?i?=?0;?i {
if?(RansacStatus[i]?!=?0)
{
RR_keypoint01.push_back(R_keypoint01[i]);
RR_keypoint02.push_back(R_keypoint02[i]);
matches[i].queryIdx?=?index;
matches[i].trainIdx?=?index;
RR_matches.push_back(matches[i]);
index++;
}
}
Mat?img_RR_matches;
drawMatches(img01?RR_keypoint01?img02?RR_keypoint02?RR_matches?img_RR_matches);
std::vector?obj;
std::vector?scene;
for?(int?i?=?0;?i? {
obj.push_back(RR_keypoint01[RR_matches[i].queryIdx].pt);
scene.push_back(RR_keypoint02[RR_matches[i].trainIdx].pt);
}
if?(RR_matches.size()?==?0)
{
myframe[cnt++]?=?img_RR_matches;
return;
}
Mat?H?=?findHomography(obj?scene?CV_RANSAC);
std::vector?obj_corners(4);
obj_corners[0]?=?cvPoint(0?0);?obj_corners[1]?=?cvPoint(img01.cols?0);
obj_corners[2]?=?cvPoint(img01.cols?img01.rows);?obj_corners[3]?=?cvPoint(0?img01.rows);
std::vector?scene_corners(4);
perspectiveTransform(obj_corners?scene_corners?H);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-12-24?14:02??1120151964-李欣欣\
?????文件????11143266??2017-12-23?17:32??1120151964-李欣欣\output.avi
?????文件???????10015??2017-12-23?17:17??1120151964-李欣欣\src.jpg
?????文件?????1311317??2017-12-23?17:27??1120151964-李欣欣\src.mp4
?????文件????????4478??2017-12-24?12:44??1120151964-李欣欣\源.cpp
?????文件?????1425310??2017-12-24?14:01??1120151964-李欣欣\程序報告.docx
評論
共有 條評論