emacs-devel
[Top][All Lists]

## Re: Image transformations

 From: Alp Aker Subject: Re: Image transformations Date: Thu, 13 Jun 2019 05:19:52 -0400

On Thu, Jun 13, 2019 at 1:41 AM Eli Zaretskii <address@hidden> wrote:
>
> This already goes contrary to my geometric intuition, please bear with
> me.  The rotation is around the (0,0) origin, i.e. around the top-left
> corner of the original image, right?  If so, the rotation should have
> been followed by a translation along the X axis, not Y

The last sentence is problematic.  When a transformation matrix describes a
rotation followed by a translation, the translation is specified relative to the
fixed coordinate axes.  The rotation doesn't affect the direction of the
translation.  It's perhaps unintuitive, but that's the way the formalism is
defined.  We could interpret "rotate 90 then translate" in the way you describe,
but then the translation into algebra would be different.

> rotating a 333-pixel wide, 233-pixel high image 90 deg clockwise
> produces a 233-pixel wide, 333-pixel high image that is entirely to
> the LEFT of the Y axis.  Here's ASCII-art representation of that:
>
>     +------------------+> X        +----------+-------------------> X
>     |                  |           |          |
>     |                  |           |          |
>     |                  |           |          |
>     |                  |   ===>    |          |
>     +------------------+           |          |
>     |                              |          |
>     |                              |          |
>     |                              |          |
>     |                              +----------+
>     |                                         |
>     V                                         V
>     Y                                         Y
>
> The above is just after the rotation around (0,0).  Is that correct,
> or am I missing something?

That's correct.

> I also tried to approach this from the matrix notation aspect.  Is the
> following the correct equations of computing (x',y'), the new
> coordinates of any pixel of the image, from its original coordinates
> (x,y)?
>
>   x' = m11 * x + m12 * y + tx
>   y' = m21 * x + m22 * y + ty
>
> where the factors are related to the matrix as follows:
>
>    m[0][0] = m11 | m[0][1] = m12 | m[0][2] = 0
>    --------------+---------------+-------------
>    m[1][0] = m21 | m[1][1] = m22 | m[1][2] = 0
>    --------------+---------------+-------------
>    m[2][0] = tx  | m[2][1] = ty  | m[2][2] = 1

I confess I'm not sure how to interpret that matrix.  I just looked through
image_set_rotation and found it somewhat confusing, as it seems to use
column-major representation where I'd expect row-major.  E.g., the above matrix
looks odd to me, because tx and ty would normally be in m[0][2] and m[1][2] (and
I'd expect m[2][0] == m[2][1] == 0).  Similarly, the rotation matrix used in
image_set_rotation:

[0][0] = cos_r, [0][1] = -sin_r
[1][0] = sin_r, [1][1] = cos_r

would normally describe a counter-clockwise rotation by r, not a clockwise
rotation.

That said, if I correctly understand the layout of the data, the equations
should be:

x' = m11 * x + m21 * y + tx
y' = m12 * x + m22 * y + ty

> If the above is correct, then the transformation of the top-left
> corner of the original image, whose original coordinates are (0,0),
> [...]
> the correct coordinates should be (233,0), not (0,232).
> What am I missing here?

The transformation described by the matrix is: rotate 90 degrees around the
origin, then translate by 232 along the y axis.  The first operation leaves (0,
0) unmoved, then the second operation moves it to (0, 232).

> Sorry, now I'm even more confused: aren't we dealing with affine
> transformations?  Then how are homogeneous coordinates related to
> this?  And does that mean the formulae for calculating (x',y') I show
> above are incorrect?

I was being unnecessarily general, which caused confusion; my
apologies.  Probably best for present purposes to ignore what I said
there.  (What I meant: Transformation matrices can be used for both affine and
non-affine transformations.  I first described the general case, then described
how the calculations work when we restrict to affine transformations.  It's
homogeneous coordinates in both cases, though.  I also wrote this last part
before noticing the transposition issue I mentioned above, which probably adds
to the confusion.)