ITK/Examples/ImageProcessing/BinaryThinningImageFilter

From KitwarePublic

Jump to: navigation, search

BinaryThinningImageFilter.cxx

#include "itkBinaryThinningImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
 
typedef itk::Image<unsigned char, 2>  ImageType;
 
static void WriteImage(const ImageType::Pointer image, const std::string& fileName);
static void CreateImage(ImageType::Pointer image);
 
int main(int argc, char *argv[])
{
  ImageType::Pointer image = ImageType::New();
  if( argc == 2)
    {
    typedef itk::ImageFileReader<ImageType> ImageReader;
    ImageReader::Pointer reader = ImageReader::New();
    std::string fileName = argv[1];
    reader->SetFileName(fileName);
    reader->Update();
    image = reader->GetOutput();
    }
  else
    {
    CreateImage(image);
    WriteImage(image, "input.png");
    }
 
  typedef itk::BinaryThinningImageFilter <ImageType, ImageType> BinaryThinningImageFilterType;
  BinaryThinningImageFilterType::Pointer binaryThinningImageFilter = BinaryThinningImageFilterType::New();
  binaryThinningImageFilter->SetInput(image);
  binaryThinningImageFilter->Update();
 
  // Rescale the image so that it can be seen (the output is 0 and 1, we want 0 and 255)
  typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleType;
  RescaleType::Pointer rescaler = RescaleType::New();
  rescaler->SetInput( binaryThinningImageFilter->GetOutput() );
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
  rescaler->Update();
 
  WriteImage(rescaler->GetOutput(), "output.png");
 
  return EXIT_SUCCESS;
}
 
void CreateImage(ImageType::Pointer image)
{
  // Create an image
  ImageType::IndexType start;
  start.Fill(0);
 
  ImageType::SizeType size;
  size.Fill(100);
 
  ImageType::RegionType region(start,size);
 
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(0);
 
  // Draw a 5 pixel wide line
  for(unsigned int i = 20; i < 80; ++i)
    {
    for(unsigned int j = 50; j < 55; ++j)
      {
      itk::Index<2> index;
      index[0] = i;
      index[1] = j;
      image->SetPixel(index, 255);
      }
    }
}
 
void WriteImage(const ImageType::Pointer image, const std::string& fileName)
{
  typedef  itk::ImageFileWriter< ImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(fileName);
  writer->SetInput(image);
  writer->Update();
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
project(BinaryThinningImageFilter)
 
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
 
add_executable(BinaryThinningImageFilter BinaryThinningImageFilter.cxx)
 
if( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(BinaryThinningImageFilter ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(BinaryThinningImageFilter ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )
Personal tools