What is dimension order of numpy shape for image data?
OK, here's my take:
Using scipy.ndimage.imread('img.jpg', mode='RGB')
, the resulting array will always have this order: (H, W, D)
i.e. (height, width, depth) because of the terminology that numpy uses for ndarrays (axis=0, axis=1, axis=2)
or analogously (Y, X, Z)
if one would like to visualize in 3 dimensions.
# read imageIn [21]: img = scipy.ndimage.imread('suza.jpg', mode='RGB')# image shape as (H, W, D)In [22]: img.shapeOut[22]: (634, 1366, 3)# transpose to shape as (D, H, W)In [23]: tr_img = img.transpose((-1, 0, 1)) In [23]: tr_img.shapeOut[23]: (3, 634, 1366)
If you consider the img_shape as a tuple,
# index (0, 1, 2)img_shape = (634, 1366, 3)# or index (-3, -2, -1)
Choose which one is a convenient way for you to remember.
NOTE: The scipy.ndimage.imread() API has been removed since Scipy 1.2.0. So, it is now recommended to use imageio.imread(), which reads the image and returns Array, a subclass of numpy array, following the same conventions discussed above.
# read image$ img = imageio.imread('suza.jpg', format='jpg')# convert the image to a numpy array$ img_np = np.asarray(img)
PS: It should also be noted that libraries like tensorflow also (almost) follows the same convention as numpy.
tf.image_decode_jpeg() returns:
A Tensor of type uint8. 3-D with shape
[height, width, channels]