如何在 python 中以随机角度旋转 3D 图像

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

我正在使用一组 32x32x32 灰度图像,我想在通过 tflearn + tensorflow 训练 CNN 时对图像应用随机旋转作为数据增强的一部分。我使用以下代码来执行此操作:

    # Real-time data preprocessing
    img_prep = ImagePreprocessing()
    img_prep.add_featurewise_zero_center()
    img_prep.add_featurewise_stdnorm()


    # Real-time data augmentation
    img_aug = ImageAugmentation()
    img_aug.add_random_rotation(max_angle=360.)

    # 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,
                 data_preprocessing=img_prep,
                 data_augmentation=img_aug)

(我正在使用 tensorflow 和 tflearn 的组合,以便能够使用两者的功能,所以请耐心等待。如果我使用占位符等的方式有问题,请告诉我。 )

我发现使用 add_random_rotation(它本身使用 scipy.ndimage.interpolation.rotate)将我的灰度图像的第三维视为通道(如 RGB 通道)并通过随机角度围绕 z 轴旋转所有 32 个三维图像(将我的 3D 图像视为具有 32 个通道的 2D 图像)。但我希望图像在 space 中旋转(围绕所有三个轴)。你知道我该怎么做吗? space中是否有方便旋转3D图像的函数或包?!

更难融入ImageAugmentation(),但scipy.ndimage.rotate函数默认正确旋转3D图像并采用指定旋转平面的轴参数(https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.ndimage.interpolation.rotate.html).绕第一个轴(x)旋转意味着你通过axes=(1,2),绕第二个轴(y)旋转使用axes=(0,2)

def random_rotation_3d(batch, max_angle):
    """ Randomly rotate an image by a random angle (-max_angle, max_angle).

    Arguments:
    max_angle: `float`. The maximum rotation angle.

    Returns:
    batch of rotated 3D images
    """
    size = batch.shape
    batch = np.squeeze(batch)
    batch_rot = np.zeros(batch.shape)
    for i in range(batch.shape[0]):
        if bool(random.getrandbits(1)):
            image1 = np.squeeze(batch[i])
            # rotate along z-axis
            angle = random.uniform(-max_angle, max_angle)
            image2 = scipy.ndimage.interpolation.rotate(image1, angle, mode='nearest', axes=(0, 1), reshape=False)

            # rotate along y-axis
            angle = random.uniform(-max_angle, max_angle)
            image3 = scipy.ndimage.interpolation.rotate(image2, angle, mode='nearest', axes=(0, 2), reshape=False)

            # rotate along x-axis
            angle = random.uniform(-max_angle, max_angle)
            batch_rot[i] = scipy.ndimage.interpolation.rotate(image3, angle, mode='nearest', axes=(1, 2), reshape=False)
            #                print(i)
        else:
            batch_rot[i] = batch[i]
    return batch_rot.reshape(size)