资源简介
使用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 << endl;
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 < key_points.size(); ++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 < knn_matches.size(); ++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 < min_dist) min_dist = dist;
}
matches.clear();
for (size_t r = 0; r < knn_matches.size(); ++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 < descriptor_for_all.size() - 1; ++i)
{
cout << “Matching images “ << i << “ - “ << i + 1 << endl;
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年
- 下一篇:视觉显著模型代码合集
评论
共有 条评论