Change array shape of an image in python
Question: When I read a colour image in OpenCV, it is showing the dimensions as
256x256x3. But I need to pass it as 3x256x256 array to my neural network. How
do I change the array shape, retaining the pixel locations in BGR.


Answer: You can simply transpose the array. For an example, my picture is a 10 x 10
picture:

    
    
    import numpy as np
    
    #my picture
    wrong_format = np.arange(300).reshape(10,10,3)
    
    correct_format = wrong_format.T 
    

If it works properly, then correct_format(0,1,1) should be equal to
wrong_format(1,1,0). And we can see that it is:

    
    
    correct_format(0,1,1) == wrong_format(1,1,0)
    

True

