#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <Eigen/SVD>
#include <g2o/core/base_vertex.h>
#include <g2o/core/base_unary_edge.h>
#include <g2o/core/block_solver.h>
#include <g2o/core/optimization_algorithm_gauss_newton.h>
#include <g2o/solvers/eigen/linear_solver_eigen.h>
#include <g2o/types/sba/types_six_dof_expmap.h>
using namespace std;
using namespace cv;
void find_feature_matches(
const Mat& img_1, const Mat& img_2,
vector<KeyPoint>& keypoints_1,
vector<KeyPoint>& keypoints_2,
vector<DMatch>& matches
);
Point2d pixel2cam(const Point2d& p, const Mat& K);
void pose_estimation_3d3d(
const vector<Point3f>& pts1,
const vector<Point3f>& pts2,
Mat& R, Mat& t
);
void bundleAdjustment(
const vector<Point3f>& pts1,
const vector<Point3f>& pts2,
Mat& R, Mat& t
);
//define edge of g2o
//节点为优化变量,这里是g2o::VertexSE3Expmap类型的相机位姿,由于只有一帧,所以整个图中只有一个位姿量,也就只有一个顶点,进而边就是一元边,只连接到一个顶点
//边是误差项,这里的误差是观测到的3d点坐标减去计算得到的3d点坐标,也即观测值减去计算值,
//在这步减法运算中,类型是不变的,也就是误差类型和观测值的类型一样,就是Eigen::Vector3d类型,维度为3维
//继承自基本一元边类,观测值为3d空间点坐标,所以维度为3,类型为Eigen::Vector3d类型,一元边链接一个顶点,类型为相机位姿g2o::VertexSE3Expmap
class EdgeProjectXYZRGBDPoseOnly : public g2o::BaseUnaryEdge<3, Eigen::Vector3d, g2o::VertexSE3Expmap>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
//构造函数需要一个Eigen::Vector3d类型的 point
EdgeProjectXYZRGBDPoseOnly( const Eigen::Vector3d& point ) : _point(point) {}
//误差函数
virtual void computeError()
{
//从顶点容器_vertices[]中将唯的一个顶点取出来,并将其类型转换成位姿节点指针(注意这里还是节点类型指针,虽然这个节点是位姿,但是还是节点类型),用于后面_error中的计算值求取
const g2o::VertexSE3Expmap* pose = static_cast<const g2o::VertexSE3Expmap*> ( _vertices[0] );
// measurement is p, point is p'
//map函数定义: Vector3D map(const Vector3D & xyz) const { return _r*xyz+_t; }
//pose->estimate()取得估计值,也就是位姿了
//可以看到,pose->estimate().map( _point )计算后就是p'经过Rt变换后的坐标。然后跟观测值p作差
_error = _measurement - pose->estimate().map( _point );
}
//线性增量计算
virtual void linearizeOplus()
{
//又是把位姿节点取出来
g2o::VertexSE3Expmap* pose = static_cast<g2o::VertexSE3Expmap *>(_vertices[0]);
//调取位姿节点数据,并转换成转换向量,也就是李代数
g2o::SE3Quat T(pose->estimate());
//_point为p',这里乘上变换向量,就是世界坐标系中p的坐标,也就是变换之前,p在相机中的坐标,因为相机就动作了一次,将动作之前的位置定位世界坐标系
Eigen::Vector3d xyz_trans = T.map(_point);
double x = xyz_trans[0];
double y = xyz_trans[1];
double z = xyz_trans[2];
//以上这些步骤就是为了求p'经过变换后的p点坐标,P173页第一个式子。
//因为雅克比矩阵经计算知道,只跟变换后的坐标有关系,所以求出来直接定义就好了。P175页式7.60与P164页式7.44
_jacobianOplusXi(0,0) = 0;
_jacobianOplusXi(0,1) = -z;
_jacobianOplusXi(0,2) = y;
_jacobianOplusXi(0,3) = -1;
_jacobianOplusXi(0,4) = 0;
_jacobianOplusXi(0,5) = 0;
_jacobianOplusXi(1,0) = z;
_jacobianOplusXi(1,1) = 0;
_jacobianOplusXi(1,2) = -x;
_jacobianOplusXi(1,3) = 0;
_jacobianOplusXi(1,4) = -1;
_jacobianOplusXi(1,5) = 0;
_jacobianOplusXi(2,0) = -y;
_jacobianOplusXi(2,1) = x;
_jacobianOplusXi(2,2) = 0;
_jacobianOplusXi(2,3) = 0;
_jacobianOplusXi(2,4) = 0;
_jacobianOplusXi(2,5) = -1;
}
//输入输出这里留空
bool read ( istream& in ) {}
bool write ( ostream& out ) const {}
protected:
//这里的这个point就是采集到的有误差的数据。在边的定义中,要把采集到的数据定义为成员变量,采集进来,用于书写误差函数
Eigen::Vector3d _point;
};
int main ( int argc, char** argv )
{
if ( argc != 5 )
{
cout<<"usage: pose_estimation_3d3d img1 img2 depth1 depth2"<<endl;
return 1;
}
//-- 读取图像
Mat img_1 = imread ( argv[1], CV_LOAD_IMAGE_COLOR );
Mat img_2 = imread ( argv[2], CV_LOAD_IMAGE_COLOR );
vector<KeyPoint> keypoints_1, keypoints_2;
vector<DMatch> matches;
find_feature_matches ( img_1, img_2, keypoints_1, keypoints_2, matches );
cout<<"一共找到了"<<matches.size() <<"组匹配点"<<endl;
// 建立3D点
Mat depth1 = imread ( argv[3], CV_LOAD_IMAGE_UNCHANGED ); // 深度图为16位无符号数,单通道图像
Mat depth2 = imread ( argv[4], CV_LOAD_IMAGE_UNCHANGED ); // 深度图为16位无符号数,单通道图像
Mat K = ( Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
vector<Point3f> pts1, pts2;
for ( DMatch m:matches )
{
//这个过程注释参见3d2d那个如何由二维像素图片和深度图片生成3d点,其实这块功能也可以单独拿出写成个函数
ushort d1 = depth1.ptr<unsigned short> ( int ( keypoints_1[m.queryIdx].pt.y ) ) [ int ( keypoints_1[m.queryIdx].pt.x ) ];
ushort d2 = depth2.ptr<unsigned short> ( int ( keypoints_2[m.trainIdx].pt.y ) ) [ int ( keypoints_2[m.trainIdx].pt.x ) ];
if ( d1==0 || d2==0 ) // bad depth
continue;
Point2d p1 = pixel2cam ( keypoints_1[m.queryIdx].pt, K );
Point2d p2 = pixel2cam ( keypoints_2[m.trainIdx].pt, K );
float dd1 = float ( d1 ) /1000.0;
float dd2 = float ( d2 ) /1000.0;
pts1.push_back ( Point3f ( p1.x*dd1, p1.y*dd1, dd1 ) );
pts2.push_back ( Point3f ( p2.x*dd2, p2.y*dd2, dd2 ) );
}
cout<<"3d-3d pairs: "<<pts1.size() <<endl;
Mat R, t;
pose_estimation_3d3d(pts1, pts2, R, t);
cout<<"ICP via SVD results: "<<endl;
cout<<"R = "<<R<<endl;
cout<<"t = "<<t<<endl;
cout<<"R_inv = "<<R.t() <<endl;//.t()方法为Mat类型的R的求逆方法
cout<<"t_inv = "<<-R.t() *t<<endl;
cout<<"calling bundle adjustment"<<endl;
bundleAdjustment( pts1, pts2, R, t );
return 0;
}
void find_feature_matches(
const Mat& img_1, const Mat& img_2,
std::vector<KeyPoint>& keypoints_1, std::vector<KeyPoint>& keypoints_2,
std::vector< DMatch >& matches_good
)
{
//创建描述子
Mat descriptors_1, descriptors_2;
//初始化为三步走:
//创建detector,用于检测keypoint
Ptr<FeatureDetector> detector = ORB::create();
//创建descriptor,用于检测描述子
Ptr<DescriptorExtractor> descriptor = ORB::create();
//创建描述子匹配器,用于根据描述子产生匹配
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");
//第一步:检测角点
detector->detect(img_1, keypoints_1);
detector->detect(img_2, keypoints_2);
//第二步:根据角点位置计算描述子
descriptor->compute(img_1, keypoints_1, descriptors_1);
descriptor->compute(img_2, keypoints_2, descriptors_2);
//第三步:根据描述子进行匹配,这里使用的是Hamming距离
vector<DMatch> matches;
matcher->match(descriptors_1, descriptors_2, matches);
//第四步:对匹配点进行筛选
//筛选前需要找出最大距离和最小距离,也就是最不相似和最相似的匹配
//首先定义两个最值,初值设置为第一个match的距离
double min_dist = matches[0].distance;
double max_dist = matches[0].distance;
for(auto m:matches)
{
if (m.distance<min_dist) min_dist = m.distance;
if (m.distance>max_dist) max_dist = m.distance;
}
cout<<"Max_dist:"<<max_dist<<endl
<<"Min_dist:"<<min_dist<<endl;
//筛选:当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限
for(auto m:matches)
{
if (m.distance <= max(2*min_dist, 30.0))
matches_good.push_back(m);
}
}
//将像素坐标转换成相机归一化平面坐标
Point2d pixel2cam ( const Point2d& p, const Mat& K )
{
//定义相机归一化坐标
Point2d camera_canonical_coordinates;
//设定坐标值
camera_canonical_coordinates.x = (p.x-K.at<double>(0,2))/K.at<double>(0,0);
camera_canonical_coordinates.y = (p.y-K.at<double>(1,2))/K.at<double>(1,1);
//返回
return camera_canonical_coordinates;
//其实简单就是用下面一句,直接用像素坐标和内参矩阵构造一个2d点,直接返回。
//return Point2d((p.x-K.at<double>(0,2))/K.at<double>(0,0), (p.y-K.at<double>(1,2))/K.at<double>(1,1));
}
//SVD方法求位姿估计
void pose_estimation_3d3d(
const vector<Point3f>& pts1,
const vector<Point3f>& pts2,
Mat& R, Mat& t
)
{
//质心
Point3f p1, p2;
//空间点个数N
int N = pts1.size();
//遍历空间点,将点求和,这里求和就是XYZ坐标对应的相加起来(两个空间点相加也就是这么算的)
for ( int i=0; i<N; i++ )
{
p1 += pts1[i];
p2 += pts2[i];
}
//前面p1、p2已经计算出来一个总和值,下面要除以个数,这里先将p1、p2转换成Vec3f类型,再除以N。
//是因为Point3f类型没有除法运算?
p1 = Point3f( Vec3f(p1) / N);
p2 = Point3f( Vec3f(p2) / N);
//创建去质心后的3d点数组
vector<Point3f> q1(N), q2(N);
//去质心运算
for ( int i=0; i<N; i++ )
{
q1[i] = pts1[i]-p1;
q2[i] = pts2[i]-p2;
}
// 根据P174页下方总结,计算 W = sum(q1*q2^T)
//首先定义W,类型为Eigen::Matrix3d,并初始化为0矩阵
Eigen::Matrix3d W = Eigen::Matrix3d::Zero();
for ( int i=0; i<N; i++ )
{
W += Eigen::Vector3d(q1[i].x, q1[i].y, q1[i].z)*Eigen::Vector3d(q2[i].x, q2[i].y, q2[i].z).transpose();
}
cout<<"W="<<W<<endl;
// 将W进行UV分解,注意用法,这里是Eigen库中的方法,创建一个JacobiSVD模板类对象,模板参数为Eigen::Matrix3d类型
Eigen::JacobiSVD<Eigen::Matrix3d> svd(W, Eigen::ComputeFullU|Eigen::ComputeFullV);
Eigen::Matrix3d U = svd.matrixU();
Eigen::Matrix3d V = svd.matrixV();
cout<<"U="<<U<<endl;
cout<<"V="<<V<<endl;
Eigen::Matrix3d R_ = U*(V.transpose());
Eigen::Vector3d t_ = Eigen::Vector3d(p1.x, p1.y, p1.z)-R_*Eigen::Vector3d(p2.x, p2.y, p2.z);
// convert to cv::Mat将Eigen::Matrix3d类型的R_转换成Mat类型的R,这里看起来有点啰嗦啊
R=(Mat_<double>(3, 3)<<
R_(0, 0), R_(0, 1), R_(0, 2),
R_(1, 0), R_(1, 1), R_(1, 2),
R_(2, 0), R_(2, 1), R_(2, 2)
);
t = (Mat_<double>(3, 1)<<t_(0, 0), t_(1, 0), t_(2, 0));
}
void bundleAdjustment (
const vector< Point3f >& pts1,
const vector< Point3f >& pts2,
Mat& R, Mat& t )
{
// 初始化g2o
//定义矩阵块求解器的类型,这里pose维度为 6, landmark 维度为 3。trait:特性
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block;
//定义线性方程求解器,用于后面的矩阵块求解器构造
Block::LinearSolverType* linearSolver = new g2o::LinearSolverEigen<Block::PoseMatrixType>();
//定义矩阵块求解器,用于后面的优化算法构造
Block* solver_ptr = new Block( linearSolver );
//定义优化算法,这里用的是LM方法
g2o::OptimizationAlgorithmGaussNewton* solver = new g2o::OptimizationAlgorithmGaussNewton( solver_ptr );
//创建一个优化器,传入上方定义的solver
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm( solver );
// Vertex 定义顶点,只有一个顶点就是相机pose。
g2o::VertexSE3Expmap* pose = new g2o::VertexSE3Expmap(); // camera pose
pose->setId(0);
//这里相当于给了一个初始值,旋转为单位阵,平移为0向量
pose->setEstimate(g2o::SE3Quat(Eigen::Matrix3d::Identity(), Eigen::Vector3d(0, 0, 0)));
optimizer.addVertex(pose);
// edges 定义边,这里边就很多了,因为一张图上有很多匹配点,也就有很多观测值和误差项,
int index = 1;
//这里用到了上方定义的边的类型
vector<EdgeProjectXYZRGBDPoseOnly*> edges;
//for循环加进去
for ( size_t i=0; i<pts1.size(); i++ )
{
//边类型指针
EdgeProjectXYZRGBDPoseOnly* edge = new EdgeProjectXYZRGBDPoseOnly(Eigen::Vector3d(pts2[i].x, pts2[i].y, pts2[i].z) );
//每个边设置一个ID
edge->setId(index);
//设定链接的顶点,这里是一元边,所以一条边只链接一个顶点,而且整个图也就一个顶点,就是位姿pose
edge->setVertex(0, dynamic_cast<g2o::VertexSE3Expmap*> (pose));//这里这个类型转换貌似有点多余,因为pose类型本身就是g2o::VertexSE3Expmap*类型
//设定测量值,也就是第一组3d点,pts1
edge->setMeasurement(Eigen::Vector3d(pts1[i].x, pts1[i].y, pts1[i].z));
//设置信息矩阵,不知道为啥要*1e4
edge->setInformation(Eigen::Matrix3d::Identity()*1e4);
//添加到优化器中
optimizer.addEdge(edge);
index++;
edges.push_back(edge);
}
//优化过程信息输出设定为true
optimizer.setVerbose( true );
//开始优化
optimizer.initializeOptimization();
//优化次数10次
optimizer.optimize(10);
//优化结果输出
cout<<endl<<"after optimization:"<<endl;
cout<<"T="<<endl<<Eigen::Isometry3d( pose->estimate() ).matrix()<<endl;
return;
}
3d3d
最新推荐文章于 2023-06-14 02:39:09 发布