ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Segmentation/Watersheds/SegmentWithWatershedImageFilter/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Run with:
// ./SegmentWithWatershedImageFilter inputImageFile outputImageFile threshold level
// e.g.
// ./SegmentWithWatershedImageFilter BrainProtonDensitySlice.png OutBrainWatershed.png 0.005 .5
// (A rule of thumb is to set the Threshold to be about 1 / 100 of the Level.)
int
main(int argc, char * argv[])
{
if (argc < 5)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << argv[0] << " inputImageFile outputImageFile threshold level" << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
using InputImageType = itk::Image<unsigned char, Dimension>;
using FloatImageType = itk::Image<float, Dimension>;
using RGBPixelType = itk::RGBPixel<unsigned char>;
using FileReaderType = itk::ImageFileReader<InputImageType>;
FileReaderType::Pointer reader = FileReaderType::New();
reader->SetFileName(argv[1]);
GradientMagnitudeImageFilterType::Pointer gradientMagnitudeImageFilter = GradientMagnitudeImageFilterType::New();
gradientMagnitudeImageFilter->SetInput(reader->GetOutput());
gradientMagnitudeImageFilter->Update();
using WatershedFilterType = itk::WatershedImageFilter<FloatImageType>;
WatershedFilterType::Pointer watershed = WatershedFilterType::New();
float threshold = std::stod(argv[3]);
float level = std::stod(argv[4]);
watershed->SetThreshold(threshold);
watershed->SetLevel(level);
watershed->SetInput(gradientMagnitudeImageFilter->GetOutput());
watershed->Update();
RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New();
colormapImageFilter->SetInput(watershed->GetOutput());
colormapImageFilter->Update();
using FileWriterType = itk::ImageFileWriter<RGBImageType>;
FileWriterType::Pointer writer = FileWriterType::New();
writer->SetFileName(argv[2]);
writer->SetInput(colormapImageFilter->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}
itk::GradientMagnitudeImageFilter
Computes the gradient magnitude of an image region at each pixel.
Definition: itkGradientMagnitudeImageFilter.h:42
itkUnaryFunctorImageFilter.h
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itkScalarToRGBPixelFunctor.h
itk::ScalarToRGBColormapImageFilter
Implements pixel-wise intensity->rgb mapping operation on one image.
Definition: itkScalarToRGBColormapImageFilter.h:131
itkImageFileReader.h
itkVectorGradientAnisotropicDiffusionImageFilter.h
itk::WatershedImageFilter
A low-level image analysis algorithm that automatically produces a hierarchy of segmented,...
Definition: itkWatershedImageFilter.h:149
itk::ScalarToRGBColormapImageFilterEnums::RGBColormapFilter::Jet
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itkRescaleIntensityImageFilter.h
itkImageFileWriter.h
itkWatershedImageFilter.h
itkScalarToRGBColormapImageFilter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itkGradientMagnitudeImageFilter.h