Sobel Edge Detection Image Filter#

Synopsis#

Apply SobelEdgeDetectionImageFilter to an image

Results#

Input image

Input image#

Rescaled Output image

Rescaled Output image (values are in [0, 255])#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Sobel Edge Detection Image Filter.")
parser.add_argument("input_image")
parser.add_argument("output_image")
args = parser.parse_args()

input_image = itk.imread(args.input_image, pixel_type=itk.F)

output_image = itk.sobel_edge_detection_image_filter(input_image)

itk.imwrite(output_image, args.output_image)

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSobelEdgeDetectionImageFilter.h"

int
main(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << "<InputFileName> <OutputFileName>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int Dimension = 2;

  using InputPixelType = unsigned char;
  using InputImageType = itk::Image<InputPixelType, Dimension>;

  const auto input = itk::ReadImage<InputImageType>(argv[1]);

  using OutputPixelType = float;
  using OutputImageType = itk::Image<OutputPixelType, Dimension>;

  using FilterType = itk::SobelEdgeDetectionImageFilter<InputImageType, OutputImageType>;
  auto filter = FilterType::New();
  filter->SetInput(input);

  try
  {
    itk::WriteImage(filter->GetOutput(), argv[2]);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class SobelEdgeDetectionImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

A 2D or 3D edge detection using the Sobel operator.

This filter uses the Sobel operator to calculate the image gradient and then finds the magnitude of this gradient vector. The Sobel gradient magnitude (square-root sum of squares) is an indication of edge strength.

See

ImageToImageFilter

See

SobelOperator

See

Neighborhood

See

NeighborhoodOperator

See

NeighborhoodIterator

ITK Sphinx Examples:

See itk::SobelEdgeDetectionImageFilter for additional documentation.