4

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.

1

2 Answers 2

4

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)

Sign up to request clarification or add additional context in comments.

Comments

1
arr.transpose(2,0,1).shape
# (3, 256, 256)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.