Function iaffine

Synopse

Affine transform.

  • g = iaffine(f, T, domain)
    • g: Image.
    • f: Image.
    • T: Image. Affine matrix for the geometric transformation.
    • domain: Output Image domain. Optional. Default is domain of the input f.
01. import numpy as np
02. 
03. def iaffine(f,T,domain=0):
04. 
05.     if np.sum(domain) == 0:
06.         domain = f.shape
07.     if len(f.shape) == 2:
08.         H,W = f.shape
09.         y1,x1 = np.indices((domain))
10. 
11.         yx1 = np.array([ y1.ravel(),
12.                          x1.ravel(),
13.                          np.ones(np.product(domain))])
14.         yx_float = np.dot(np.linalg.inv(T), yx1)
15.         yy = np.rint(yx_float[0]).astype(int)
16.         xx = np.rint(yx_float[1]).astype(int)
17. 
18.         y = np.clip(yy,0,H-1).reshape(domain)
19.         x = np.clip(xx,0,W-1).reshape(domain)
20. 
21.         g = f[y,x]
22. 
23.     if len(f.shape) == 3:
24.         D,H,W = f.shape
25.         z1,y1,x1 = np.indices((domain))
26.         zyx1 = np.array([ z1.ravel(),
27.                           y1.ravel(),
28.                           x1.ravel(),
29.                           np.ones(np.product(domain)) ])
30.         zyx_float = np.dot(np.linalg.inv(T), zyx1)
31. 
32.         zz = np.rint(zyx_float[0]).astype(int)
33.         yy = np.rint(zyx_float[1]).astype(int)
34.         xx = np.rint(zyx_float[2]).astype(int)
35. 
36.         z = np.clip(zz, 0, D-1).reshape(domain) #z
37.         y = np.clip(yy, 0, H-1).reshape(domain) #rows
38.         x = np.clip(xx, 0, W-1).reshape(domain) #columns
39. 
40.         g = f[z,y,x]
41. 
42.     return g

Description

Perform the affine transformation of an image in 2D or 3D.

Examples

Example 1

01. import dicom
02. from ia636 import *
03. from math import *
04. import ia636 as ia
05. 
06. scale = [1,0.5]
07. theta = pi/4
08. t = [2,1]
09. 
10. Ts   = [[scale[1],0,0], [0,scale[0],0], [0,0,1]]
11. Trot = [[cos(theta),-sin(theta),0], [sin(theta),cos(theta),0], [0,0,1]]
12. Tx   = [[1,0,t[1]], [0,1,t[0]], [0,0,1]]
13. 
14. f = array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
15. print 'Original matrix:','\n'
16. print f,'\n'
17. g = iaffine(f,Ts)
18. print 'Result with scale:','\n'
19. print g,'\n'
20. 
21. g = iaffine(f,Trot)
22. print 'Result with rotation:','\n'
23. print g,'\n'
24. 
25. g = iaffine(f,Tx)
26. print 'Result:','\n'
27. print g,'\n'
Original matrix: 

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]] 

Result with scale: 

[[ 1  2  3  4  5]
 [11 12 13 14 15]
 [11 12 13 14 15]] 

Result with rotation: 

[[ 1  7  7 13 14]
 [ 6  6 12 12 13]
 [ 6 11 11 12 12]] 

Result: 

[[1 1 1 2 3]
 [1 1 1 2 3]
 [6 6 6 7 8]]

Example 2

01. import dicom
02. from ia636 import *
03. from math import *
04. 
05. f = iaread('lenina.pgm')
06. theta = pi/4
07. scale = [0.5,0.5]
08. t = [0,64]
09. 
10. Ts   = [[scale[1],0,0], [0,scale[0],0], [0,0,1]]                         #Scale
11. Trot = [[cos(theta),-sin(theta),0], [sin(theta),cos(theta),0], [0,0,1]]  #Rotation
12. Tx   = [[1,0,t[1]], [0,1,t[0]], [0,0,1]]                                 #Translation
13. 
14. g = iaffine(f,dot(Tx,dot(Ts,Trot)))
15. adshow(f,"Original image")
16. adshow(g,"Affine transformation")

Original image

Affine transformation

Example 3

01. import dicom
02. from ia636 import *
03. from math import *
04. 
05. f = array([[[0,1,3],
06.             [2,3,1],
07.             [1,2,1]],
08. 
09.             [[4,5,6],
10.             [6,7,3],
11.             [4,5,6]]])
12. 
13. print "f:\n",f
14. print "shape f\n",f.shape
15. 
16. Trx = array([[1,0,0,0],[0,cos(pi/2),sin(pi/2),0],[0,-sin(pi/2),cos(pi/2),0],[0,0,0,1]],'d')
17. g = iaffine(f,Trx)
18. print "Rotation in X:\n",g
19. 
20. Try = array([[cos(pi/2),0,-sin(pi/2),0],[0,1,0,0],[sin(pi/2),0,cos(pi/2),0],[0,0,0,1]],'d')
21. g = iaffine(f,Try)
22. print "Rotation in Y:\n",g
23. 
24. Trz = array([[cos(pi/2),sin(pi/2),0,0],[-sin(pi/2),cos(pi/2),0,0],[0,0,1,0],[0,0,0,1]],'d')
25. g = iaffine(f,Trz)
26. print "Rotation in Z:\n",g
f:
[[[0 1 3]
  [2 3 1]
  [1 2 1]]

 [[4 5 6]
  [6 7 3]
  [4 5 6]]]
shape f
(2, 3, 3)
Rotation in X:
[[[0 0 0]
  [1 1 1]
  [3 3 3]]

 [[4 4 4]
  [5 5 5]
  [6 6 6]]]
Rotation in Y:
[[[0 4 4]
  [2 6 6]
  [1 4 4]]

 [[0 4 4]
  [2 6 6]
  [1 4 4]]]
Rotation in Z:
[[[0 1 3]
  [0 1 3]
  [0 1 3]]

 [[2 3 1]
  [2 3 1]
  [2 3 1]]]

Example 4

01. import dicom
02. from math import *
03. 
04. filename = 'PHILIPS/DICOM/IM_0007'
05. dataset = dicom.read_file(find_attachment_file(filename))
06. data = dataset.pixel_array.astype(float64)
07. data = data[90:120,:,:]
08. g = iamosaic(data, 5)
09. adshow(ianormalize(g), title = 'Original Image')
10. 
11. theta = pi/3
12. # Transformation of line rotation
13. Trr = [[cos(theta), 0, sin(theta), 0],
14.        [0, 1, 0, 0],
15.        [-sin(theta), 0, cos(theta), 0],
16.        [0, 0, 0, 1]]
17. 
18. # Transformation of slices rotation
19. Trs = [[1, 0, 0, 0],
20.        [0, cos(theta), -sin(theta), 0],
21.        [0, sin(theta), cos(theta), 0],
22.        [0, 0, 0, 1]]
23. 
24. #Transformation of columns rotation
25. Trc = [[cos(theta), -sin(theta), 0, 0],
26.        [sin(theta), cos(theta), 0, 0],
27.        [0, 0, 1, 0],
28.        [0, 0, 0, 1]]
29. 
30. g = iaffine(data,Trc)
31. adshow(ia.ianormalize(iamosaic(g, 5)), title = 'columns rotated')

Original Image

columns rotated

Equation

See Also

Contributions

  • Fernando Paolieri Neto, April 2009
  • Jan Beeck, 1st semester 2011