Numpy fastest 3D to 2D projection

后端 未结 3 1384
天命终不由人
天命终不由人 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:56

    Some time back I wrote the below as a visualization aid for 3D arrays. Was also a good learning exercise.

    # Python 2.7.10
    from __future__ import print_function
    from numpy import *
    
    def f_Print3dArray(a_Array):
        v_Spacing = (len(str(amax(abs(a_Array)))) + 1) if amin(a_Array)\
            < 0 else (len(str(amax(a_Array))) + 1)
        for i in a_Array[:,:,::-1].transpose(0,2,1):
            for index, j in enumerate(i):
                print(" " * (len(i) - 1 - index) + "/ ", end="")
                for k in j:
                    print(str(k).ljust( v_Spacing + 1), end="")
                print("/")
            print()
    
    a_Array = arange(27).reshape(3, 3, 3)
    print(a_Array)
    print()
    
    f_Print3dArray(a_Array)
    

    Converts this:

    [[[ 0  1  2]
      [ 3  4  5]
      [ 6  7  8]]
    
     [[ 9 10 11]
      [12 13 14]
      [15 16 17]]
    
     [[18 19 20]
      [21 22 23]
      [24 25 26]]]
    

    To this:

      / 2   5   8   /
     / 1   4   7   /
    / 0   3   6   /
    
      / 11  14  17  /
     / 10  13  16  /
    / 9   12  15  /
    
      / 20  23  26  /
     / 19  22  25  /
    / 18  21  24  /
    

    Hope it helps someone.

提交回复
热议问题