Affine transformations. Reflection

59 Views Asked by At

I'm trying to develop a programme, which transforms a 2d image. I'm trying to create a simple reflection.

Based on a theory affine transformation is determined as follows: Given a transformation matrix $$ A=\begin{bmatrix}\theta_1&\theta_2&s_1\\\theta_3&\theta_4&s_2\end{bmatrix}$$ to apply affine transform, we should take the source coordinates and make a matrix multiplication like this $$x_{new}, y_{new} = A*\begin{bmatrix}x_1\\x_2\\1\end{bmatrix}$$ Then we take pixels from original image by coordinates $x_1,x_2$ and input them in the new coordinates $x_{new}, y_{new}$.

Suppose I want to make a simple reflection on y-axis. Input image is 2d matrix $$image=\begin{bmatrix}1&0&2\\1&0&2\\1&0&2\end{bmatrix}$$ The affine matrix corresponding to reflection is $$A=\begin{bmatrix}1&0&0\\0&-1&0\end{bmatrix}$$

The corresponding coordinates matrix is \begin{bmatrix}(0,0)&(0,1)&(0,2)\\(1,0)&(1,1)&(1,2)\\(2,0)&(2,1)&(2,2)\end{bmatrix} When I try to reflect the coordinates in the first column, by the formula above I get $$(0,0):x_{new}, y_{new} = \begin{bmatrix}1&0&0\\0&-1&0\end{bmatrix}*\begin{bmatrix}0\\0\\1\end{bmatrix} =0, 0 $$ $$(1,0):x_{new}, y_{new} = \begin{bmatrix}1&0&0\\0&-1&0\end{bmatrix}*\begin{bmatrix}1\\0\\1\end{bmatrix} =1, 0 $$ $$(2,0):x_{new}, y_{new} = \begin{bmatrix}1&0&0\\0&-1&0\end{bmatrix}*\begin{bmatrix}2\\0\\1\end{bmatrix} =2, 0 $$ So the coordinates of the first column are not reflected at all around y-axis, as for other two columns they are reflected with each other.

def warpAffine(image, M):
    rows, cols = image.shape
    output_shape = image.shape
    out_rows, out_cols = image.shape

    output = np.zeros(output_shape, dtype=image.dtype)

    for i in range(out_rows):
        for j in range(out_cols):
            pixel =  image[i, j]
            coords = [i, j, 1]
            i_1, j_1 = np.dot(M, coords).astype(int)
            
            output[i_1, j_1] = pixel

    return output

Why the first column is not reflected? What am I missing?