<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Convert Tubes To Images
This notebook contains a few examples of how to call wrapped methods in itk and ITKTubeTK.
ITK, ITKTubeTK, and ITKWidgets must be installed on your system for this notebook to work.
Step1:  Load the tubes and a reference image which provides the size, spacing, origin, and orientation for the desired output image.
Step2:  Visualize the template image, just because it looks cool - the data in the image is actually irrelevant.
Step3:  Create a binary image that represents the spatial extent of the TubeSpatialObjects in the hierarchy of SpatialObjects in the variable "tubes" that was read-in above.   If you only want to visualize centerlines of the tubes, set "UseRadius" to false.
Step4:  Visualize the results by blending the template and output images.  Again, the content of the template image
doesn't actually matter, but since these tubes were generated from the content of the template image, blending them illustrates how well the binary tube image corresponds with their source image.
<ASSISTANT_TASK:>
Python Code:
import os
import sys
import numpy
import itk
from itk import TubeTK as ttk
from itkwidgets import view
Explanation: Convert Tubes To Images
This notebook contains a few examples of how to call wrapped methods in itk and ITKTubeTK.
ITK, ITKTubeTK, and ITKWidgets must be installed on your system for this notebook to work.
End of explanation
PixelType = itk.F
Dimension = 3
ImageType = itk.Image[PixelType, Dimension]
    
# Read tre file
TubeFileReaderType = itk.SpatialObjectReader[Dimension]
    
tubeFileReader = TubeFileReaderType.New()
tubeFileReader.SetFileName("Data/MRI-Normals/Normal071-VascularNetwork.tre")
tubeFileReader.Update()
tubes = tubeFileReader.GetGroup()
# Read template image
TemplateImageType = itk.Image[PixelType, Dimension]
TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]
    
templateImageReader = TemplateImageReaderType.New()
templateImageReader.SetFileName("Data/MRI-Normals/Normal071-MRA.mha")
templateImageReader.Update()
templateImage = templateImageReader.GetOutput()
Explanation: Load the tubes and a reference image which provides the size, spacing, origin, and orientation for the desired output image.
End of explanation
view(templateImage)
Explanation: Visualize the template image, just because it looks cool - the data in the image is actually irrelevant.
End of explanation
TubesToImageFilterType = ttk.ConvertTubesToImage[TemplateImageType]
tubesToImageFilter = TubesToImageFilterType.New()
tubesToImageFilter.SetUseRadius(True)
tubesToImageFilter.SetTemplateImage(templateImageReader.GetOutput())
tubesToImageFilter.SetInput(tubes)
tubesToImageFilter.Update()
outputImage = tubesToImageFilter.GetOutput()
Explanation: Create a binary image that represents the spatial extent of the TubeSpatialObjects in the hierarchy of SpatialObjects in the variable "tubes" that was read-in above.   If you only want to visualize centerlines of the tubes, set "UseRadius" to false.
End of explanation
TTKImageMathType = ttk.ImageMath[ImageType,ImageType]
imMath = TTKImageMathType.New(Input = outputImage)
imMath.AddImages(templateImage, 2048, 1)
combinedImage = imMath.GetOutput()
view(combinedImage)
Explanation: Visualize the results by blending the template and output images.  Again, the content of the template image
doesn't actually matter, but since these tubes were generated from the content of the template image, blending them illustrates how well the binary tube image corresponds with their source image.
End of explanation
<END_TASK>