Reshaping OpenCV Image (numpy) Dimensions
Question: 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.


Answer: You can use
[`numpy.rollaxis`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html)
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 `2`nd axis of the `new_shaped_image` will be at `0` spot.

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

