ITK/Examples/Curves/ContourMeanDistanceImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Template the CreateImage* functions. Use image typedefs for index and size. Fix initialization problem that was leading to a few rogue pixels breaking the test comparison.)
No edit summary
Line 100: Line 100:
</source>
</source>


{{ITKVTKCMakeLists|ContourMeanDistanceImageFilter|}}
{{ITKVTKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 16:32, 24 December 2012

ITK Examples Baseline Curves TestContourMeanDistanceImageFilter.png

ContourMeanDistanceImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkContourMeanDistanceImageFilter.h"
  1. include "QuickView.h"

template<typename TImage> static void CreateImage1(TImage* const);

template<typename TImage> static void CreateImage2(TImage* const);

int main(int argc, char*argv[]) {

 typedef itk::Image<unsigned char, 2> ImageType;
 ImageType::Pointer image1 = ImageType::New();
 CreateImage1(image1.GetPointer());
 ImageType::Pointer image2 = ImageType::New();
 CreateImage2(image2.GetPointer());
 
 typedef itk::ContourMeanDistanceImageFilter <ImageType, ImageType >
   ContourMeanDistanceImageFilterType;
 
 ContourMeanDistanceImageFilterType::Pointer contourMeanDistanceImageFilter =
   ContourMeanDistanceImageFilterType::New();
 contourMeanDistanceImageFilter->SetInput1(image1);
 contourMeanDistanceImageFilter->SetInput2(image2);
 contourMeanDistanceImageFilter->Update();
 std::cout << "Mean distance: " << contourMeanDistanceImageFilter->GetMeanDistance() << std::endl;
 
 QuickView viewer;
 viewer.AddImage(image1.GetPointer());
 viewer.AddImage(image2.GetPointer());
 viewer.Visualize();
 return EXIT_SUCCESS;

}

template<typename TImage> void CreateImage1(TImage* const image) {

 // Create an image bigger than the input image and that has dimensions which are powers of two
 typename TImage::IndexType start = Template:0,0;
 typename TImage::SizeType size = Template:20,20;
 itk::ImageRegion<2> region(start, size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 // Create a diagonal white line through the image
 for(typename TImage::IndexValueType i = 0; i < 20; i++)
   {
   for(typename TImage::IndexValueType j = 0; j < 20; j++)
     {
     if(i == j)
       {
       itk::Index<2> pixel = Template:I,j;
       image->SetPixel(pixel, 255);
       }
     }
   }

}

template<typename TImage> void CreateImage2(TImage* const image) {

 typename TImage::IndexType start = Template:0,0;
 typename TImage::SizeType size = Template:20,20;
 itk::ImageRegion<2> region(start, size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 // Create a vertical line of white pixels down the center of the image
 for(typename TImage::IndexValueType i = 0; i < 20; i++)
   {
   for(typename TImage::IndexValueType j = 0; j < 20; j++)
     {
     if(i == 10)
       {
       itk::Index<2> pixel = Template:I,j;
       image->SetPixel(pixel, 255);
       }
     }
   }

}

</source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(ContourMeanDistanceImageFilter)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(ContourMeanDistanceImageFilter MACOSX_BUNDLE ContourMeanDistanceImageFilter.cxx) target_link_libraries(ContourMeanDistanceImageFilter

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build ContourMeanDistanceImageFilter

Click here to download ContourMeanDistanceImageFilter. and its CMakeLists.txt file. Once the tarball ContourMeanDistanceImageFilter.tar has been downloaded and extracted,

cd ContourMeanDistanceImageFilter/build 
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project,

make

and run it:

./ContourMeanDistanceImageFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.