openMVS:极线校正Fusiello

立体视觉入门指南(6):对级约束与Fusiello法极线校正

原理+代码实战 | 双目视觉中的极线校正

校正目的:对两幅图像的二维匹配搜索变成一维,节省计算量,排除虚假匹配点,成平行视图。

算法流程:

坐标系:平行视图的极点位于无穷远处,极线水平对准。

  • ​​​X轴:与基线平行 。r_x=(C_2-C_1)/||C_2-C_1||
  • Y轴:与X轴正交。在Fusiello法中k为旧的Z轴所表示的单位向量。r_{y}=k\times r_{x}
  • Z轴:r_{z}=r_{x}\times r_{y} 
  • 则旋转矩阵:R_{new}=\begin{bmatrix} r_{x}^{T} \\ r_{y}^{T} \\ r_{z}^{T} \end{bmatrix}
  • 则左右两个图像新的旋转矩阵:R_{lnew}=R_{new}R_{l}^T  R_{rnew}=R_{new}R_{r}^T

  • 新的内参矩阵:K_{new}=(K_{left}+K_{right})/2 ,且把倾斜因子s设置为0。
  • 原始的相对位姿:R_{lr}=R_{r}R_{l}^t          C_{lr}=R_{l}(C_{r}-C_{l})

\begin{equation} p_{l}=R_{l} P_{w}+t_{l{\color{Red} }}\\ p_{r}=R_{r} P_{w}+t_{r} \\ R_{lr}p_{l}+t_{lr}=p_{r} \\ R_{lr}(R_{l}P_{w}+t_{l})+t_{lr}=R_{r}P_{w}+t_{r}\\ \\ R_{lr}R_{l}=R_{r}\\ R_{lr}t_{l}+t_{lr}=t_{r}\\ t=-RC\\ \\ R_{lr}=R_{r}R_{l}^T\\ C_{l}+R_{l}^TC_{lr}=C_{r} \end{equation}

  • t=R_{rnew}(R_{lr}(-C_{lr}))
  • 新的基线:baseline=t.x
// compute relative pose of the given two cameras
template<typename TYPE>
inline void ComputeRelativeRotation(const TMatrix<TYPE,3,3>& Ri, const TMatrix<TYPE,3,3>& Rj, TMatrix<TYPE,3,3>& Rij) {
	Rij = Rj * Ri.t();
} // ComputeRelativeRotation
template<typename TYPE>
inline void ComputeRelativePose(const TMatrix<TYPE,3,3>& Ri, const TPoint3<TYPE>& Ci, const TMatrix<TYPE,3,3>& Rj, const TPoint3<TYPE>& Cj, TMatrix<TYPE,3,3>& Rij, TPoint3<TYPE>& Cij) {
	Rij = Rj * Ri.t();
	Cij = Ri * (Cj - Ci);
} // ComputeRelativePose
// see: "A compact algorithm for rectification of stereo pairs", A. Fusiello, E. Trucco, and A. Verri, 2000
// 极线校正
REAL Camera::StereoRectifyFusiello(const cv::Size& size1, const Camera& camera1, const cv::Size& size2, const Camera& camera2, Matrix3x3& R1, Matrix3x3& R2, Matrix3x3& K1, Matrix3x3& K2)
{
	// compute relative pose
	// 计算相对位姿
	RMatrix poseR;
	CMatrix poseC;
	ComputeRelativePose(camera1.R, camera1.C, camera2.R, camera2.C, poseR, poseC);

	// new x axis (baseline, from C1 to C2)
	// 新的x轴,基线方向
	const Point3 v1(camera2.C-camera1.C);
	// new y axes (orthogonal to old z and new x)
	// 新的y轴,垂直旧的Z轴(光轴)和新的X轴
	const Point3 v2(camera1.Direction().cross(v1));
	// new z axes (no choice, orthogonal to baseline and y)
	// 新的Z轴,垂直上面两个新轴
	const Point3 v3(v1.cross(v2));

	// new extrinsic (translation unchanged)
	// 新的外参,平移不变
	RMatrix R;
	R.SetFromRowVectors(normalized(v1), normalized(v2), normalized(v3));

	// new intrinsic (arbitrary)
	// 新的内参
	K1 = camera1.K; K1(0,1) = 0;
	K2 = camera2.K; K2(0,1) = 0;
	K1(1,1) = K2(1,1) = (camera1.K(1,1)+camera2.K(1,1))/2;

	// new rotations
	// 新的选择从校正前的相机坐标系转到校正后的相机坐标系
	R1 = R*camera1.R.t();
	R2 = R*camera2.R.t();

	#if 0
	// new projection matrices
	PMatrix P1, P2;
	AssembleProjectionMatrix(K1, R, camera1.C, P1);
	AssembleProjectionMatrix(K2, R, camera2.C, P2);

	// rectifying image transformation
	#if 0
	const Matrix3x3 H1((PMatrix::EMat(P1).leftCols<3>()*(PMatrix::EMat(camera1.P).leftCols<3>().inverse())).eval());
	const Matrix3x3 H2((PMatrix::EMat(P2).leftCols<3>()*(PMatrix::EMat(camera2.P).leftCols<3>().inverse())).eval());
	#else
	const Matrix3x3 H1(K1*R*camera1.R.t()*camera1.GetInvK());
	const Matrix3x3 H2(K2*R*camera2.R.t()*camera2.GetInvK());
	#endif
	#endif

	// 计算新的基线距离
	const Point3 t(R2 * (poseR*(-poseC)));
	ASSERT(ISEQUAL(-t.x, norm(v1)) && ISZERO(t.y) && ISZERO(t.z));
	return t.x;
} // StereoRectifyFusiello

 

【资源说明】 基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip 基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip 基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip 基于C++实现极线纠正+立体匹配源码(含超详细代码注释).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值