Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshaping OpenCV Image (numpy) Dimensions

I need to convert an image in a numpy array loaded via cv2 into the correct format for the deep learning library mxnet for its convolutional layers.

My current images are shaped as follows: (256, 256, 3), or (height, width, channels).

From what I've been told, this actually needs to be (3, 256, 256), or (channels, height, width).

Unfortunately, my knowledge of numpy/python opencv isn't good enough to know how to manipulate the arrays correctly.

I've figured out that I could split the arrays into channels by cv2.split, but I'm uncertain of how to combine them again in the right format (I don't know if using cv2.split is optimal, or if there are better ways in numpy).

Thanks for any help.

like image 684
AdmiralJonB Avatar asked Sep 14 '25 00:09

AdmiralJonB


2 Answers

You can use numpy.rollaxis as follow: If your image as shape (height, width, channels)

import numpy as np

new_shaped_image = np.rollaxis(image, axis=2, start=0)

This means that the 2nd axis of the new_shaped_image will be at 0 spot.

So new_shaped_image.shape will be (channels, height, width)

like image 50
Francesco Nazzaro Avatar answered Sep 15 '25 12:09

Francesco Nazzaro


arr.transpose(2,0,1).shape
# (3, 256, 256)
like image 43
Scott Avatar answered Sep 15 '25 12:09

Scott