Skip to content

Latest commit

 

History

History
203 lines (101 loc) · 14.4 KB

homography.md

File metadata and controls

203 lines (101 loc) · 14.4 KB

1. Homography

Any two images of the same planar surface in space are related by a homography. The planar homography relates the transformation between two planes (up to a scale factor):

https://latex.codecogs.com/svg.latex?s \begin{bmatrix} x^{'} \\ y^{'} \\ 1 \end{bmatrix} = \mathbf{H} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & h_{33} \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

The homography matrix is a 3x3 matrix with 8 DoF as it is estimated up to a scale. It is generally normalized:

https://latex.codecogs.com/svg.latex?\left\{\begin{matrix} h_{33} = 1 \\ or \\ h_{11}^2 + h_{12}^2 + h_{13}^2 + h_{21}^2 + h_{22}^2 + h_{23}^2 + h_{31}^2 + h_{32}^2 + h_{33}^2 = 1
\end{matrix}\right.

Refs: 1, 2

2. Different Kinds of Transformation Related By Homography

The transformations shown in the followings instances are all related to transformations between two planes.

2.1 Planar Surface And The Image Plane

2.2 Planar Surface Viewed By Two Cameras

2.3 Rotating Camera Around Its Axis of Projection,

A rotating camera around its axis of projection, equivalent to consider that the points are on a plane at infinity (image taken from

3. Calculating Homography Matrix

For any point in the world the projection of the point on the camera plan would be: https://latex.codecogs.com/svg.latex?{\displaystyle  {\begin{bmatrix}u\\v\\w\end{bmatrix}}=K\,{\begin{bmatrix}r_{11} & r_{12} & r_{13} &t_{1} \\ r_{21} & r_{22} & r_{23} &t_{2} \\ r_{31} & r_{31} & r_{31} &t_{3} \end{bmatrix}}{\begin{bmatrix}X_{w}\\Y_{w}\\Z_{w}\\W_{w}\end{bmatrix}} =K\,{\begin{bmatrix}R_{w}^{c} &T_{w}^{c}\end{bmatrix}}{\begin{bmatrix}X_{w}\\Y_{w}\\Z_{w}\\W_{w}\end{bmatrix}}=P{\begin{bmatrix}X_{w}\\Y_{w}\\Z_{w}\\W_{w}\end{bmatrix}}}



now if we put world reference frame on the plane such that X and Y axis lays on the plane:

https://latex.codecogs.com/svg.latex?\begin{bmatrix}u\\v\\w\end{bmatrix}=\begin{bmatrix} p_{11} & p_{12} & p_{13} &p_{14}
\\p_{21} & p_{22} & p_{23} &p_{24} \\ p_{31} & p_{32} & p_{33} &p_{34} \end{bmatrix} \begin{bmatrix}X_{w}\\Y_{w}\\Z_{w}\\W_{w}\end{bmatrix}

so all point the Z will be zero:

https://latex.codecogs.com/svg.latex?\begin{bmatrix}u\\v\\w\end{bmatrix}=\begin{bmatrix} p_{11} & p_{12} & 0 &p_{14}
\\ p_{21} & p_{22} & 0 &p_{24} \\ p_{31} & p_{32} & 0 &p_{34} \end{bmatrix}\begin{bmatrix}X_{w}\\Y_{w}\\0\\W_{w}\end{bmatrix}



https://latex.codecogs.com/svg.latex?\begin{bmatrix}u\\v\\w\end{bmatrix}= \begin{bmatrix} p_{11} & p_{12}  &p_{14} \\p_{21} & p_{22}  &p_{24} \\ p_{31} & p_{32}  &p_{34} \end{bmatrix} \begin{bmatrix}X_{w}\\Y_{w}\\W_{w}\end{bmatrix}



https://latex.codecogs.com/svg.latex?\begin{bmatrix}u\\v\\w\end{bmatrix}=\begin{bmatrix} H_{11} & H_{12}  &H_{13} \\ H_{21} & H_{22}  &H_{23} \\ H_{31} & H_{32}  &H_{33} \end{bmatrix} \begin{bmatrix}X_{w}\\Y_{w}\\W_{w}\end{bmatrix}



https://latex.codecogs.com/svg.latex?\begin{bmatrix}u_1\\v_1\\w_1\end{bmatrix}=
H_{1}
\begin{bmatrix}X_{w}\\Y_{w}\\W_{w}\end{bmatrix}



https://latex.codecogs.com/svg.latex?\begin{bmatrix}u_2\\v_2\\w_2\end{bmatrix}=H_{2}H^{-1}_{1}\begin{bmatrix}u_{1}\\v_{1}\\w_{1}\end{bmatrix}



https://latex.codecogs.com/svg.latex?\begin{bmatrix}u_2\\v_2\\w_2\end{bmatrix}= H \begin{bmatrix}u_{1}\\v_{1}\\w_{1}\end{bmatrix}



\\ \text{if we set:} \\ w_1=1 \\ w_2=1 \\ \text{which gives us:} \\ \\ \left\{\begin{matrix} u_1 =   \frac{H_{11} u_1 + H_{12} v_1 + H_{13}}{H_{31} u_1 + H_{32} v_1 + H_{33}}  \\ \\ v_1 =    \frac{H_{21} u_1 + H_{22} v_1 + H_{23}}{H_{31} u_1 + H_{32} v_1 + H_{33}} \end{matrix}\right.



or:



https://latex.codecogs.com/svg.image?\left\{\begin{matrix}
xp_n =   \frac{H_{11} x_n + H_{12} y_n + H_{13}}{H_{31} x_n + H_{32} y_n + H_{33}} 
\\ 
\\ 
yp_n =    \frac{H_{21} x_n + H_{22} y_n + H_{23}}{H_{31} x_n + H_{32} y_n + H_{33}}  
\end{matrix}\right.



if we write the upper equation for 4 points and rewrite them we would get the following linear equation to solve:






Singular-value Decomposition (SVD) of any given matrix




is the last column of

OpenCV API

To find homography Matrix from 4 Corresponding Points:

cv::Mat homographyMatrix= cv::getPerspectiveTransform(point_on_plane1,point_on_plane2);
cv::Mat H = cv::findHomography(  point_on_plane1,point_on_plane2,0 );

If you need to perform the Homography matrix transformation on points:

cv::perspectiveTransform 

If you want to transform an image using perspective transformation, use:

cv::warpPerspective

The function cv::warpPerspective transforms the source image using the specified matrix:



Apply homography on image,
Finding homography matrix from 4 corresponding points
Finding homography Matrix between two images using keypoints and RANSAC

Decompose Homography Matrix

Refs: 1