Function iarot90

Description

This function returns a new 3D image, rotated by 90 degrees, either by X, Y or Z axis. The first parameter is the input image, and the second one is the axis in which this image will be rotated. This second parameter can be ommited, meaning that the rotation will occur on the X axis.

Synopsis

Creates a new 3D image, rotated by 90 degrees. This resulting image can be rotated either by axis X, Y or Z.

  • g = iarot90(img, axis='X')
    • g: Resulting Image.
    • img: Input Image that will be rotated
    • axis: Axis where the image will be rotated. This can be 'X', 'Y' or 'Z'

Code

01. from numpy import *
02. 
03. def iarot90(img, axis='X'):
04. 
05.    from ia636 import iaffine
06.    PIVAL = math.pi
07. 
08.    g = 0
09. 
10.    if axis == 'X':
11.       Trx = array([[cos(PIVAL/2), -sin(PIVAL/2), 0, img.shape[1] - 1],
12.                    [sin(PIVAL/2),  cos(PIVAL/2), 0, 0],
13.                    [           0,             0, 1, 0],
14.                    [           0,             0, 0, 1]]).astype(float)
15.       g = iaffine(img, Trx, [img.shape[1], img.shape[0], img.shape[2]])
16. 
17.    elif axis == 'Y':
18.       Try = array([[ cos(PIVAL/2), 0, sin(PIVAL/2), 0],
19.                     [            0, 1,            0, 0],
20.                     [-sin(PIVAL/2), 0, cos(PIVAL/2), img.shape[0] - 1],
21.                     [            0, 0,            0, 1]])
22.       g = iaffine(img, Try, [img.shape[2], img.shape[1], img.shape[0]])
23.    elif axis == 'Z':
24.       Trz = array([[1,            0,             0, 0],
25.                     [0, cos(PIVAL/2), -sin(PIVAL/2), img.shape[2] - 1],
26.                     [0, sin(PIVAL/2),  cos(PIVAL/2), 0],
27.                     [0,            0,             0, 1]]).astype(float)
28.       g = iaffine(img, Trz, [img.shape[0], img.shape[2], img.shape[1]])
29. 
30.    return g

Numeric Example

01. from ia636 import *
02. 
03. arr3 = arange(0, 24).reshape(3, 4, 2)
04. print "Array 3 Values = \n", arr3
05. 
06. # Z Rotation
07. print " ---- Z ROTATION ----- "
08. g = iarot90(arr3, axis='Z')
09. print "Rotated Array = \n", g
10. 
11. # X Rotation
12. print "\n ---- X ROTATION ----- "
13. g = iarot90(arr3, axis='X')
14. print "Rotated Array = \n", g
15. 
16. # Y Rotation
17. print "\n ---- Y ROTATION ----- "
18. g = iarot90(arr3, axis='Y')
19. print "Rotated Array = \n", g
Array 3 Values = 
[[[ 0  1]
  [ 2  3]
  [ 4  5]
  [ 6  7]]

 [[ 8  9]
  [10 11]
  [12 13]
  [14 15]]

 [[16 17]
  [18 19]
  [20 21]
  [22 23]]]
 ---- Z ROTATION ----- 
Rotated Array = 
[[[ 1  3  5  7]
  [ 0  2  4  6]]

 [[ 9 11 13 15]
  [ 8 10 12 14]]

 [[17 19 21 23]
  [16 18 20 22]]]

 ---- X ROTATION ----- 
Rotated Array = 
[[[ 6  7]
  [14 15]
  [22 23]]

 [[ 4  5]
  [12 13]
  [20 21]]

 [[ 2  3]
  [10 11]
  [18 19]]

 [[ 0  1]
  [ 8  9]
  [16 17]]]

 ---- Y ROTATION ----- 
Rotated Array = 
[[[16  8  0]
  [18 10  2]
  [20 12  4]
  [22 14  6]]

 [[17  9  1]
  [19 11  3]
  [21 13  5]
  [23 15  7]]]

Image Example

01. from ia636 import *
02. import dicom
03. 
04. filename = 'PHILIPS/DICOM/IM_0007'
05. dataset = dicom.read_file(find_attachment_file(filename))
06. 
07. # Image Characteristics
08. r = dataset.Rows
09. c = dataset.Columns
10. z = dataset.NumberofFrames
11. 
12. 
13. data = ianormalize(dataset.pixel_array)
14. data = data[::2,::2,::2]
15. sliced_data = data[40:50,:,:]
16. 
17. mos = iamosaic(sliced_data, 5)
18. adshow(mos, title = 'Original Mosaic')
19. 
20. g = iarot90(data,'Y')
21. g1 = g[40:50,:,:]
22. mos = iamosaic(g1, 5)
23. adshow(mos, title = 'Rotated Mosaic')
24. 
25. g = iarot90(data,'X')
26. g1 = g[40:50,:,:]
27. mos = iamosaic(g1, 5)
28. adshow(mos, title = 'Rotated Mosaic')
29. 
30. g = iarot90(data,'Z')
31. g1 = g[40:50,:,:]
32. mos = iamosaic(g1, 5)
33. adshow(mos, title = 'Rotated Mosaic')

Original Mosaic

Rotated Mosaic

Rotated Mosaic

Rotated Mosaic

Equation

See Also

Contributions

  • Guilherme Oelsen Franchi Jr, 1st semester 2011