Function mosaic

Synopse

Create a 2D visualization of a 3D image.

  • g = mosaic(f,N,s)

    • g: Image. Mosaic of 2D images.

    • f: Image. 3D image.

    • N: Integer. Number of image columns in mosaic.
    • s: Float. Default: 1.0. Scale factor.

Description

This function puts the slices of a 3D image side-by-side on a mosaic (2D image). The objective is to provide basic 3D visualization.


In [8]:
import numpy as np
import scipy.misc

def mosaic(f,N,s=1.0):
    f = np.asarray(f)
    d,h,w = f.shape
    N = int(N)
    nLines = int(np.ceil(float(d)/N))
    nCells = int(nLines*N)
 
    # Add black slices to match the exact number of mosaic cells
    fullf = np.resize(f, (nCells,h,w))
    fullf[d:nCells,:,:] = 0

    Y,X = np.indices((nLines*h,N*w))
    Pts = np.array([
            (np.floor(Y/h)*N + np.floor(X/w)).ravel(),
            np.mod(Y,h).ravel(),
            np.mod(X,w).ravel() ]).astype(int).reshape((3,int(nLines*h),int(N*w)))
    g = fullf[Pts[0],Pts[1],Pts[2]]
    if (s != 1.0):
        #g = scipy.ndimage.interpolation.zoom(g,s,order=5)
        g = scipy.misc.imresize(g,s,interp='bilinear')
    return g

Examples


In [1]:
testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python mosaic.ipynb
    import sys
    import os
    import numpy as np
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia


[NbConvertApp] Converting notebook mosaic.ipynb to python
[NbConvertApp] Writing 2709 bytes to mosaic.py

Example 1


In [4]:
if testing:
    t = np.arange(60)
    t.shape = (5,3,4)

    print('Original 3D image:\n', t, '\n\n')

    for i in range(1,7):
        g = ia.mosaic(t,i)
        print('Mosaic with %d image column(s):\n' % i)
        print(g)
        print()

    for i in range(1,7):
        g = ia.mosaic(t,i,0.8)
        print('Mosaic with %d image column(s):\n' % i)
        print(g)
        print()


Original 3D image:
 [[[ 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 27]
  [28 29 30 31]
  [32 33 34 35]]

 [[36 37 38 39]
  [40 41 42 43]
  [44 45 46 47]]

 [[48 49 50 51]
  [52 53 54 55]
  [56 57 58 59]]] 


Mosaic with 1 image column(s):

[[ 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 27]
 [28 29 30 31]
 [32 33 34 35]
 [36 37 38 39]
 [40 41 42 43]
 [44 45 46 47]
 [48 49 50 51]
 [52 53 54 55]
 [56 57 58 59]]

Mosaic with 2 image column(s):

[[ 0  1  2  3 12 13 14 15]
 [ 4  5  6  7 16 17 18 19]
 [ 8  9 10 11 20 21 22 23]
 [24 25 26 27 36 37 38 39]
 [28 29 30 31 40 41 42 43]
 [32 33 34 35 44 45 46 47]
 [48 49 50 51  0  0  0  0]
 [52 53 54 55  0  0  0  0]
 [56 57 58 59  0  0  0  0]]

Mosaic with 3 image column(s):

[[ 0  1  2  3 12 13 14 15 24 25 26 27]
 [ 4  5  6  7 16 17 18 19 28 29 30 31]
 [ 8  9 10 11 20 21 22 23 32 33 34 35]
 [36 37 38 39 48 49 50 51  0  0  0  0]
 [40 41 42 43 52 53 54 55  0  0  0  0]
 [44 45 46 47 56 57 58 59  0  0  0  0]]

Mosaic with 4 image column(s):

[[ 0  1  2  3 12 13 14 15 24 25 26 27 36 37 38 39]
 [ 4  5  6  7 16 17 18 19 28 29 30 31 40 41 42 43]
 [ 8  9 10 11 20 21 22 23 32 33 34 35 44 45 46 47]
 [48 49 50 51  0  0  0  0  0  0  0  0  0  0  0  0]
 [52 53 54 55  0  0  0  0  0  0  0  0  0  0  0  0]
 [56 57 58 59  0  0  0  0  0  0  0  0  0  0  0  0]]

Mosaic with 5 image column(s):

[[ 0  1  2  3 12 13 14 15 24 25 26 27 36 37 38 39 48 49 50 51]
 [ 4  5  6  7 16 17 18 19 28 29 30 31 40 41 42 43 52 53 54 55]
 [ 8  9 10 11 20 21 22 23 32 33 34 35 44 45 46 47 56 57 58 59]]

Mosaic with 6 image column(s):

[[ 0  1  2  3 12 13 14 15 24 25 26 27 36 37 38 39 48 49 50 51  0  0  0  0]
 [ 4  5  6  7 16 17 18 19 28 29 30 31 40 41 42 43 52 53 54 55  0  0  0  0]
 [ 8  9 10 11 20 21 22 23 32 33 34 35 44 45 46 47 56 57 58 59  0  0  0  0]]

Mosaic with 1 image column(s):

[[  5  11  16]
 [ 26  31  37]
 [ 46  51  57]
 [ 67  73  78]
 [ 90  96 101]
 [112 118 123]
 [132 138 143]
 [154 159 165]
 [177 183 188]
 [198 204 209]
 [218 224 229]
 [239 245 250]]

Mosaic with 2 image column(s):

[[  6  12  20  55  64  69]
 [ 26  32  40  75  84  89]
 [ 80  85  94 129 137 143]
 [122 128 136 171 180 185]
 [164 170 170 127 125 129]
 [218 223 208  21   0   0]
 [238 244 227  23   0   0]]

Mosaic with 3 image column(s):

[[  7  13  21  56  65  73 108 117 122]
 [ 43  49  57  92 101 106 117 123 128]
 [149 155 163 198 206 193  34  16  17]
 [185 191 199 234 242 225  23   0   0]]

Mosaic with 4 image column(s):

[[  7  13  21  56  65  73 108 117 125 160 169 174]
 [ 49  55  61  71  77  84 115 123 130 161 169 174]
 [195 200 188  28  10  11  15  16  17  21  22  22]
 [237 242 225  23   0   0   0   0   0   0   0   0]]

Mosaic with 5 image column(s):

[[  7  13  17  49  63  69  94 113 120 139 164 170 184 216 220 226]
 [ 29  35  39  71  85  91 116 135 142 161 186 192 206 238 242 248]]

Mosaic with 6 image column(s):

[[  7  13  17  51  63  69  96 114 120 143 165 171 189 216 221 181   0   0
    0]
 [ 29  35  39  73  85  91 118 136 142 165 187 193 211 238 243 199   0   0
    0]]

Example 2


In [5]:
if testing:
    d,r,c = np.indices((5,3,4))
    t = ((r + c)%2) >0

    for i in range(1,7):
       g = ia.mosaic(t,i,0.8)
       print('Mosaic with %d image column(s):\n' % i)
       print(g)
       print()


Mosaic with 1 image column(s):

[[102 128 153]
 [136 128 119]
 [ 76 128 179]
 [147 128 108]
 [ 84 128 171]
 [119 128 136]
 [119 128 136]
 [ 84 128 171]
 [147 128 108]
 [ 76 128 179]
 [136 128 119]
 [102 128 153]]

Mosaic with 2 image column(s):

[[104 128 143 112 128 151]
 [132 128 124 131 128 123]
 [ 76 128 162  93 128 179]
 [147 128 114 141 128 108]
 [ 76 128 162  68  81 114]
 [132 128 112  10   0   0]
 [104 128 137  17   0   0]]

Mosaic with 3 image column(s):

[[115 128 136 119 128 136 119 128 140]
 [110 128 139 116 128 139 108 114 125]
 [110 128 139 116 128 131  23  14  20]
 [115 128 136 119 128 128  14   0   0]]

Mosaic with 4 image column(s):

[[115 128 136 119 128 136 119 128 136 119 128 140]
 [110 128 139 108 114 121 106 114 121 106 114 125]
 [110 128 131  23  14  18  10  14  18  10  14  20]
 [115 128 128  14   0   0   0   0   0   0   0   0]]

Mosaic with 5 image column(s):

[[112 133 133 115 140 122 122 140 115 133 133 115 140 122 122 143]
 [112 133 133 115 140 122 122 140 115 133 133 115 140 122 122 143]]

Mosaic with 6 image column(s):

[[112 132 134 115 138 125 119 140 117 128 138 115 136 130 117 121   0   0
    0]
 [112 132 134 115 138 125 119 140 117 128 138 115 136 130 117 121   0   0
    0]]

Equation

Each element of the original 3D image $(x_{0},y_{0},z_{0})$ is mapped to an element on the destination 2D image (mosaic) $(x_{d},y_{d})$ by the equations.

$$ \begin{matrix} x_{0} &=& x_{d} &mod& w \\ y_{0} &=& y_{d} &mod& h \\ z_{0} &=& \lfloor \frac{y_{d}}{h} \rfloor N + \lfloor \frac{x_{d}}{w} \rfloor \end{matrix} $$

were $N$ is the number of image columns in mosaic, $w$ is the original image width and $h$ is the original image height.


In [ ]: