How do I rotate an image?

后端 未结 4 1292
悲&欢浪女
悲&欢浪女 2021-02-08 07:46

See also: Why is my image rotation algorithm not working?

This question isn\'t language specific, and is a math problem. I will however use

4条回答
  •  故里飘歌
    2021-02-08 08:11

    The usual way to solve this is by doing it backwards. Instead of calculating where each pixel in the input image ends up in the output image, you calculate where each pixel in the output image is located in the input image (by rotationg the same amount in the other direction. This way you can be sure that all pixels in the output image will have a value.

    output = new Image(input.size())
    
    for each pixel in input:
    {
      p2 = rotate(pixel, -angle);
      value = interpolate(input, p2)
      output(pixel) = value
    }
    

    There are different ways to do interpolation. For the formula of rotation I think you should check https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

    But just to be nice, here it is (rotation of point (x,y) angle degrees/radians):

     newX = cos(angle)*x - sin(angle)*y
     newY = sin(angle)*x + cos(angle)*y
    

提交回复
热议问题