I am trying to write an algorithm to rotate a bitmap image of $n$ by $n$ size by an angle $\alpha$.

I know that I have to find a rotation matrix, then perform matrix multiplication of the rotation matrix by the image matrix data input.

I know that the $2\times 2$ rotation matrix is

$$T_\alpha=\left(\begin{array}{cc}\cos\alpha & -\sin\alpha \\ \sin \alpha & \cos \alpha \end{array}\right)$$

However, I am not sure how to find the appropriate $n \times n$ matrix.

  • 2
    $\begingroup$ @CheungJoonHee Not a duplicate. The O.P. isn’t looking for rotations in an $n$-dimensional vector space. The problem here is to rotate an $n\times n$ pixel array. $\endgroup$ – amd Apr 30 at 4:57
  • 1
    $\begingroup$ You’re confusing the size of the image pixel array with the dimension of a vector space. You need an algorithm to rotate a pixel array. Try stackoverflow.com/q/484573 for suggestions. $\endgroup$ – amd Apr 30 at 4:59
  • 1
    $\begingroup$ This is a 2-d rotation. You need to pick an origin and decide how to map the resulting rotated pixels into another $n \times n$ array. $\endgroup$ – copper.hat Apr 30 at 5:00

Let $w, h$ be the width & height of the image in pixels, but promote them to floats for further computation.

You take the image center $c = (w, h)/2$

Then treat each pixel coordinate as a coordinate with origin at $c$. In other words subtract $c$ from each pixel coordinate $v' = (x,y) - c$.

Now as you have the rotation matrix, you need only convert $v'$ to a column vector and multiply with $v'$ on the right.

If your rotation matrix was constructed using the right hand rule. And your image space (in whatever coding environment) originally started with $(0,0)$ at the bottom left, then this will rotate the image around its center $\alpha$ radians counter clockwise.

As far as final image quality though, this is probably bad once you discretize into a new image. This is why you should use something already coded.


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.