# Lesson - Using iamosaic

In this lesson we are going to demonstrate how to use functions iarot90 (function used to rotate image slices, based on the implementation of iaffine) + iamosaic to display images in a mosaic.

# Using the functions - Step by Step

In this first step, a DICOM file is read and assigned to a variable for later usage.

```01. import dicom
02. from ia636 import *
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. data = ianormalize(dataset.pixel_array).astype(uint8)
13.
14. print "= Printing Image Characteristics ="
15. print "Image Rows = ", r
16. print "Image Columns = ", c
17. print "Total Slices = ", z
18.
19. print iaimginfo(data)```
```= Printing Image Characteristics =
Image Rows =  224
Image Columns =  224
Total Slices =  180
<type 'numpy.ndarray'> (180, 224, 224) uint8 0 255
```

## Example 1 - Without Step

In the next piece of code, a rotation of 90 degrees is applied to the Y axis, using the image obtained from the above piece of code.

`1. img_rot = iarot90(data, axis='Y')`

Then, a new ROI is defined:

`1. img_roi = img_rot[50:104,0:200,0:200]`

And, as a final step, the image is then displayed in a mosaic image, using the function iamosaic.

```1. img_mos = iamosaic(img_roi, 11)
2.
3. adshow(img_mos, title='Image Mosaic')``` Image Mosaic

## Example 2 - Using Step

The next code applies a 90 degrees rotation in the Y axis for the same image previously obtained.

`1. img_rot = iarot90(data, axis='Y')`

Then, a ROI is defined in that image, using a step of 3, discarding 2 slices for every 3

`1. img_roi = img_rot[50:113:3,0:200,0:200]`

And, as a final step, the image is then displayed in a mosaic image, using the function iamosaic.

```1. img_mos = iamosaic(img_roi, 11)
2.
3. adshow(img_mos, title='Image Mosaic with step')``` Image Mosaic with step

# Using one single function - iamosaicfull

This function adds all the above steps into a single function, called iamosaicfull.

## Sinopse

• g = iamosaicfull(f, p1, p2, n, step=1, rot='none')
• g: Resulting Image - Mosaic
• f: Image - Input Image - Volumetric Image (3D)
• rot: String - Rotation Axis (90 degrees) - X, Y, Z or none
• p1: List - Starting Top Left points
• p2: List - Ending Bottom Right points
• step: Integer - Step which will be used to select intercalated slices.
• n: Integer - images per row.

## Code

```01. def iamosaicfull(f, p1, p2, n, step=1, rot='none'):
02.
03.
04.    g = iamosaic(iarot90(f, rot)[p1:p2:step,
05.                                 p1:p2,
06.                                 p1:p2],n)
07.
08.    return g
09.
10. # Testing function
11. img_res = iamosaicfull(data, [50,0,0], [113,200,200], 11, 3, 'Y')
12. adshow(img_res, title='Resulting Image - iamosaicfull')``` Resulting Image - iamosaicfull

# Final Conclusions

Separating each function is possible to have a better control of what is happening, as well as what is going to be the final result on each step. And it is also easier to understand. Those functions were created to use the minimum of necessary parameters, even in the case of the optional ones (as in the case of iaroi3, which defines an additional parameter, case a step is used to intercalate slices).