This notebook contains a few examples of how to use the MergeAdjacentImages application from TubeTK. First, we will include Python's os package as well as ITK (Python wrapping). We also set the TubeTK build directory variable TUBETK_BUILD_DIR:

If ITK is not installed in your python environment, you need to define the environment variable ITK_BUILD_DIR that contains the path to where ITK was built.

We need to find the directory in which TubeTK was build. This is required to find the path to the testing data, and may be also required to find the TubeTK library paths if your python environment does not include it. The environment variable TubeTK_BUILD_DIR needs to be defined.

In [1]:
import os
import sys
import numpy

In [2]:
# Path for TubeTK libs
#Values takend from TubeTK launcher

sys.path.append("C:/src/TubeTK_Python_ITK/TubeTK-build/lib/")
sys.path.append("C:/src/TubeTK_Python_ITK/TubeTK-build/lib/Release")

In [3]:
# Setting TubeTK Build Directory

TubeTK_BUILD_DIR=None
if 'TubeTK_BUILD_DIR' in os.environ:
TubeTK_BUILD_DIR = os.environ['TubeTK_BUILD_DIR']
else:
print('  Set environment variable')
os.environ["TubeTK_BUILD_DIR"] = "C:/src/TubeTK_Python_ITK/TubeTK-build"
TubeTK_BUILD_DIR = os.environ["TubeTK_BUILD_DIR"]
#sys.exit( 1 )

if not os.path.exists(TubeTK_BUILD_DIR):
print('  TubeTK_BUILD_DIR = ' + TubeTK_BUILD_DIR )
sys.exit(1)

TubeTK_BUILD_DIR not found!
Set environment variable

In [4]:
try:
import itk
except:
ITK_BUILD_DIR = None
if 'ITK_BUILD_DIR' in os.environ:
ITK_BUILD_DIR = os.environ['ITK_BUILD_DIR']
else:
print('  Set environment variable')
os.environ["ITK_BUILD_DIR"] = "C:/src/TubeTK_Python_R/ITK-build"
ITK_BUILD_DIR = os.environ["ITK_BUILD_DIR"]
#sys.exit( 1 )

if not os.path.exists(ITK_BUILD_DIR):
print('  ITK_BUIDL_DIR = ' + ITK_BUILD_DIR )
sys.exit(1)
# Append ITK libs
sys.path.append("C:/src/TubeTK_Python_ITK/ITK-build/Wrapping/Generators/Python/Release")
sys.path.append("C:/src/TubeTK_Python_ITK/ITK-build/lib/Release")
sys.path.append("C:/src/TubeTK_Python_ITK/ITK-build/lib")

# Append TubeTK libs
sys.path.append("C:/src/TubeTK_Python_ITK/TubeTK-build/ITKModules/TubeTKITK-build/Wrapping/Generators/Python/Release")
import itk

ITK_BUILD_DIR not found!
Set environment variable

In [5]:
from itk import TubeTKITK as itktube

import matplotlib.pyplot as plt
from matplotlib import cm

%matplotlib inline


Next, we load the first input image and show it's origin, spacing, etc.:

In [6]:
input_image1 = os.path.join(TubeTK_BUILD_DIR, 'MIDAS_Data\ES0015_Large.mha')
print("Origin:")
print im0.GetOrigin()
print("Spacing:")
print im0.GetSpacing()
print("Direction:")
print im0.GetDirection()

Origin:
itkPointD2 ([200, 200])
Spacing:
itkVectorD2 ([1, 1])
Direction:
<itkMatrixPython.itkMatrixD22; proxy of <Swig Object of type 'itkMatrixD22 *' at 0x000000000C4C1390> >

itkImageFileReaderIF2: 0.000000itkImageFileReaderIF2: 1.000000

We get the numpy array for the image and visualize it:

In [7]:
ImageType=itk.Image[itk.F,2]
im_np0 = itk.PyBuffer[ImageType].GetArrayFromImage(im0)

In [8]:
plt.imshow(im_np0, cm.gray)

Out[8]:
<matplotlib.image.AxesImage at 0xc709278>

Let's do the same for the second image:

In [9]:
input_image0 = os.path.join(TubeTK_BUILD_DIR, 'MIDAS_Data\ES0015_Large_Wo_offset.mha')
print("Image file path:%s"%input_image0)
print im1.GetOrigin()
print im1.GetSpacing()
print im1.GetDirection()

Image file path:C:/src/TubeTK_Python_ITK/TubeTK-build\MIDAS_Data\ES0015_Large_Wo_offset.mha
itkPointD2 ([0, 0])
itkVectorD2 ([1, 1])
<itkMatrixPython.itkMatrixD22; proxy of <Swig Object of type 'itkMatrixD22 *' at 0x000000000C55C600> >

itkImageFileReaderIF2: 0.000000itkImageFileReaderIF2: 1.000000
In [10]:
im_np1 = itk.PyBuffer[ImageType].GetArrayFromImage(im1)

In [11]:
plt.imshow(im_np1, cm.gray)

Out[11]:
<matplotlib.image.AxesImage at 0xca3db00>

Let's check if the spacing and direction are compatible:

In [12]:
im0.GetSpacing() == im1.GetSpacing() and im0.GetDirection() == im1.GetDirection()

Out[12]:
True

We see that im0 and im1 are in fact compatible, but the origin of im0 is at (200, 200).

Merging - Example 1¶

In this example, we just want to merge our two images (without registration). Let's build the command-line arguments (The output image will be written to /tmp/merged.mha).

In [13]:
output_image = os.path.join(TubeTK_BUILD_DIR, 'Temporary\Python.MergeAdjacentImages-Ex1.mha')
'-i', '0', # Number of iterations ... here i=0, which means no registration,
input_image0, # First image to merge
input_image1, # Second image to merge
output_image
]


Let's execute that command (via the subprocess module):

In [14]:
import subprocess
subprocess.call(cmd)

Out[14]:
0

... and check if the output image /tmp/merged.mha was actually written to disk:

In [15]:
print os.path.exists(output_image)

True


We are now ready to visualize the result:

In [16]:
output_reader = itk.ImageFileReader.New(FileName=output_image)
print out_im.GetOrigin()
print out_im.GetSpacing()
print out_im.GetLargestPossibleRegion().GetSize()

itkPointD2 ([0, 0])
itkVectorD2 ([1, 1])
itkSize2 ([712, 712])

itkImageFileReaderIF2: 0.000000itkImageFileReaderIF2: 1.000000
In [17]:
plt.imshow(itk.PyBuffer[ImageType].GetArrayFromImage(out_im), cm.gray)

Out[17]:
<matplotlib.image.AxesImage at 0xcc976d8>

We see that the output image is larger by 212 pixel in both dimensions, since the second image's origin was at (200, 200) and the image size of both images was 512 times 512 pixel.

Merging - Example 2¶

We can also add some padding (e.g., 50 pixel on each side). For that we modify cmd as follows:

In [18]:
output_image = os.path.join(TubeTK_BUILD_DIR, 'Temporary/Python.MergeAdjacentImages-Ex2.mha')

'-i','0', # Number of iterations ... here i=0, which means no registration,
'-b','50,50', # This adds a white border around the second image (50 pixel each side)
input_image0,
input_image1,
output_image
]

In [19]:
subprocess.call(cmd1)

Out[19]:
0
In [20]:
reader=itk.ImageFileReader.New(FileName=output_image)

itkImageFileReaderIF2: 0.000000itkImageFileReaderIF2: 1.000000
Out[20]:
<matplotlib.image.AxesImage at 0xcedf278>

Merging - Example 3¶

Let's do the same example WITH rigid registration.

In [21]:
output_image = os.path.join(TubeTK_BUILD_DIR, 'Temporary/Python.MergeAdjacentImages-Ex3.mha')

input_image1,
input_image0,
output_image
]

In [22]:
subprocess.call(cmd)

Out[22]:
0
In [23]:
reader=itk.ImageFileReader.New(FileName=output_image)

itkImageFileReaderIF2: 0.000000itkImageFileReaderIF2: 1.000000
<matplotlib.image.AxesImage at 0xcfa20f0>