Find Zero Crossings In Signed Image

Synopsis

Find zero crossings in a signed image.

Results

Warning

Fix Errors Example contains errors needed to be fixed for proper output.

Code

C++

#include "itkImageFileReader.h"
#include "itkZeroCrossingImageFilter.h"

#include "itksys/SystemTools.hxx"
#include <sstream>

#ifdef ENABLE_QUICKVIEW
#  include "QuickView.h"
#endif

using ImageType = itk::Image<float, 2>;

static void
CreateImage(ImageType::Pointer image);

int
main(int argc, char * argv[])
{
  ImageType::Pointer image = ImageType::New();
  if (argc < 2)
  {
    CreateImage(image);
  }
  else
  {
    using ReaderType = itk::ImageFileReader<ImageType>;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();
    image = reader->GetOutput();
  }

  using FilterType = itk::ZeroCrossingImageFilter<ImageType, ImageType>;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(image);
  filter->SetBackgroundValue(0);
  filter->SetForegroundValue(255);

#ifdef ENABLE_QUICKVIEW
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(), true, argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");

  std::stringstream desc;
  desc << "Zero Crossing";
  viewer.AddImage(filter->GetOutput(), true, desc.str());

  viewer.Visualize();
#endif

  return EXIT_SUCCESS;
}

void
CreateImage(ImageType::Pointer image)
{
  itk::Index<2> start;
  start.Fill(0);

  itk::Size<2> size;
  size.Fill(100);

  itk::ImageRegion<2> region(start, size);

  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(-1);

  // Make half of the image negative
  for (unsigned int i = 0; i < 100; ++i)
  {
    for (unsigned int j = 0; j < 50; ++j)
    {
      itk::Index<2> index;
      index[0] = i;
      index[1] = j;
      image->SetPixel(index, 1);
    }
  }
}

Classes demonstrated

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

This filter finds the closest pixel to the zero-crossings (sign changes) in a signed itk::Image.

Pixels closest to zero-crossings are labeled with a foreground value. All other pixels are marked with a background value. The algorithm works by detecting differences in sign among neighbors using city-block style connectivity (4-neighbors in 2d, 6-neighbors in 3d, etc.).

Inputs and Outputs

The input to this filter is an itk::Image of arbitrary dimension. The algorithm assumes a signed data type (zero-crossings are not defined for unsigned data types), and requires that operator>, operator<, operator==, and operator!= are defined.

The output of the filter is a binary, labeled image of user-specified type. By default, zero-crossing pixels are labeled with a default “foreground” value of itk::NumericTraits<OutputDataType>::OneValue(), where OutputDataType is the data type of the output image. All other pixels are labeled with a default “background” value of itk::NumericTraits<OutputDataType>::ZeroValue().

Parameters

There are two parameters for this filter. ForegroundValue is the value that marks zero-crossing pixels. The BackgroundValue is the value given to all other pixels.

See

Image

See

Neighborhood

See

NeighborhoodOperator

See

NeighborhoodIterator

ITK Sphinx Examples:

See itk::ZeroCrossingImageFilter for additional documentation.