What is dimension order of numpy shape for image data?

前提是你 提交于 2019-12-09 09:20:34

问题


I am using nibabel lib to load data from nii file. I read the document of the lib at http://nipy.org/nibabel/gettingstarted.html, and found that

This information is available without the need to load anything of the main image data into the memory. Of course there is also access to the image data as a NumPy array

This is my code to load the data and it shapes

import nibabel as nib
img = nib.load('example.nii')
data = img.get_data()
data = np.squeeze(data)
data = np.copy(data, order="C")
print data.shape

I got the result

128, 128, 64

What is order of data shape? Is it WidthxHeightxDepth? And my input must arranged as depth, height, width. So I will use input=data.transpose(2,0,1). Is it right? Thanks all

Update: I found that the Numpy will read the image by order Height x Width x Depth as the reference http://www.python-course.eu/images/axis.jpeg


回答1:


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]



来源:https://stackoverflow.com/questions/43272848/what-is-dimension-order-of-numpy-shape-for-image-data

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