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('TubeTK_BUILD_DIR not found!')
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 set by directory not found!')
print(' TubeTK_BUILD_DIR = ' + TubeTK_BUILD_DIR )
sys.exit(1)
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('ITK_BUILD_DIR not found!')
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_BUILD_DIR set by directory not found!')
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
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')
reader0 = itk.ImageFileReader.New(FileName=input_image1)
reader0.Update()
im0=reader0.GetOutput()
print("Origin:")
print im0.GetOrigin()
print("Spacing:")
print im0.GetSpacing()
print("Direction:")
print im0.GetDirection()
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]:
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)
reader1 = itk.ImageFileReader.New(FileName=input_image0)
reader1.Update()
im1=reader1.GetOutput()
print im1.GetOrigin()
print im1.GetSpacing()
print im1.GetDirection()
In [10]:
im_np1 = itk.PyBuffer[ImageType].GetArrayFromImage(im1)
In [11]:
plt.imshow(im_np1, cm.gray)
Out[11]:
Let's check if the spacing and direction are compatible:
In [12]:
im0.GetSpacing() == im1.GetSpacing() and im0.GetDirection() == im1.GetDirection()
Out[12]:
We see that im0 and im1 are in fact compatible, but the origin of im0 is at (200, 200).
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')
cmd = [os.path.join(TubeTK_BUILD_DIR, 'bin\Release\MergeAdjacentImages'),
'-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]:
... and check if the output image /tmp/merged.mha was actually written to disk:
In [15]:
print os.path.exists(output_image)
We are now ready to visualize the result:
In [16]:
output_reader = itk.ImageFileReader.New(FileName=output_image)
output_reader.Update()
out_im=output_reader.GetOutput()
print out_im.GetOrigin()
print out_im.GetSpacing()
print out_im.GetLargestPossibleRegion().GetSize()
In [17]:
plt.imshow(itk.PyBuffer[ImageType].GetArrayFromImage(out_im), cm.gray)
Out[17]:
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.
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')
cmd1 = [os.path.join(TubeTK_BUILD_DIR, 'bin/Release/MergeAdjacentImages'),
'-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]:
In [20]:
reader=itk.ImageFileReader.New(FileName=output_image)
reader.Update()
plt.imshow(itk.PyBuffer[ImageType].GetArrayFromImage(reader.GetOutput()), cm.gray)
Out[20]:
Let's do the same example WITH rigid registration.
In [21]:
output_image = os.path.join(TubeTK_BUILD_DIR, 'Temporary/Python.MergeAdjacentImages-Ex3.mha')
cmd = [os.path.join(TubeTK_BUILD_DIR, 'bin/Release/MergeAdjacentImages'),
input_image1,
input_image0,
output_image
]
In [22]:
subprocess.call(cmd)
Out[22]:
In [23]:
reader=itk.ImageFileReader.New(FileName=output_image)
reader.Update()
plt.imshow(itk.PyBuffer[ImageType].GetArrayFromImage(reader.GetOutput()), cm.gray)
Out[23]: