ITK/Examples/EdgesAndGradients/GradientMagnitudeImageFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 01:16, 14 October 2010 by Daviddoria (talk | contribs) (Created page with "==GradientMagnitudeImageFilter.cxx== <source lang="cpp"> #include <string> #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkRes...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

GradientMagnitudeImageFilter.cxx

#include <string>

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkGradientMagnitudeImageFilter.h"

int main(int argc, char * argv[])
{
  // Verify command line arguments
  if( argc < 3 )
    {
	  std::cerr << "Usage: " << std::endl;
	  std::cerr << argv[0] << "  inputImageFile  outputImageFile " << std::endl;
	  return EXIT_FAILURE;
    }

  // Parse command line arguments
  std::string inputFilename = argv[1];
  std::string outputFilename = argv[2];

  // Setup types
  typedef    float    inputPixelType;
  typedef    float    outputPixelType;

  typedef itk::Image< inputPixelType,  2 >   inputImageType;
  typedef itk::Image< outputPixelType, 2 >   outputImageType;

  typedef itk::ImageFileReader< inputImageType >  readerType;

  typedef itk::GradientMagnitudeImageFilter<
		  inputImageType, outputImageType >  filterType;

  // Create and setup a reader
  readerType::Pointer reader = readerType::New();
  reader->SetFileName( inputFilename.c_str() );

  // Create and setup a gradient filter
  filterType::Pointer gradientFilter = filterType::New();
  gradientFilter->SetInput( reader->GetOutput() );
  gradientFilter->Update();

  // To write the gradient image file, we must rescale the gradient values
  // to a reasonable range
  typedef unsigned char writePixelType;
  typedef itk::Image< writePixelType, 2 > writeImageType;
  typedef itk::RescaleIntensityImageFilter<
		  outputImageType, writeImageType > rescaleFilterType;

  rescaleFilterType::Pointer rescaler = rescaleFilterType::New();
  rescaler->SetOutputMinimum(   0 );
  rescaler->SetOutputMaximum( 255 );
  rescaler->SetInput( gradientFilter->GetOutput() );

  // Create and setup a writer
  typedef itk::ImageFileWriter< writeImageType >  writerType;
  writerType::Pointer writer = writerType::New();
  writer->SetFileName( outputFilename.c_str() );
  writer->SetInput( rescaler->GetOutput() );
  writer->Update();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(GradientMagnitudeImageFilter)

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(GradientMagnitudeImageFilter GradientMagnitudeImageFilter.cxx)
TARGET_LINK_LIBRARIES(Gradient ITKNumerics ITKIO ITKAlgorithms)