What is dimension order of numpy shape for image data?

Deadly 提交于 2019-12-03 11:30:01

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 image
In [21]: img = scipy.ndimage.imread('suza.jpg', mode='RGB')

# image shape as (H, W, D)
In [22]: img.shape
Out[22]: (634, 1366, 3)

# transpose to shape as (D, H, W)
In [23]: tr_img = img.transpose((-1, 0, 1))    

In [23]: tr_img.shape
Out[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.


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]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!