llodds
llodds

Reputation: 305

How to apply random rotation to multiple images with the same rotation angle

I am trying to do some preprocessing to my input. I have three input images (X1, X2, X3). I want to augment the data and apply tf.keras.layers.experimental.preprocessing.RandomRotation() on every element (X1, X2, X3). I am hoping all the images in the same element are rotated with the same angle. How can I do that?

A related post: How to apply random image geometric transformation simultaneously to multiple images?

Any other solutions besides calling tf.stack() and tf.split()?

Upvotes: 1

Views: 732

Answers (1)

Gerry P
Gerry P

Reputation: 8112

try this

import scipy.misc
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
img1_path=r'c:\temp\people\test\savory\001.jpg' # path to first image
img2_path=r'c:\temp\people\test\savory\002.jpg' # path to second image
img3_path=r'c:\temp\people\test\savory\003.jpg' # path to third image
path_list=[img1_path, img2_path, img3_path]     # list of image paths
degree=np.random.random() * 360 # get random angle between 0 to 360 degrees
print (' rotation angle in counter clockwise direction = ', degree)
plt.figure(figsize=(12,12))
rotated_img_list=[] # create empty list to store rotated images
for i,path in enumerate(path_list):
    img= plt.imread(path) # read in the image
    rotated_img = ndimage.rotate(img, degree) # rotate the image counter clockwise by degree
    rotated_img_list.append(rotated_img) # appended rotated image to the list
    plt.subplot(1, 3, i + 1)
    plt.imshow(rotated_img)    # show the ith  rotated image
    name=str(i) #label the image
    plt.title(name, color='white', fontsize=16)
    plt.axis('off')
plt.show()

In your case you probably have the images in a list so don't both with reading them in. I showed an example for reading in and rotating 3 different images by the same random angle and displayed the results.

Upvotes: 1

Related Questions