How do I rotate an image?

后端 未结 4 1286
悲&欢浪女
悲&欢浪女 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 07:54

    Note there's another solution apart from rotation matrices, that doesn't loose image information through aliasing. You can separate 2D image rotation into skews and scalings, which preserve the image quality.

    Here's a simpler explanation

    0 讨论(0)
  • 2021-02-08 08:04

    It seems like the example you've provided is some edge detection kernel. So if what you want to is detect edges of different angles you'd better choose some continuous function (which in your case might be a parametrized gaussian of x1 multiplied by x2) and then rotate it according to formulae provided by kigurai. As a result you would be able to produce a diskrete kernel more efficiently and without aliasing.

    0 讨论(0)
  • 2021-02-08 08:10

    To rotate an image, you create 3 points:

    A----B 
    |
    |
    C
    

    and rotate that around A. To get the new rotated image you do this:

    • rotate ABC around A in 2D, so this is a single euler rotation
    • traverse in the rotated state from A to B. For every pixel you traverse also from left to right over the horizontal line in the original image. So if the image is an image of width 100, height 50, you'll traverse from A to B in 100 steps and from A to C in 50 steps, drawing 50 lines of 100 pixels in the area formed by ABC in their rotated state.

    This might sound complicated but it's not. Please see this C# code I wrote some time ago: rotoZoomer by me

    When drawing, I alter the source pointers a bit to get a rubber-like effect, but if you disable that, you'll see the code rotates the image without problems. Of course, on some angles you'll get an image which looks slightly distorted. The sourcecode contains comments what's going on so you should be able to grab the math/logic behind it easily.

    If you like Java better, I also have made a java version once, 14 or so years ago ;) -> http://www.xs4all.nl/~perseus/zoom/zoom.java

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题