magpack.image_utils.get_perspective_matrix#

get_perspective_matrix(source, destination)#

Provides the non-affine matrix that maps four points on the source image to the destination image.

Parameters:
sourcearray_like, tuple

Array of four pairs of coordinates from source. [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]

destinationarray_like, tuple

Array of four pairs of destination coordinates. [[x'1,y'1], [x'2,y'2], [x'3,y'3], [x'4,y'4]]

Returns:
np.ndarray

Non-affine transformation matrix that maps the points from the source to the destination.

Notes

The expressions describing the map between the source (x, y) and destination image (X, Y) are:

\[X = \frac{m_{11}x + m_{12}y + m_{13}}{m_{31}x + m_{32}y + 1}, \quad Y = \frac{m_{21}x + m_{22}y + m_{23}}{m_{31}x + m_{32}y + 1}\]

with eight unknowns, the matrix elements \(m_{11}, m_{12}, ..., m_{33}\) can be re-labeled as \(m_{1}, m_{2}, ..., m_{8}\). By forming four pairs of simultaneous equations, these elements can be determined:

\[\begin{split}X = m_{1}x + m_{2}y + m_{3} - m_{7}xX - m_{8}yX + 1 \\ Y = m_{4}x + m_{5}y + m_{6} - m_{7}xY - m_{8}yY + 1\end{split}\]

The equations are solved using linear algebra to calculate the perspective matrix.