資源簡介
使用OpenCV實現了多圖像的三維重建。
使用VS2015開發,程序運行后會讀取images目錄下的圖片進行重建。
重建完成后,可以運行Viewer下的SfMViewer.exe查看重建結果。
詳見博客 http://blog.csdn.net/aichipmunk/article/
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?cv;
using?namespace?std;
void?extract_features(
vector&?image_names
vector>&?key_points_for_all
vector&?descriptor_for_all
vector>&?colors_for_all
)
{
key_points_for_all.clear();
descriptor_for_all.clear();
Mat?image;
//讀取圖像,獲取圖像特征點,并保存
Ptr?sift?=?xfeatures2d::SIFT::create(0?3?0.04?10);
for?(auto?it?=?image_names.begin();?it?!=?image_names.end();?++it)
{
image?=?imread(*it);
if?(image.empty())?continue;
cout?<“Extracing?features:?“?<*it?<
vector?key_points;
Mat?descriptor;
//偶爾出現內存分配失敗的錯誤
sift->detectAndCompute(image?noArray()?key_points?descriptor);
//特征點過少,則排除該圖像
if?(key_points.size()?<=?10)?continue;
key_points_for_all.push_back(key_points);
descriptor_for_all.push_back(descriptor);
vector?colors(key_points.size());
for?(int?i?=?0;?i? {
Point2f&?p?=?key_points[i].pt;
colors[i]?=?image.at(p.y?p.x);
}
colors_for_all.push_back(colors);
}
}
void?match_features(Mat&?query?Mat&?train?vector&?matches)
{
vector>?knn_matches;
BFMatcher?matcher(NORM_L2);
matcher.knnMatch(query?train?knn_matches?2);
//獲取滿足Ratio?Test的最小匹配的距離
float?min_dist?=?FLT_MAX;
for?(int?r?=?0;?r? {
//Ratio?Test
if?(knn_matches[r][0].distance?>?0.6*knn_matches[r][1].distance)
continue;
float?dist?=?knn_matches[r][0].distance;
if?(dist? }
matches.clear();
for?(size_t?r?=?0;?r? {
//排除不滿足Ratio?Test的點和匹配距離過大的點
if?(
knn_matches[r][0].distance?>?0.6*knn_matches[r][1].distance?||
knn_matches[r][0].distance?>?5?*?max(min_dist?10.0f)
)
continue;
//保存匹配點
matches.push_back(knn_matches[r][0]);
}
}
void?match_features(vector&?descriptor_for_all?vector>&?matches_for_all)
{
matches_for_all.clear();
//?n個圖像,兩兩順次有?n-1?對匹配
//?1與2匹配,2與3匹配,3與4匹配,以此類推
for?(int?i?=?0;?i?riptor_for_all.size()?-?1;?++i)
{
cout?<“Matching?images?“?< vector?matches;
match_features(descriptor_for_all[i]?descriptor_for_all[i?+?1]?matches);
matches_for_all.push_back(matches);
}
}
bool?find_transform(Mat&?K?vector&?p1?vector&?p2?Mat&?R?Mat&?T?Mat&?mask)
{
//根據內參矩陣獲取相機的焦距和光心坐標(主點坐標)
double?focal_length?=?0.5*(K.at(0)?+?K.at(4));
Point2d?principle_point(K.at(2)?K.at(5));
//根據匹配點求取本征矩陣,使用RANSAC,進一步排除失配點
Mat?E?=?findEssentialMat(p1?p2?focal_length?principle_point?RANSAC?0.999?1.0?mask);
if?(E.empty())?return?false;
doubl
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-04-24?14:46??images\
?????文件?????6244540??2008-09-17?15:42??images\0001.png
?????文件?????6273917??2008-09-17?15:42??images\0002.png
?????文件?????6382709??2008-09-17?15:42??images\0003.png
?????文件???????12133??2016-04-24?13:56??main.cpp
?????文件?????????901??2016-04-23?14:01??OpenCV_3_ex_x86.props
?????文件?????????906??2016-04-23?14:01??OpenCV_3_ex_x86d.props
?????目錄???????????0??2016-04-24?14:18??Release\
?????文件?????2154749??2016-04-24?14:18??Release\main.obj
?????文件???????55296??2016-04-24?14:18??Release\SequentialSfM.exe
?????文件?????1564570??2016-04-24?14:18??Release\SequentialSfM.iobj
?????文件??????357024??2016-04-24?14:18??Release\SequentialSfM.ipdb
?????文件????????2125??2016-04-24?14:18??Release\SequentialSfM.log
?????文件?????1896448??2016-04-24?14:18??Release\SequentialSfM.pdb
?????目錄???????????0??2016-04-24?14:18??Release\SequentialSfM.tlog\
?????文件????????1430??2016-04-24?14:18??Release\SequentialSfM.tlog\cl.command.1.tlog
?????文件???????32516??2016-04-24?14:18??Release\SequentialSfM.tlog\CL.read.1.tlog
?????文件?????????290??2016-04-24?14:18??Release\SequentialSfM.tlog\CL.write.1.tlog
?????文件????????3322??2016-04-24?14:18??Release\SequentialSfM.tlog\li
?????文件????????4722??2016-04-24?14:18??Release\SequentialSfM.tlog\li
?????文件?????????580??2016-04-24?14:18??Release\SequentialSfM.tlog\li
?????文件?????????200??2016-04-24?14:18??Release\SequentialSfM.tlog\SequentialSfM.lastbuildstate
?????文件??????888832??2015-09-02?14:23??Release\vc120.pdb
?????文件?????1019904??2016-04-24?14:18??Release\vc140.pdb
?????文件?????????971??2015-09-01?19:18??SequentialSfM.sln
?????文件????????4407??2016-04-23?14:15??SequentialSfM.vcxproj
?????文件?????????956??2015-09-01?19:22??SequentialSfM.vcxproj.filters
?????目錄???????????0??2016-04-23?14:07??tinydir\
?????文件?????????359??2015-03-05?06:26??tinydir\.gitignore
?????文件?????????514??2015-03-05?06:26??tinydir\.travis.yml
?????文件????????1295??2015-03-05?06:26??tinydir\COPYING
............此處省略26個文件信息
- 上一篇:滑模變結構控制理論及應用 [陳志梅 著] 2012年
- 下一篇:視覺顯著模型代碼合集
評論
共有 條評論