Why does this composition of affine transformations not behave as expected?

142 Views Asked by At

I am learning affine transformations for applications in computer vision. I seem to be getting the hang of it, but I ran into an unexpected result that I can't explain.

I have a $100 \times 100$ pixel image of a square. I know that the following matrix, $S$, scales the image proportional to $a$, with the image anchored at the top left corner:

$$ S_a = \begin{pmatrix} a & 0 & 0 \\ 0 & a & 0 \\ 0 & 0 & 1 \end{pmatrix} $$

To scale the image anchored at the center, I can translate the image, scale it, and then translate back:

$$ S'_a = \begin{pmatrix} 1 & 0 & 49.5 \\ 0 & 1 & 49.5 \\ 0 & 0 & 1 \end{pmatrix} S_a \begin{pmatrix} 1 & 0 & -49.5 \\ 0 & 1 & -49.5 \\ 0 & 0 & 1 \end{pmatrix} = \begin{pmatrix} a & 0 & -49.5 \\ 0 & a & -49.5 \\ 0 & 0 & 1 \end{pmatrix} $$

The problem comes when I try to do the transformation $S'_aS_{1/a}$. Let $a = 2$. My expectation is to have the image resulting from the transformation $S'_2$ squished towards the top left corner, just as the transformation $S_{1/2}$ does on its own. However, what seems to happen is that the image is first translated to the top left and then scaled, causing three quarters of the original image to be lost outside the boundaries of the new image.

enter image description here

Evidently there's something I don't understand about affine transformations, but I have not been able to figure out what that is.

1

There are 1 best solutions below

1
On BEST ANSWER

Note that for $a = 2$, we have that $$S'_aS_{1/a} = \left(\begin{matrix}2 & 0 & -49.5 \\ 0 & 2 & -49.5 \\ 0 & 0 & 1\end{matrix}\right)\left(\begin{matrix}1/2 & 0 & 0 \\ 0 & 1/2 & 0 \\ 0 & 0 & 1\end{matrix}\right) = \left(\begin{matrix}1 & 0 & -49.5 \\ 0 & 1 & -49.5 \\ 0 & 0 & 1\end{matrix}\right).$$

Thus all this tranformation does is translate the image so that the center is in the top-left corner.

I believe the misunderstanding is the order in which the affine transformations are applied. Note that affine transformations are applied from right-to-left, that is $S_{1/a}$ is applied first, then $S'_a$.

If we reverse the order, which seems to be your intent, we find $$ S_{1/a}S'_a = \left(\begin{matrix}1 & 0 & -24.75 \\ 0 & 1 & -24.75 \\ 0 & 0 & 1\end{matrix}\right), $$

which does what you expect.