Slicer Plug ins Part
Document Sample


Programming into Slicer3
Sonia Pujol, Ph.D.
Surgical Planning Laboratory
Harvard Medical School
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -1-
Slicer3
• An end-user application for image analysis
• An open-source environment for software
development
• A software platform that is both easy to use for
clinical researchers and easy to extend for
programmers
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -2-
Learning objective
Following this course, you’ll be able
1) to plug-in an external program into Slicer3
2) to implement an image filter and to run the
analysis from Slicer3
3) to write and run a test using the CTest tool
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -3-
Material
This course requires a user compiled version of the Slicer3 software
and the training dataset:
• Slicer 3 Software and building instructions
http://www.na-mic.org/Wiki/index.php/Slicer3:Downloads
http://www.na-mic.org/Wiki/index.php/Slicer3:Build_Instructions
• HelloWorld.zip archive
Disclaimer
It is the responsibility of the user of 3DSlicer to
comply with both the terms of the license and with
the applicable laws, regulations and rules.
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -4-
Overview
• Part A: integration of the HelloWorld program
into Slicer3
• Part B: implementation of a Discrete
Gaussian filter within the HelloWorld module
• Part C: implementation of a test for the
HelloWorld module
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -5-
Part A:
Integrating an
executable into Slicer3
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -6-
Slicer3 Execution Model
• This course is based on the Execution Model which
provides a mechanism for incorporating command
line programs as Slicer modules.
• The Slicer modules are described using XML files
which are used to generate the C++ command line
code and the Graphical User Interface (GUI).
www.na-mic.org/Wiki/index.php/Slicer3:Execution_Model_Documentation
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -7-
HelloWorld_CourseMaterial.tgz archive
unzip the HelloWorld.zip archive
HelloWorld.cxx
(application)
spgr.* HelloWorld.xml
(124 SPGR images) (Execution Model)
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -8-
HelloWorld.xml
<?xml version="1.0" encoding="utf-8"?>
<executable>
<category>
Demonstration</category> Open the file
<title>
Hello World</title> HelloWorld.xml
Module <description>
Slicer Developer Course</description>
Description <version>
1.0</version>
<documentation-url></documentation-url>
<license></license>
<contributor>
Sonia Pujol, Ph.D. </contributor>
<acknowledgements>
This work is part of the National Alliance for Medical Image Computing (NAMIC), funded by the National Institutes
of Health through the NIH Roadmap for Medical Research, Grant U54 EB005149. </acknowledgements>
<parameters>
<label>Input/Output</label>
<description>Input/output parameters</description>
<image>
Module <name>helloWorldInputVolume</name>
<label>Input Volume</label>
Parameters <channel>input</channel>
<index>0</index>
<default>None</default>
<description>Input volume</description>
</image>
<image>
<name>helloWorldOutputVolume</name>
<label>Output Volume</label>
<channel>output</channel>
<index>1</index>
<description>Output filtered</description>
Slicer3 for developers –
</image> Sonia Pujol, Ph.D.
</parameters>
National Alliance for Medical Image Computing
</executable> -9-
Module Description
<?xml version="1.0" encoding="utf-8"?>
<executable>
<category>
Demonstration</category>
<title>
Hello World</title>
<description>
Slicer Developer Course</description>
<version>
1.0</version>
<documentation-url></documentation-url>
<license></license>
<contributor> Sonia Pujol, Ph.D. </contributor>
<acknowledgements>
This work is part of the National Alliance for Medical Image
Computing (NAMIC), funded by the National Institutes of Health through the
NIH Roadmap for Medical Research, Grant U54 EB005149.
</acknowledgements>
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -10-
Module Parameters
<parameters>
<label>Input/Output</label>
<description>Input/output parameters</description>
<image>
<name>helloWorldInputVolume</name>
<label>Input Volume</label> A file that
Input <channel>input</channel> specifies
<index>0</index>
Volume <default>None</default>
the image
<description>Input volume</description>
</image>
<image>
<name>helloWorldOutputVolume</name>
<label>Output Volume</label>
Output <channel>output</channel>
<index>1</index>
Volume <default>None</default>
<description>Output filtered</description>
</image>
Slicer3 for developers – Sonia Pujol, Ph.D.
</parameters>
National Alliance for Medical Image Computing -11-
</executable>
Modifying the source code
Open the file HelloWorld.cxx
# include <iostream>
int main(int argc, char * argv [])
{
std::cout<< “Hello World !”<<std::endl;
return 0 ;
}
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -12-
Modifying the source code
Add the following lines to the file HelloWorld.cxx
# include <iostream>
#include “HelloWorldCLP.h”
int main(int argc, char * argv [])
{
PARSE_ARGS;
std::cout<< “Hello World !”<<std::endl;
return EXIT_SUCCESS ;
}
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -13-
Slicer architecture
.svn
Slicer3
Applications
Base
Slicer3-build CMake
Libs
Modules
Slicer3-lib Resources
Scripts
Testing
Utilities
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -14-
Slicer architecture
.svn
Slicer3
Applications
CLI
Base
Copy the 2 files CMake
HelloWorld.cxx
Libs
HelloWorld.xml
into the directory Modules
Slicer3\Applications\CLI
Resources
Scripts
Testing
Utilities
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -15-
Slicer architecture
.svn
Slicer3
Applications
CLI
Base
Open the file CMakeLists.txt
CMake
into the directory
Slicer3\Applications\CLI Libs
Modules
Resources
Scripts
Testing
Utilities
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -16-
Editing CMakeLists.txt – part 1
Add the following lines to CMakeLists.txt located
in Slicer3/Applications/CLI (above the SUBDIRS
lines):
SET (CLP HelloWorld)
SET (${CLP}_SOURCE ${CLP}.cxx)
GENERATECLP(${CLP}_SOURCE ${CLP}.xml)
GENERATECLP generates the file HelloWorldCLP.h for parsing
the command line arguments.
Slicer3 for developers – Sonia Pujol, Ph.D.
‘CLP’ means Command Line Processing
National Alliance for Medical Image Computing -17-
Editing CMakeLists.txt – part 2
Add the following lines to CMakeLists.txt located in
Slicer3/Applications/CLI after the ‘GENERATECLP’ line
you just added
ADD_EXECUTABLE(${CLP} ${${CLP}_SOURCE})
TARGET_LINK_LIBRARIES (${CLP} ITKIO
ITKBasicFilters ITKCommon)
ADD_EXECUTABLE creates the stand-alone executable
HelloWorld.exe that can be run from a command line.
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -18-
Building Slicer3
Mac/Linux
Run ‘make’ in the directory Slicer3-build/Applications/CLI
Windows
• Step1: Select BuildBuild Slicer3 to build the solution
Slicer3.sln located in Slicer3-build/
• Step2: Close the solution Slicer3, and re-load it again in the
building environment
• Step3: Select the solution HelloWorld, and launch
‘BuildHelloWorld’
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -19-
Running Slicer3
Mac/Linux
Run ‘./Slicer3’ in Slicer3-build/
Windows
Run ‘./Slicer3.exe’ in Slicer3-build/
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -20-
HelloWorld module in Slicer3
Select the category
‘Demonstration’, and the
module ‘HelloWorld’ in the
Modules menu
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -21-
HelloWorld Module in Slicer3
The program ‘HelloWorld’ is
now integrated into Slicer3
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -22-
Part B:
Implementing an
image filter
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -23-
Goal
• In this section, we’ll implement a Gaussian
smoothing operator to ‘blur’ the images and
remove detail and noise.
• This implementation will allow us to run the filter
on volumes loaded in Slicer, and to integrate the
resulting filtered volumes as MRML nodes.
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -24-
Discrete Gaussian Filter
Variance
Discrete
Gaussian Filter
Input volume Output volume
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -25-
Editing the file HelloWorld.xml
<?xml version="1.0" encoding="utf-8"?>
<executable>
<category>
Demonstration</category>
<title>
Hello World</title>
<description> Add a new parameter
Slicer Developer Example</description>
<version> group to HelloWorld.xml
1.0</version>
<documentation-url></documentation-url>
<license></license>
<contributor>
Sonia Pujol</contributor>
<acknowledgements>
This work is part of the National Alliance for Medical Image Computing (NAMIC), funded by the National Institutes of Health
through the NIH Roadmap for Medical Research, Grant U54 EB005149. </acknowledgements>
<parameters>
<label>Input/Output</label>
<description>Input/output parameters</description>
….
</parameters>
<parameters>
<label>Discrete Gaussian Parameters</label>
<description>Parameters of the Discrete Gaussian Filter </description>
</parameters>
</executable>
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -26-
Editing the file HelloWorld.xml
<parameters>
<label>Discrete Gaussian Parameters</label>
<description>Parameters of the Discrete Gaussian Filter </description>
<double>
<name>variance</name>
<longflag>--variance</longflag>
<description>Variance ( width of the filter kernel) </description>
<label>Variance</label>
<default>0.5</default>
</double>
</parameters>
‘variance’
Add the parameter– Sonia Pujol, Ph.D. which corresponds to the
Slicer3 for developers
the for Medical Image Computing
variance ofAlliance Discrete Gaussian Filter to HelloWorld.xml
National -27-
Implementing I/O functionalities
Add the following lines to HelloWorld.cxx
#include <iostream>
#include "HelloWorldCLP.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
int main(int argc, char * argv [])
{
PARSE_ARGS;
std::cout << "Hello World!" << std::endl;
return EXIT_SUCCESS ;
}
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -28-
Implementing Input/Output functionalities
Add the following command lines to set-up the reading
and writing functionalities in the ‘main’ procedure in
HelloWorld.cxx
int main ( int argc, char * argv[])
{
PARSE_ARGS;
std::cout << "Hello World!" << std::endl;
typedef itk::Image<short,3> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter<ImageType> WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
return EXIT_SUCCESS;
}
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -29-
Implementing Input/Output functionalities
Set the input and output volumes parameters defined in
HelloWorld.xml
int main ( int argc, char * argv[])
{
PARSE_ARGS;
std::cout << "Hello World!" << std::endl;
typedef itk::Image< short, 3 > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName(helloWorldInputVolume.c_str() );
writer->SetFileName (helloWorldOutputVolume.c_str());
return EXIT_SUCCESS;
}
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -30-
Implementing the filter in HelloWorld.cxx
Implement the filter itk::DiscreteGaussianImageFilter
#include "itkDiscreteGaussianImageFilter.h"
int main ( int argc, char * argv[])
{
PARSE_ARGS;
std::cout << "Hello World!" << std::endl;
typedef itk::Image< short, 3 > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( helloWorldInputVolume.c_str() );
writer->SetFileName(helloWorldOutputVolume.c_str());
typedef itk::DiscreteGaussianImageFilter <ImageType, ImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
return EXIT_SUCCESS;
Slicer3 for developers – Sonia Pujol, Ph.D.
} National Alliance for Medical Image Computing -31-
Implementing the filter in HelloWorld.cxx
int main ( int argc, char * argv[])
{
PARSE_ARGS; Add the following lines
std::cout << "Hello World!" << std::endl;
typedef itk::Image< short, 3 > ImageType;
for the filter execution:
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( helloWorldInputVolume.c_str() );
writer->SetFileName (helloWorldOutputVolume.c_str());
typedef itk::DiscreteGaussianImageFilter <ImageType, ImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
try {
filter->SetInput(reader->GetOutput());
filter->SetVariance(variance);
writer->SetInput(filter->GetOutput());
writer->Update();
}
catch (itk::ExceptionObject &excep){
std::cerr << argv[0] << ": exception caught !" << std::endl;
Slicer3 for developers – Sonia Pujol, Ph.D.
Nationalreturn EXIT_FAILURE;} Computing
Alliance for Medical Image -32-
return EXIT_SUCCESS;}
Final Version of HelloWorld.cxx
int main ( int argc, char * argv[])
{
PARSE_ARGS;
std::cout << "Hello World!" << std::endl;
typedef itk::Image< short, 3 > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( helloWorldInputVolume.c_str() );
writer->SetFileName (helloWorldOutputVolume.c_str());
typedef itk::DiscreteGaussianImageFilter <ImageType, ImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
try { - Linux and Mac:
filter->SetInput(reader->GetOutput());
filter->SetVariance(variance);
Type make in
writer->SetInput(filter->GetOutput()); Slicer3-build/
writer->Update();} - Windows: build
catch (itk::ExceptionObject &excep){ Slicer3.sln
std::cerr << argv[0] << ": exception caught !" << std::endl;
Slicer3 for developers – Sonia Pujol, Ph.D.
return EXIT_FAILURE;}
National Alliance for Medical Image Computing
return EXIT_SUCCESS;} -33-
Running Slicer3
Mac/Linux
Run ‘./Slicer3’ in Slicer3-build/
Windows
Run ‘./Slicer3.exe’ in Slicer3-build/
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -34-
Running the Filter
Go in FileAdd Volume and
load the dataset spgr.nrrd
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -35-
Running the Filter
Browse to the Category ‘Demonstration’
in the Modules menu, and select the
module ‘HelloWorld’
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -36-
Running the Filter
Select the input volume ‘spgr.nhdr’, the
output volume ‘Create New’.
Enter the value ‘0.9’ for the variance and
click on Apply
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -37-
Running the Filter
Slicer displays the filtered volume
‘HelloWorldVolume1’.
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -38-
Running the Filter
Select the Foreground volume
‘spgr.nhdr’ and fade between the
Background and Foreground images
to visualize the effect of the filter.
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -39-
Part C: Testing
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -40-
Goal
• This section describes a simple example for testing that
the ‘help’ functionality of our newly implemented module
‘HelloWorld’ works correctly.
• CTest is a core element of Slicer3’s quality control system
for software development.
http://www.cmake.org/Wiki/CMake_Testing_With_CTest
• The goal of ‘HelloWorldTest1’ is to test the following
command:
./HelloWorld --help
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -41-
HelloWorld Test 1
To implement this test, add the following lines to the
CMakeLists.txt file located in the directory:
Slicer3\Applications\CLI\CMakeLists.txt and build Slicer3
SET (SLICER_EXE ${Slicer3_BINARY_DIR}/Slicer3 )
IF (WIN32)
ADD_TEST(HelloWorldTest1 ${SLICER_EXE} --launch
${EXECUTABLE_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}/${CLP} --help)
ELSE(WIN32)
ADD_TEST(HelloWorldTest1 ${SLICER_EXE} --launch ${EXECUTABLE_OUTPUT_PATH}/${CLP} --help)
ENDIF (WIN32)
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -42-
HelloWorld Test 1
SET( SLICER_EXE ${Slicer3_BINARY_DIR}/Slicer3 )
IF (WIN32)
ADD_TEST(HelloWorldTest1 ${SLICER_EXE} --launch
${EXECUTABLE_OUTPUT_PATH}/${CMAKE_BUILD_TYPE}/${CLP} --help)
ELSE(WIN32)
ADD_TEST(HelloWorldTest1 ${SLICER_EXE} --launch
${EXECUTABLE_OUTPUT_PATH}/${CLP} --help)
ENDIF (WIN32)
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -43-
Running HelloWorldTest1
Mac/Linux
In Slicer3-build/ run
../Slicer3-lib/CMake-build/bin/ctest -R HelloWorldTest1
Windows
In Slicer3-build/ run
../Slicer3-lib/CMake-build/bin/ctest.exe -R HelloWorldTest1
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -44-
Running HelloWorldTest1
When the module successfully passes the test,
the output below is generated:
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -45-
Conclusion
• This course described functionalities for integrating,
developing and testing external program within Slicer3.
• The Execution Model of Slicer3 provides a simple
mechanism for incorporating command line programs
as Slicer modules.
• www.slicer.org
Slicer3 for developers – Sonia Pujol, Ph.D.
National Alliance for Medical Image Computing -46-
Get documents about "