How to rotate a 3D image by a random angle in python


I’m using a set of 32x32x32 grayscale images and I want to apply random rotations on the images as a part of data augmentation while training a CNN by tflearn + tensorflow. I was using the following code to do so:

    # Real-time data preprocessing
    img_prep = ImagePreprocessing()

    # Real-time data augmentation
    img_aug = ImageAugmentation()

    # Input data
    with tf.name_scope('Input'):
        X = tf.placeholder(tf.float32, shape=(None, image_size, 
    image_size, image_size, num_channels), name='x-input')
        Y = tf.placeholder(tf.float32, shape=(None, label_cnt), name='y-input')

    # Convolutional network building
    network = input_data(shape=[None, 32, 32, 32, 1],
                 placeholder = X,

(I’m using a combination of tensorflow and tflearn to be able to use the features from both, so please bear with me. Let me know if something is wrong with the way I’m using placeholders, etc.)

I found that using the add_random_rotation (which itself uses scipy.ndimage.interpolation.rotate) treats the third dimension of my grayscale images as channels (like RGB channels) and rotates all 32 images of the third dimension by a random angel around z-axis(treats my 3D image as a 2D image with 32 channels). But I want the image to be rotated in the space (around all three axes). Do you have any idea how can I do that? Is there a function or package for easily rotating the 3D images in space?!


It is more difficult to incorporate in the ImageAugmentation() but the scipy.ndimage.rotate function by default rotates 3D images correctly and takes the axes argument which specifies which specify the plane of rotation ( Rotating around the first axis (x) means you pass axes=(1,2), to rotate around the second axis (y) useaxes=(0,2)

Answered By – kmader

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published