ITK/Examples/ImageProcessing/ImageFilterOnARegion: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Deprecated content that is moved to sphinxe)
 
Line 1: Line 1:
<div class="floatcenter">[[File:ITK_Examples_Baseline_ImageProcessing_TestImageFilterOnARegion.png]]</div>
{{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.
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.
}}
==ImageFilterOnARegion.cxx==
<source lang="cpp">
#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;
}
 
</source>
 
{{ITKVTKCMakeLists|{{SUBPAGENAME}}}}

Latest revision as of 20:54, 5 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.