ITK/Examples/ImageProcessing/ImageFilterOnARegion

From KitwarePublic

Jump to: navigation, search
ITK Examples Baseline ImageProcessing TestImageFilterOnARegion.png

This examples runs the MedianImageFilter on a region of the image. It uses the RequestRegion to tell the filter which region to process. Then the example uses PasteImageFilter to paste the processed region into the original image.

Contents

ImageFilterOnARegion.cxx

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkMedianImageFilter.h"
#include "itkPasteImageFilter.h"
#include "itkSubtractImageFilter.h"
 
#include "itksys/SystemTools.hxx"
 
#include <sstream>
 
#include "QuickView.h"
 
int main(int argc, char * argv[])
{
  // Verify command line arguments
  if( argc < 2 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " InputImageFile [radius]" << std::endl;
    return EXIT_FAILURE;
    }
  std::string inputFilename = argv[1];
 
  // Setup types
  typedef itk::Image<float, 2 >                         ImageType;
  typedef itk::ImageFileReader<ImageType>               ReaderType;
  typedef itk::MedianImageFilter<ImageType, ImageType > FilterType;
  typedef itk::SubtractImageFilter<ImageType>           SubtractType;
  typedef itk::PasteImageFilter<ImageType, ImageType >  PasteType;
 
  // Create and setup a reader
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( inputFilename );
 
  // Create and setup a median filter
  FilterType::Pointer medianFilter = FilterType::New();
  FilterType::InputSizeType radius;
  radius.Fill(2);
  if (argc > 2)
    {
    radius.Fill(atoi(argv[2]));
    }
 
  reader->Update();
 
  itk::Size<2> processSize;
  processSize[0] =
    reader->GetOutput()->GetLargestPossibleRegion().GetSize()[0] / 2;
  processSize[1] =
    reader->GetOutput()->GetLargestPossibleRegion().GetSize()[1] / 2;
 
  itk::Index<2> processIndex;
  processIndex[0] = processSize[0] / 2;
  processIndex[1] = processSize[1] / 2;
 
  itk::ImageRegion<2> processRegion(processIndex, processSize);
 
  medianFilter->SetRadius(radius);
  medianFilter->SetInput( reader->GetOutput() );
  medianFilter->GetOutput()->SetRequestedRegion( processRegion );
 
  PasteType::Pointer pasteFilter = PasteType::New();
 
  pasteFilter->SetSourceImage(medianFilter->GetOutput());
  pasteFilter->SetSourceRegion(medianFilter->GetOutput()->GetRequestedRegion());
  pasteFilter->SetDestinationImage(reader->GetOutput());
  pasteFilter->SetDestinationIndex(processIndex);
 
  SubtractType::Pointer diff = SubtractType::New();
  diff->SetInput1(reader->GetOutput());
  diff->SetInput2(pasteFilter->GetOutput());
 
  QuickView viewer;
  viewer.AddImage(
    reader->GetOutput(),true,
    itksys::SystemTools::GetFilenameName(inputFilename));  
 
  std::stringstream desc;
  desc << "Median/PasteImageFilter, radius = " << radius;
  viewer.AddImage(
    pasteFilter->GetOutput(),
    true,
    desc.str());  
 
  std::stringstream desc2;
  desc2 << "Original - Median/Paste";
  viewer.AddImage(
    diff->GetOutput(),
    true,
    desc2.str());  
 
  viewer.Visualize();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
project(ImageFilterOnARegion)
 
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
 
add_executable(ImageFilterOnARegion ImageFilterOnARegion.cxx)
target_link_libraries(ImageFilterOnARegion
  ItkVtkGlue  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

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

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.