4
$\begingroup$

I have a warping grid, that has to be applied to an image. When I apply the grid, I get the results I expect.

Now, I want to apply the same grid to the same image, but the image is modified in the following way: it is rotated 270 degrees counterclockwise.

The grid is stored in memory as an array of points. It is [-1, -1] - [1, 1] normalized. I am trying to rotate the whole thing around the origin, that is [0, 0] id est the center of the image. Also, I am trying to rotate in discrete steps of 90 degrees.

P1 P2 P3 

P4 P5 P6

P7 P8 P9

These are destination coordinates of the points in the image. For example, the lower right corner of the input image has coordinates of [1,1]. In the output image, this point is moved to [P9.x, P9.y] (a.k.a. forward mapping).

I transpose the matrix:

P1 P4 P7

P2 P5 P8

P3 P6 P9

Then I apply to every point the transformation

x = y'

y = -x',

where ' denotes old coordinate and lack of prime denotes new coordinate. I apply the derived grid to the rotate

However, I do not get the expected warp. This task seemed incredibly simple to me, but I find myself struggling with the strange behaviour for a third day. Could someone point out my mistake or a procedure to get things right?

$\endgroup$
  • $\begingroup$ Welcome to DSP.SE! Are you trying to rotate the grid about the origin? $\endgroup$ – Phonon Jun 26 '12 at 13:45
  • $\begingroup$ @Phonon, thanks for the wellcoming, man!Yes, I am trying to rotate the grid around the origin i.e. it's center, in discrete 90-degree steps. I hope this question is on-topic in this SE forum. $\endgroup$ – Vorac Jun 26 '12 at 13:52
  • $\begingroup$ I don't think you need to transpose. Your coordinate change should be sufficient. $\endgroup$ – Phonon Jun 26 '12 at 13:59
  • $\begingroup$ Well, not transposing the grid causes the following: after warpig the image, it becomes rotated to zero rotation (was 270). I want the image to sustain it's orientation. Of course, I could be making a programming mistake somewhere. $\endgroup$ – Vorac Jun 26 '12 at 14:24
  • $\begingroup$ I do the transposition so that the computer reads out the points in the correct order. The warper expects the first point to correspond to [-1, -1] and the last point to correspond to [1, 1]. $\endgroup$ – Vorac Jun 26 '12 at 14:27
1
$\begingroup$

In two dimensions, to rotate a point by a certain angle you need to multiply that point by a rotation matrix of the form:

$ \left[\begin{array}{c}x' \\y' \end{array}\right] = \left[\begin{array}{cc}\cos\theta & -\sin\theta \\\sin\theta & \cos\theta\end{array}\right] \left[\begin{array}{c}x \\y \end{array}\right] $

Where $(x, y)$ is the old coordinate, $(x', y')$ is the new coordinate, and $\theta$ is the angle of (counterclockwise) rotation you want to apply.

Thus, for a rotation of 270 degrees, the matrix you apply is:

$ \left[\begin{array}{c}x' \\y' \end{array}\right] = \left[\begin{array}{cc}\cos(270)& -\sin(270) \\\sin(270) & \cos(270)\end{array}\right]\left[\begin{array}{c}x \\y \end{array}\right] = \left[\begin{array}{cc}0& 1 \\-1 & 0\end{array}\right]\left[\begin{array}{c}x \\y \end{array}\right] = \left[\begin{array}{c}y \\-x \end{array}\right] $

There is a comprehensive wikipedia page on rotation matrices, explaining them in more detail, extending them to 3D, and sumarising some of their properties.

$\endgroup$
  • $\begingroup$ Yeah, that's how I derived the "x =; y = " part of the conversion in my answer. However this also rotates the warped image, and I don't want that. $\endgroup$ – Vorac Jun 27 '12 at 12:49
  • $\begingroup$ Perhaps it's unclear what you were asking. I thought you had a set of coordinates describing your grid, and you wanted to transform those coordinates according to a rotation? $\endgroup$ – Chris Jun 27 '12 at 12:52
  • $\begingroup$ Basically, I needed to apply a grid to an image, but the image can be given rotated and the grid should be applied the same way, without changing the orientation of the image, just warping it. I commited a minor edit to the headline. If you feel you can edit my question to express it more clearly, please, by all means, do so! $\endgroup$ – Vorac Jun 27 '12 at 13:04
  • $\begingroup$ Could you add a couple of images? For instance, the original image and the expected result? Also, do you know the angle the image has been rotated by? $\endgroup$ – Chris Jun 27 '12 at 13:13
1
$\begingroup$

After three and a half days of cursing, a couple of beers with a friend helped solve the problem. My mistake was that the reordering of points needed to be not exactly transpose, but close. So:

To rotate a grid 90 degrees counter-clockwise:

  • transpose the grid and reflect it along the horizontal central line
  • apply the transformation

x = y'

y = -x',

where primes denote old coordinates.

To rotate a grid 180 degrees

  • reflect the grid around its horizontal and around it's vertical axis
  • apply the transformation

x = - x'

y = - y'

To rotate a grid 270 degrees:

  • transpose the grid and reflect it along the vertical central line
  • apply the transformation

x = - y'

y = x'

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.