如何在python中以随机角度旋转3D图像
我正在使用一组32x32x32灰度图像,我想在图像上应用随机旋转作为数据增强的一部分,同时通过tflearn+tensorflow训练CNN。我正在使用以下代码执行此操作:如何在python中以随机角度旋转3D图像,python,image,tensorflow,deep-learning,tflearn,Python,Image,Tensorflow,Deep Learning,Tflearn,我正在使用一组32x32x32灰度图像,我想在图像上应用随机旋转作为数据增强的一部分,同时通过tflearn+tensorflow训练CNN。我正在使用以下代码执行此操作: # Real-time data preprocessing img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Re
# 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的组合,以便能够使用两者的功能,因此请耐心等待。如果我使用占位符的方式有问题,请告诉我。)
我发现使用添加随机旋转(它本身使用scipy.ndimage.interpolation.rotate)将我的灰度图像的第三维视为通道(如RGB通道),并围绕z轴以随机角度旋转所有32个三维图像(将我的3D图像视为具有32个通道的2D图像)。但我希望图像在空间中旋转(围绕所有三个轴)。你知道我怎么做吗?是否有一个功能或软件包可以在空间中轻松旋转3D图像 在
ImageAugmentation()
中更难合并,但是默认情况下,scipy.ndimage.rotate
函数正确旋转三维图像,并使用axes参数指定旋转平面()。围绕第一个轴(x)旋转意味着你通过轴=(1,2)
,要围绕第二个轴(y)旋转,请使用轴=(0,2)
谢谢,我使用了你的建议,并想出了以下函数,以指定的最大角度围绕轴随机旋转一批3D图像(我在这里发布它,以防有人需要):@kmader,轴参数的顺序是否起作用?i、 e.轴=(1,2)
是否等于轴=(2,1)
?
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)