1

I had a question about the implementation of scipy's ndimage.rotate method, specifically relating to the way it actually works when rotating an array by an arbitrary amount, and whether it preserves the numbers contained within the array?

I am attempting to find a way to rotate some arrays whilst also preserving the numbers in the array and not loosing any information.

I have attempted to test it by initializing some arrays with known shapes inside it, but I have noticed that if you initialize some specific shapes, such as crosses, then when you rotate by a certain amount, the cross will become a square, and I am concerned that I am loosing information here.

matr_1= [[0, 1, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0],
    [0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0],
    [0, 0, 0, 1, 1, 1],
    [0, 0, 0, 0, 1, 0]]

 matr_2= [[0, 0, 1, 0, 0, 0],
    [0, 1, 1, 1, 0, 0],
    [0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 0],
    [0, 0, 0, 1, 1, 1],
    [0, 0, 0, 0, 1, 0]]

plt.imshow(matr_1)

plt.show()

print("Rotation by 45 degrees")

print(sp.ndimage.rotate(matr_1, 45, reshape=False)) 

plt.imshow(sp.ndimage.rotate(matr_1,45,reshape=False))

plt.show()

print("Rotation by 55 degrees")

print(sp.ndimage.rotate(matr_2, 90, reshape=False))

plt.imshow(sp.ndimage.rotate(matr_1,90,reshape=False))

I understand that this might be an issue inherent to all rotation algorithms, but I want to understand what operations are actually being applied to the arrays.

1
  • Rotation an image by anything other than a multiple of 90° will always result in some loss. Commented Apr 10, 2019 at 3:53

2 Answers 2

1

It's good exercise to read the code of an open-source library you're using when in doubt. Here's the link:

https://github.com/scipy/scipy/blob/v1.2.1/scipy/ndimage/interpolation.py#L634

You'll notice that the rotation is defined by a rotation matrix, here defined as:

    m11 = math.cos(angle)
    m12 = math.sin(angle)
    m21 = -math.sin(angle)
    m22 = math.cos(angle)
    matrix = numpy.array([[m11, m12],
                          [m21, m22]], dtype=numpy.float64)

in discrete images, loss is invetiable for angles that aren't multiples of 90.

Sign up to request clarification or add additional context in comments.

Comments

0

It is possible to rotate an image and use Nearest Neighbour Interpolation. That means that rather than taking a calculated percentage of each of the neighbouring pixels for each location in the new raster (grid), the algorithm is constrained to pick the nearest neighbour from the existing raster.

This means that no new pixel values (essentially colours or shades of grey) will be introduced into the image. That is, if you started with a white rectangle on a black background, no matter how many degrees you rotate it, you will never get a single, new grey pixel to smooth a jaggy edge - all output pixels will continue to be black or white.

I am uncertain of the exact maths, but I don't believe this means the exact number of black and white pixels in the input image will be preserved, but the values will be preserved insofar as no new, different values will be introduced.

I can't tell from the way your question is worded if this is an option for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.