Reputation: 569
Let's suppose that X
is a numpy.ndarray
tensor that holds a set of color images. For instance, X
's shape is (100, 3, 32, 32)
; i.e., we have 100 32x32 rgb images (such as those provided by the CIFAR dataset).
I want to rotate the images in X
and I found out that this could be done using the scipy.ndimage.interpolation.rotate function of scipy.
However, I can do it correctly only for a single image and a single color channel each time, while I would like to apply the function for all images and all color channels at once.
What I tried and it works so far is the following:
import scipy.ndimage.interpolation
image = X[0,0,:,:]
rotated_image = scipy.ndimage.interpolation.rotate(input=image, angle=45, reshape=False)
but what I want to do, which could look like this
X_rot = scipy.ndimage.interpolation.rotate(input=X, angle=45, reshape=False)
does not work.
Is there any way to apply scipy.ndimage.interpolation.rotate
for a batch of images like the numpy.ndarray tensor X
?
In any case, is there any better way to rotate a batch of images (stored in a numpy.ndarray
) efficiently?
Upvotes: 8
Views: 1283
Reputation: 2013
If you want a one-liner,
import scipy.ndimage.interpolation as intrp
rotated_imgs = [intrp.rotate(input=img, angle=45, reshape=False) for img in X]
assumed your X has the more standard shape (n_images, w, h, color_channels)
.
If you want to get your hands on matrices, I suggest you to generate your rotation matrix with cv2.getRotationMatrix2D
, replicate/stack it along the first axis, and then apply it to your X
, but it can get complicated without cv2.warpAffine
, which takes a single image as input.
Upvotes: 0