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
[–] Comments
Camila De Lima at 2015-03-28 14:23h
 0
Eu tentei realizar a implementação da translação utilizando a convolução periódica (iptrans2) e não funciona para o teste 2, em que valores de t são maiores que a f de entrada. Acusa um problema com shapes diferentes na função iapconv. Por que isso acontece?

Roberto Lotufo at 2015-03-24 18:18h
Likes:
Andre Vinícius Lopes
 1
Olá Guilherme, esta é uma forma similar à feita na iaffine, porém trabalhando apenas com a translação. Veja que no iaffine também usamos o indices para gerar as matrizes das coordenadas de todos os pixels.

Guilherme Lucas at 2015-03-24 18:07h
Likes:
Andre Vinícius Lopes
 1
Não entendi o uso da funcao indices nesse caso, além da operacao entre as matrizes de indices e a tupla: row,col = np.indices(f.shape) g[:] = f[(row-rr)%H, (col-cc)%W