Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dimension order of numpy shape for image data?

Tags:

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

like image 354
John Avatar asked Apr 07 '17 08:04

John


People also ask

What are numpy dimensions?

It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes. For example, the array for the coordinates of a point in 3D space, [1, 2, 1] , has one axis. That axis has 3 elements in it, so we say it has a length of 3.

How do you find the shape and dimension of a numpy array?

You can get the number of dimensions, shape (length of each dimension), and size (number of all elements) of the NumPy array with ndim , shape , and size attributes of numpy. ndarray . The built-in function len() returns the size of the first dimension.

Which function find the dimension of the data in numpy?

Use ndim attribute available with numpy array as numpy_array_name. ndim to get the number of dimensions. Alternatively, we can use shape attribute to get the size of each dimension and then use len() function for the number of dimensions.

What is shape of image in Python?

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray. shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.


Video Answer


1 Answers

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.


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]

like image 139
kmario23 Avatar answered Oct 11 '22 08:10

kmario23