Difference between revisions of "ITK/Examples/Curves/ContourMeanDistanceImageFilter"

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Removed unused includes.)
(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.)
Line 5: Line 5:
 
#include "itkImageFileReader.h"
 
#include "itkImageFileReader.h"
 
#include "itkContourMeanDistanceImageFilter.h"
 
#include "itkContourMeanDistanceImageFilter.h"
 
#include <itksys/SystemTools.hxx>
 
#include "vnl/vnl_sample.h"
 
#include <math.h>
 
  
 
#include "QuickView.h"
 
#include "QuickView.h"
  
typedef itk::Image<unsigned char, 2> ImageType;
+
template<typename TImage>
 +
static void CreateImage1(TImage* const);
  
static void CreateImage1(ImageType::Pointer);
+
template<typename TImage>
static void CreateImage2(ImageType::Pointer);
+
static void CreateImage2(TImage* const);
  
 
int main(int argc, char*argv[])
 
int main(int argc, char*argv[])
 
{
 
{
 +
  typedef itk::Image<unsigned char, 2> ImageType;
 +
 
   ImageType::Pointer image1 = ImageType::New();
 
   ImageType::Pointer image1 = ImageType::New();
   CreateImage1(image1);
+
   CreateImage1(image1.GetPointer());
  
 
   ImageType::Pointer image2 = ImageType::New();
 
   ImageType::Pointer image2 = ImageType::New();
   CreateImage2(image2);
+
   CreateImage2(image2.GetPointer());
 
    
 
    
 
   typedef itk::ContourMeanDistanceImageFilter <ImageType, ImageType >
 
   typedef itk::ContourMeanDistanceImageFilter <ImageType, ImageType >
Line 44: Line 43:
 
}
 
}
  
void CreateImage1(ImageType::Pointer image)
+
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
 
   // Create an image bigger than the input image and that has dimensions which are powers of two
   itk::Index<2> start;
+
   typename TImage::IndexType start = {{0,0}};
  start.Fill(0);
 
  
   itk::Size<2> size;
+
   typename TImage::SizeType size = {{20,20}};
  size.Fill(20);
 
  
 
   itk::ImageRegion<2> region(start, size);
 
   itk::ImageRegion<2> region(start, size);
Line 57: Line 55:
 
   image->SetRegions(region);
 
   image->SetRegions(region);
 
   image->Allocate();
 
   image->Allocate();
 +
  image->FillBuffer(0);
  
   for(unsigned int i = 0; i < 20; i++)
+
  // Create a diagonal white line through the image
 +
   for(typename TImage::IndexValueType i = 0; i < 20; i++)
 
     {
 
     {
     for(unsigned int j = 0; j < 20; j++)
+
     for(typename TImage::IndexValueType j = 0; j < 20; j++)
 
       {
 
       {
       if(i == j) // y = x
+
       if(i == j)
 
         {
 
         {
         itk::Index<2> pixel;
+
         itk::Index<2> pixel = {{i,j}};
        pixel[0] = i;
 
        pixel[1] = j;
 
 
         image->SetPixel(pixel, 255);
 
         image->SetPixel(pixel, 255);
 
         }
 
         }
Line 73: Line 71:
 
}
 
}
  
void CreateImage2(ImageType::Pointer image)
+
template<typename TImage>
 +
void CreateImage2(TImage* const image)
 
{
 
{
   // Create an image bigger than the input image and that has dimensions which are powers of two
+
   typename TImage::IndexType start = {{0,0}};
  itk::Index<2> start;
 
  start.Fill(0);
 
  
   itk::Size<2> size;
+
   typename TImage::SizeType size = {{20,20}};
  size.Fill(20);
 
  
 
   itk::ImageRegion<2> region(start, size);
 
   itk::ImageRegion<2> region(start, size);
Line 86: Line 82:
 
   image->SetRegions(region);
 
   image->SetRegions(region);
 
   image->Allocate();
 
   image->Allocate();
 +
  image->FillBuffer(0);
  
   for(unsigned int i = 0; i < 20; i++)
+
  // Create a vertical line of white pixels down the center of the image
 +
   for(typename TImage::IndexValueType i = 0; i < 20; i++)
 
     {
 
     {
     for(unsigned int j = 0; j < 20; j++)
+
     for(typename TImage::IndexValueType j = 0; j < 20; j++)
 
       {
 
       {
 
       if(i == 10)
 
       if(i == 10)
 
         {
 
         {
         itk::Index<2> pixel;
+
         itk::Index<2> pixel = {{i,j}};
        pixel[0] = i;
 
        pixel[1] = j;
 
 
         image->SetPixel(pixel, 255);
 
         image->SetPixel(pixel, 255);
 
         }
 
         }
Line 101: Line 97:
 
     }
 
     }
 
}
 
}
 +
 
</source>
 
</source>
  
 
{{ITKVTKCMakeLists|ContourMeanDistanceImageFilter|}}
 
{{ITKVTKCMakeLists|ContourMeanDistanceImageFilter|}}

Revision as of 07:30, 20 October 2012

ITK Examples Baseline Curves TestContourMeanDistanceImageFilter.png

ContourMeanDistanceImageFilter.cxx

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkContourMeanDistanceImageFilter.h"

#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 = {{0,0}};

  typename TImage::SizeType size = {{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 = {{i,j}};
        image->SetPixel(pixel, 255);
        }
      }
    }
}

template<typename TImage>
void CreateImage2(TImage* const image)
{
  typename TImage::IndexType start = {{0,0}};

  typename TImage::SizeType size = {{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 = {{i,j}};
        image->SetPixel(pixel, 255);
        }
      }
    }
}

CMakeLists.txt

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})

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.