SfM三维重建:BA优化


使用BA优化对SfM三维重建的结果进行非线性优化,环境:VS2015+OpenCV3.4+PCL1.8+Ceres Solver,包含两张图、多张图BA优化代码及images文件。
资源截图
代码片段和文件信息
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#define GLOG_NO_ABBREVIATED_SEVERITIES

#include 
#include 
#include 

#include  
#include 

#include 
#include 
#include 

#include 
#include 

#include “gflags/gflags.h“
#include “glog/logging.h“

using namespace std;
using namespace cv;
using namespace pcl;
using namespace cv::xfeatures2d;

// 代价函数
struct ReprojectCost
{
cv::Point2d observation;

ReprojectCost(cv::Point2d& observation) : observation(observation)
{
}

// 参数:内参、外参、三维点、反向投影误差
template 
bool operator()(const T* const intrinsic const T* const extrinsic const T* const pos3d T* residuals) const
{
const T* r = extrinsic;
const T* t = &extrinsic[3];

T pos_proj[3];
ceres::AngleAxisRotatePoint(r pos3d pos_proj);

// Apply the camera translation
pos_proj[0] += t[0];
pos_proj[1] += t[1];
pos_proj[2] += t[2];

const T x = pos_proj[0] / pos_proj[2];
const T y = pos_proj[1] / pos_proj[2];

const T fx = intrinsic[0];
const T fy = intrinsic[1];
const T cx = intrinsic[2];
const T cy = intrinsic[3];

// Apply intrinsic
const T u = fx * x + cx;
const T v = fy * y + cy;

residuals[0] = u - T(observation.x);
residuals[1] = v - T(observation.y);

return true;
}
};

// 提取所有图像的特征点 及 特征点处的RGB
void extract_features(vector& image_names vector>& keypoints_for_all vector& descriptor_for_all vector>& colors_for_all);
// ratio & symmetry test
void ratioTest(vector> &matches vector &goodMatches);
void symmetryTest(const vector& matches1 const vector& matches2 vector& symMatches);

// 匹配所有特征点
void match_features(vector& descriptor_for_all vector>& matches_for_all);
// 由匹配对提取特征点对
void get_matched_points(vector keypoints1vector keypoints2vector goodMatchesvector& points1vector& points2);
// 获取匹配点的RGB
void get_matched_colors(vector& color1 vector& color2 vector matches vector& out_c1 vector& out_c2);

// 剔除p1中mask值为0的元素
void maskout_points(vector& p1 Mat& mask);
void maskout_colors(vector& p1 Mat& mask);

// 重建前2张图片
void reconstruct_first2imgs(Mat K vector>& key_points_for_all vector>& colors_for_all vector>& matches_for_all vector& structure vector>& correspond_struct_idx vector& colors vector& rotations vector& translations);

// 三维重建
// 前两张图片重建
void reconstruct(Mat& K vector& points1 vector& points2 Mat& R Mat& t Mat& mask vector& poi

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       23063  2019-03-15 13:43  BA_MultiView_Rec.cpp
     文件       13219  2019-03-15 13:42  BA_Signle2Images.cpp
     目录           0  2019-03-15 13:48  images
     文件     6047052  2018-04-22 08:33  images00.png
     文件     6273917  2018-04-22 08:33  images01.png
     文件     6382709  2018-04-22 08:33  images02.png
     文件     6363282  2018-04-22 08:33  images03.png
     文件     6340215  2018-04-22 08:33  images04.png
     文件     6365922  2018-04-22 08:33  images05.png
     文件     6262661  2018-04-22 08:33  images06.png
     文件     6347685  2018-04-22 08:33  images07.png
     文件     6481233  2018-04-22 08:33  images08.png
     文件     6540372  2018-04-22 08:33  images09.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)