<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 MergeAdjacentImages Notebook
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
Step1:  Next, we load the first input image and show it's origin, spacing, etc.
Step2:  Now let's split it into two overlaping images.
Step3:  Now let's merge the images without registration
Step4:  Now rotate and translate one image
Step5:  Now run merge with registration
<ASSISTANT_TASK:>
Python Code:
import os
import sys
import numpy
import itk
from itk import TubeTK as ttk
from itkwidgets import view
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
Explanation: MergeAdjacentImages Notebook
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.
End of explanation
ImageType=itk.Image[itk.F,2]
input_image1 = "Data\ES0015_Large.mha"
reader0 = itk.ImageFileReader[ImageType].New(FileName=input_image1)
reader0.Update()
resampler0 = ttk.ResampleImage[itk.F,2].New(Input=reader0.GetOutput(), ResampleFactor=(0.5,0.5))
resampler0.Update()
im0 = resampler0.GetOutput()
print("Origin:")
print(im0.GetOrigin())
print("Spacing:")
print(im0.GetSpacing())
view(im0)
Explanation: Next, we load the first input image and show it's origin, spacing, etc.:
End of explanation
CropImageFilterType = ttk.CropImage[ImageType,ImageType]
crop1 = CropImageFilterType.New(Input=im0, Min=(0,0), Max=(200,200))
crop1.Update()
im1 = crop1.GetOutput()
view(im1)
crop2 = CropImageFilterType.New(Input=im0, Min=(50,100), Max=(255,255))
crop2.Update()
im2 = crop2.GetOutput()
view(im2)
Explanation: Now let's split it into two overlaping images.
End of explanation
MAIFilterType = ttk.MergeAdjacentImages[ImageType]
merge1 = MAIFilterType.New(Input1=im1, Input2=im2)
merge1.SetMaxIterations(0)
merge1.Update()
im12 = merge1.GetOutput()
view(im12)
Explanation: Now let's merge the images without registration
End of explanation
TransformType = itk.AffineTransform[itk.D, 2]
trans = TransformType.New()
trans.Rotate2D( 3.1415926 * 0.02 )
ResampleFilterType = ttk.ResampleImage[itk.F, 2]
resample2 = ResampleFilterType.New(Input=im2, Transform=trans)
resample2.SetLoadTransform( True )
resample2.Update()
im2T = resample2.GetOutput()
merge2 = MAIFilterType.New(Input1=im1, Input2=im2T)
merge2.SetBlendUsingAverage( True )
merge2.SetMaxIterations(0)
merge2.Update()
im12T = merge2.GetOutput()
view(im12T)
Explanation: Now rotate and translate one image
End of explanation
merge3 = MAIFilterType.New(Input1=im1, Input2=im2T)
merge3.SetMaxIterations(1000)
merge3.SetExpectedRotation(0.1)
merge3.SetExpectedOffset(1)
merge3.SetSamplingRatio(0.5)
merge3.SetPadding((10,10))
merge3.Update()
im12TR = merge3.GetOutput()
view(im12TR)
Explanation: Now run merge with registration
End of explanation
<END_TASK>