Search code examples
pythonrotationimage-rotationscikit-image

Invertable rotation of an image


I am looking for a rotational transform in python, that can be inverted to yield the original image. So far I am using

import skimage.transform as tf
import scipy
im = scipy.misc.ascent()

Original, unrotated image

r1 = tf.rotate(im, 10, mode='wrap')

Image rotated 10 degrees

r2 = tf.rotate(r1, -10, mode='wrap')

Rotated Image inversely rotated

If I do the same using reflect the result looks like

Result using 'reflect'

Is there a possibilty to simply rotate an image by angle and rotate the result back by -angle and end up with the original image?


Solution

  • A potential solution to your problem would be to use rotate with the optional argument resize set to True, and then to crop the final result.

    import skimage.transform as tf
    import scipy
    import matplotlib.pyplot as plt
    
    im = scipy.misc.ascent()
    
    r1 = tf.rotate(im, 10, mode='wrap', resize=True)
    plt.imshow(r1)
    
    r2 = tf.rotate(r1, -10, mode='wrap', resize=True)
    plt.imshow(r2)
    
    # Get final image by cropping
    imf = r2[int(np.floor((r2.shape[0] - im.shape[0])/2)):int(np.floor((r2.shape[0] + im.shape[0])/2)),int(np.floor((r2.shape[1] - im.shape[1])/2)):int(np.floor((r2.shape[1] + im.shape[1])/2))]
    
    plt.imshow(imf)
    

    There will be minor differences between the original and the image rotated twice due to the operations inside the rotation function, but to the eye it looks the same.