Numpy fastest 3D to 2D projection

后端 未结 3 1394
天命终不由人
天命终不由人 2021-01-15 01:51

I have a 3D array of binary data. I want to project this to 3 2D images - side on, head on, birds eye.

I have written the code:

for x in range(data.s         


        
3条回答
  •  盖世英雄少女心
    2021-01-15 02:43

    When I have 3D data, I tend to think of it as a 'cube' with rows, columns, and slices - or panels, of 2D images. Each slice or panel is a 2D image that is of dimensions (rows, cols). I usually think of it like this:

    3D data cube

    with (0,0,0) being in the upper left corner of the front slice. With numpy indexing it is super easy to select just the portions of the 3D array that you are interested in without writing your own loops:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> np.set_printoptions(precision=2)
    
    # Generate a 3D 'cube' of data
    >>> data3D = np.random.uniform(0,10, 2*3*5).reshape((2,3,5))
    >>> data3D
    array([[[ 7.44,  1.14,  2.5 ,  3.3 ,  6.05],
            [ 1.53,  8.91,  1.63,  8.95,  2.46],
            [ 3.57,  3.29,  6.43,  8.81,  6.43]],
    
           [[ 4.67,  2.67,  5.29,  7.69,  7.59],
            [ 0.26,  2.88,  7.58,  3.27,  4.55],
            [ 5.84,  9.04,  7.16,  9.18,  5.68]]])
    
    # Grab some "views" of the data
    >>> front  = data3D[:,:,0]  # all rows and columns, first slice
    >>> back   = data3D[:,:,-1] # all rows and cols, last slice
    >>> top    = data3D[0,:,:]  # first row, all cols, all slices 
    >>> bottom = data3D[-1,:,:] # last row, all cols, all slices
    >>> r_side = data3D[:,-1,:] # all rows, last column, all slices
    >>> l_side = data3D[:,0,:]  # all rows, first column, all slices
    

    See what the front looks like:

    >>> plt.imshow(front, interpolation='none')
    >>> plt.show()
    

    front of data cube

提交回复
热议问题