ITK  5.3.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>;
const auto input = itk::ReadImage<InputImageType>(argv[1]);
GradientMagnitudeImageFilterType::Pointer gradientMagnitudeImageFilter = GradientMagnitudeImageFilterType::New();
gradientMagnitudeImageFilter->SetInput(input);
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();
itk::WriteImage(colormapImageFilter->GetOutput(), argv[2]);
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:130
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
itkRescaleIntensityImageFilter.h
itkImageFileWriter.h
itkWatershedImageFilter.h
itkScalarToRGBColormapImageFilter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:254
itkGradientMagnitudeImageFilter.h