Function iaptrans

Synopse

Perform periodic translation in 1-D, 2-D or 3-D space.

  • g = iaptrans(f, t)
    • OUTPUT
      • g: Image. Periodically translated image.
    • INPUT
      • f: Image ndarray. Image to be translated.
      • t: Tuple. (tz,tr,tc)

Description

Translate a 1-D, 2-D or 3-dimesional image periodically. This translation can be seen as a window view displacement on an infinite tile wall where each tile is a copy of the original image. The periodical translation is related to the periodic convolution and discrete Fourier transform. Be careful when implementing this function using the mod, some mod implementations in C does not follow the correct definition when the number is negative.

 1 from numpy import *
 2 
 3 def iaptrans(f,t):
 4     import numpy as np
 5     g = np.empty_like(f)
 6     if f.ndim == 1:
 7       W = f.shape[0]
 8       col = arange(W)
 9       g[:] = f[(col-t)%W]
10     elif f.ndim == 2:
11       H,W = f.shape
12       rr,cc = t
13       row,col = np.indices(f.shape)
14       g[:] = f[(row-rr)%H, (col-cc)%W]
15     elif f.ndim == 3:
16       Z,H,W = f.shape
17       zz,rr,cc = t
18       z,row,col = np.indices(f.shape)
19       g[:] = f[(z-zz)%Z, (row-rr)%H, (col-cc)%W]
20     return g
21 
22 # implementation using periodic convolution
23 def iaptrans2(f, t):
24     from ia636 import iapconv
25 
26     f, t = asarray(f), asarray(t).astype(int32)
27     h = zeros(2*abs(t) + 1)
28     t = t + abs(t)
29     h[tuple(t)] = 1
30     g = iapconv(f, h)
31     return g

Examples

Example 1

Numeric examples in 2D and 3D.

 1 from ia636 import iaptrans
 2 import numpy as np
 3 
 4 # 2D example
 5 f = arange(15).reshape(3,5)
 6 
 7 print "Original 2D image:\n",f,"\n\n"
 8 print "Image translated by (0,0):\n",iaptrans(f, (0,0)).astype(int),"\n\n"
 9 print "Image translated by (0,1):\n",iaptrans(f, (0,1)).astype(int),"\n\n"
10 print "Image translated by (-1,2):\n",iaptrans(f, (-1,2)).astype(int),"\n\n"
11 
12 # 3D example
13 f1 = arange(60).reshape(3,4,5)
14 
15 print "Original 3D image:\n",f1,"\n\n"
16 print "Image translated by (0,0,0):\n",iaptrans(f1, (0,0,0)).astype(int),"\n\n"
17 print "Image translated by (0,1,0):\n",iaptrans(f1, (0,1,0)).astype(int),"\n\n"
18 print "Image translated by (-1,3,2):\n",iaptrans(f1, (-1,3,2)).astype(int),"\n\n"
Original 2D image:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]] 


Image translated by (0,0):
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]] 


Image translated by (0,1):
[[ 4  0  1  2  3]
 [ 9  5  6  7  8]
 [14 10 11 12 13]] 


Image translated by (-1,2):
[[ 8  9  5  6  7]
 [13 14 10 11 12]
 [ 3  4  0  1  2]] 


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]]] 


Image translated by (0,0,0):
[[[ 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]]] 


Image translated by (0,1,0):
[[[15 16 17 18 19]
  [ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]]

 [[35 36 37 38 39]
  [20 21 22 23 24]
  [25 26 27 28 29]
  [30 31 32 33 34]]

 [[55 56 57 58 59]
  [40 41 42 43 44]
  [45 46 47 48 49]
  [50 51 52 53 54]]] 


Image translated by (-1,3,2):
[[[28 29 25 26 27]
  [33 34 30 31 32]
  [38 39 35 36 37]
  [23 24 20 21 22]]

 [[48 49 45 46 47]
  [53 54 50 51 52]
  [58 59 55 56 57]
  [43 44 40 41 42]]

 [[ 8  9  5  6  7]
  [13 14 10 11 12]
  [18 19 15 16 17]
  [ 3  4  0  1  2]]]

Example 2

Image examples in 2D and 3D.

 1 import dicom
 2 from ia636 import iamosaic, ianormalize
 3 
 4 # 2D example
 5 f = iaread('cameraman.pgm')
 6 adshow(f, 'Original 2D image - Cameraman')
 7 adshow(iaptrans(f, array(f.shape)/2), 'Cameraman periodically translated')
 8 
 9 # 3D example
10 filename = 'PHILIPS/DICOM/IM_0007'
11 dataset = dicom.read_file(find_attachment_file(filename))
12 data = dataset.pixel_array.astype(float32)
13 f1 = data[90:120,:,:]
14 adshow(ianormalize(iamosaic(f1, 6)), 'Original 3D image - Brain')
15 adshow(ianormalize(iamosaic(iaptrans(f1, array(f1.shape)/2), 6)), 'Brain periodically translated')

Original 2D image - Cameraman

Cameraman periodically translated

Original 3D image - Brain

Brain periodically translated

Equation

For 2D case we have

The equation above can be extended to n-dimensional space.

Contributions

  • Roberto A Lotufo, Sept 2013, converted to index computation
  • André Luis da Costa, 1st semester 2011