Function iamosaic

Synopse

Create a 2D visualization of a 3D image.

  • g = iamosaic(f,N,s)
    • OUTPUT
      • g: Image. Mosaic of 2D images.
    • INPUT
      • 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.

01. import numpy as np
02. import scipy
03. import scipy.ndimage
04. 
05. def iamosaic(f,N,s=1.0):
06.     f = np.asarray(f)
07.     d,h,w = f.shape
08. 
09.     nLines = np.ceil(float(d)/N)
10.     nCells = nLines*N
11. 
12.     # Add black slices to match the exact number of mosaic cells
13.     fullf = np.resize(f, (nCells,h,w))
14.     fullf[d:nCells,:,:] = 0
15. 
16.     Y,X = np.indices((nLines*h,N*w))
17. 
18.     Pts = np.array([
19.             (np.floor(Y/h)*N + np.floor(X/w)).ravel(),
20.             np.mod(Y,h).ravel(),
21.             np.mod(X,w).ravel() ]).astype(int).reshape((3,nLines*h,N*w))
22.     g = fullf[Pts[0],Pts[1],Pts[2]]
23.     if (s != 1.0):
24.         g = scipy.ndimage.interpolation.zoom(g,s,order=5)
25.         #g = scipy.misc.imresize(g,s,interp='bilinear')
26.     return g

Examples

Example 1

01. import numpy as np
02. import ia636 as ia
03. 
04. t = np.arange(60)
05. t.shape = (5,3,4)
06. 
07. print 'Original 3D image:\n', t, '\n\n'
08. 
09. for i in range(1,7):
10.     g = ia.iamosaic(t,i)
11.     print 'Mosaic with %d image column(s):\n' % i
12.     print g
13.     print
14. 
15. for i in range(1,7):
16.     g = ia.iamosaic(t,i,0.8)
17.     print 'Mosaic with %d image column(s):\n' % i
18.     print g
19.     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):

[[ 0  2  3]
 [ 5  7  8]
 [10 12 13]
 [15 17 18]
 [20 22 23]
 [25 27 28]
 [31 32 34]
 [36 37 39]
 [41 42 44]
 [46 47 49]
 [51 52 54]
 [56 58 59]]

Mosaic with 2 image column(s):

[[ 0  2  2 13 13 15]
 [ 5  6  7 17 18 19]
 [19 20 21 33 33 35]
 [28 30 30 41 41 43]
 [37 38 41 30 32 33]
 [51 51 60 -9 -2 -4]
 [56 55 65 -6  2  0]]

Mosaic with 3 image column(s):

[[  0   2   2  13  13  14  25  25  27]
 [  4   6   6  17  18  18  37  36  38]
 [ 40  42  42  53  50  59 -10  -2  -4]
 [ 44  46  46  57  54  63  -6   2   0]]

Mosaic with 4 image column(s):

[[ 0  2  2 12 13 14 25 26 27 37 37 39]
 [ 3  5  4 22 23 24 36 37 38 50 50 52]
 [53 53 63 -6  0 -3 -4 -4 -4 -5 -5 -6]
 [56 55 66 -3  3 -1  0  0  0  0  0  0]]

Mosaic with 5 image column(s):

[[ 0  2  2 11 13 14 21 25 26 30 37 38 40 49 49 51]
 [ 8 10 10 19 21 22 29 33 34 38 45 46 48 57 57 59]]

Mosaic with 6 image column(s):

[[ 0  2  2 11 13 14 21 25 26 31 37 38 41 51 49 44 -6  1  0]
 [ 8 10 10 19 21 22 29 33 34 39 45 46 49 59 56 51 -7  2  0]]

Example 2

1. d,r,c = np.indices((5,3,4))
2. t = ((r + c)%2) >0
3. 
4. for i in range(1,7):
5.     g = ia.iamosaic(t,i,0.8)
6.     print 'Mosaic with %d image column(s):\n' % i
7.     print g
8.     print
Mosaic with 1 image column(s):

[[False False  True]
 [False False False]
 [False False  True]
 [False False False]
 [False False  True]
 [False False False]
 [False False False]
 [False False  True]
 [False False False]
 [False False  True]
 [False False False]
 [False False  True]]

Mosaic with 2 image column(s):

[[False False False False False  True]
 [False False False False False False]
 [False False  True False False  True]
 [ True False False False False False]
 [False False  True False False False]
 [False False False False False False]
 [False False False False False False]]

Mosaic with 3 image column(s):

[[False False False False False False False False  True]
 [False False False False False False False False False]
 [False False False False False False False False False]
 [False False False False False False False False False]]

Mosaic with 4 image column(s):

[[False False False False False False False False False False False  True]
 [False False False False False False False False False False False False]
 [False False False False False False False False False False False False]
 [False False False False False False False False False False False False]]

Mosaic with 5 image column(s):

[[False False False False False False False False False False False False
  False False False  True]
 [False False False False False False False False False False False False
  False False False  True]]

Mosaic with 6 image column(s):

[[False False False False False False False False False False False False
  False False False  True False False False]
 [False False False False False False False False False False False False
  False False False  True False False False]]

Example 3

01. import dicom
02. 
03. filename = 'PHILIPS/DICOM/IM_0007'
04. dataset = dicom.read_file(find_attachment_file(filename))
05. data = dataset.pixel_array.astype(float32)
06. f1 = data[90:120,:,:]
07. 
08. adshow(ia.ianormalize(ia.iamosaic(f1,5)), "Mosaic of a 3D brain image")
09. adshow(ia.ianormalize(ia.iamosaic(f1,5,0.2)), "Mosaic of a 3D brain image with scale factor 0.2")
10. adshow(ia.iamosaic(ia.ianormalize(f1)>80,5), "Mosaic of a binary 3D brain image")
11. adshow(ia.ianormalize(ia.iamosaic((ia.ianormalize(f1)>80).astype('uint8'),5,0.2)), "Mosaic of a binary 3D brain image with scale factor 0.2")

Mosaic of a 3D brain image

Mosaic of a 3D brain image with scale factor 0.2

Mosaic of a binary 3D brain image

Mosaic of a binary 3D brain image with scale factor 0.2

Notice that the first mosaic do not appear to be 5 times bigger than the second one because it was scaled by the visualization function adshow().

Equation

Each element of the original 3D image is mapped to an element on the destination 2D image (mosaic) by the equations

were is the number of image columns in mosaic, is the original image width and is the original image height.

Contributions

  • André Luis da Costa, 1o. semestre de 2011