Towards the Theory of Stereo Rectification
[This is an original blog, not citation]
「このブロクについて全ては枠主の原作です、ご注意ください!」
Visit My GitHub
For stereo matching, rectification is required, however, it will be confucious to complete this task using opencv.
Calibration and rectification are implemented in opencv by a wierd manner: the projection matrix of \(3\times4\) elements are not used fully, only \(3\times3\) of the top-left submatrix is used to rectify the views. While, we can give it an explanation but the author does not give even a hint in the code, not to mention an explanation in the documents. Actually, only rotation matrix can be used in rectification, despite the fact that the translation vector exists between two views.
A translation vector contains offset in axis \(Z\), which is strongly related to the exact depth of point in real world. This is going beyond our control with only pixels on hand. We do not know the depth for each pixel with certainty, therefore, the projection matrix should not include any non-zero element on \(Z\) axis. Opencv adopted an approach to avoiding using Translation vector in its absolute scale, converting a single translation transform into two rotation matrices, one for each view.
This is how it is done:
- Retrieve \(R\) and \(\vec{t}\), satisfying \(P_2\equiv R\cdot P_1+\vec{t}\);
- Set the first rotation matrix for both view \(R_1=R^{-\frac{1}{2}}\), \(R_2=R^{\frac{1}{2}}\) to put two cameras parallel but not in the same plane;
- Set the second rotation matrix for both view \(R_3=R_y\cdot R_z\) to put tow cameras co-plannar, in which, \(R_y\) and \(R_z\) are respectively the corresponding rotation matrix along axis \(Y\) and \(Z\) to rotate the transformed vector of \(\vec{t'}=R_1\cdot\vec{t}\) to new axis \(X\) of left camera, the angles are easy to solve as: \(\cos\theta_y = \vec{t'}\cdot\vec{e_y}\) and \(\cos\theta_z = \vec{t'}\cdot\vec{e_z}\);
- Apply \(R_1\cdot R_3\) to left camera, apply \(R_2\cdot R_3\) to the right.
Decompose a rotation matrix into 3 rotations along \(X\), \(Y\), and \(Z\) axis
\[R_x =\left[\begin{matrix} 1 & 0 & 0\\0 & \cos\phi & -\sin\phi\\0 & \sin\phi & \cos\phi \end{matrix}\right]\]
\[R_y =\left[\begin{matrix} \cos\theta & 0 & \sin\theta \\0 & 1 & 0\\-\sin\theta & 0 & \cos\theta \end{matrix}\right]\]
\[R_z =\left[\begin{matrix} \cos\psi & -\sin\psi & 0 \\ \sin\psi & \cos\psi & 0\\0 & 0 & 1 \end{matrix}\right]\]
The rotation matrix is compositional and non-sequential:
\[ \vec{r_x} =\left[\begin{matrix}\cos\theta \cos\psi\\ -\cos\theta \sin\psi\\ \sin\theta\end{matrix}\right]\]
\[ \vec{r_y} =\left[\begin{matrix}\sin\phi \sin\theta \cos\psi + \cos\phi \sin\psi\\ -\sin\phi \sin\theta \sin\psi + \cos\phi \cos\psi\\ -\sin\phi \cos\theta\end{matrix}\right]\]
\[\vec{r_z} =\left[\begin{matrix}-\cos\phi \sin\theta \cos\psi + \sin\phi \sin\psi\\ \cos\phi \sin\theta \sin\psi + \sin\phi \cos\psi\\ \cos\phi \cos\theta\end{matrix}\right]\]
\[R_xR_yR_z= \left[\begin{matrix} \vec{r_x}^T \\ \vec{r_y}^T \\ \vec{r_z}^T \end{matrix}\right]\tag{1}\]
Notice that the rotation matrix along axis is a perfect matrix, which means it meets plenty of standards and exhibits a lot of desirable features, such as its L2 norm being an unit. And if you try to inverse it, you will simply get its tranposed matrix. What's more, its components satisfy,
\[\vec{r_x}\cdot\vec{r_y}=0, \vec{r_y}\cdot\vec{r_z}=0, \vec{r_z}\cdot\vec{r_x}=0\]
that is:
\[\vec{r_x}\times\vec{r_y}=\vec{r_z}, \vec{r_y}\times\vec{r_z}=\vec{r_x}, \vec{r_z}\cdot\vec{r_x}=\vec{r_y}\]
These features enables the rotation matrix to be computed and used in a much, much, much easier way across geometric world.
So far so good.
But I am writing this blog only for testing the markdown grammar support, and check if \(\mathbf{LaTex}\) is supported fully.
While, btw, a sample code of python is tested, too.
# this is an example of code in python
def main():
print("hellow my lady!")
return
if __name__ == '__main__':
main()