What is dimension order of numpy shape for image data?

前端 未结 1 1388
轻奢々
轻奢々 2021-02-02 15:16

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

相关标签:
1条回答
  • 2021-02-02 15:26

    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]

    0 讨论(0)
提交回复
热议问题