ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Segmentation/Watersheds/MorphologicalWatershedSegmentation/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
*
* https://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.
*
*=========================================================================*/
#include <iostream>
// Run with:
// ./WatershedImageFilter threshold level
// e.g.
// ./WatershedImageFilter 0.005 .5
// (A rule of thumb is to set the Threshold to be about 1 / 100 of the Level.)
using UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;
using RGBPixelType = itk::RGBPixel<unsigned char>;
using RGBImageType = itk::Image<RGBPixelType, 2>;
using LabeledImageType = itk::Image<unsigned long, 2>;
static void
CreateImage(UnsignedCharImageType::Pointer image);
static void
PerformSegmentation(FloatImageType::Pointer image, const float threshold, const float level);
int
main(int argc, char * argv[])
{
// Verify arguments
if (argc < 3)
{
std::cerr << "Parameters " << std::endl;
std::cerr << " threshold level" << std::endl;
return 1;
}
// Parse arguments
std::string strThreshold = argv[1];
float threshold = 0.0;
std::stringstream ssThreshold;
ssThreshold << strThreshold;
ssThreshold >> threshold;
std::string strLevel = argv[2];
float level = 0.0;
std::stringstream ssLevel;
ssLevel << strLevel;
ssLevel >> level;
// Output arguments
std::cout << "Running with:" << std::endl
<< "Threshold: " << threshold << std::endl
<< "Level: " << level << std::endl;
CreateImage(image);
auto gradientMagnitudeImageFilter = GradientMagnitudeImageFilterType::New();
gradientMagnitudeImageFilter->SetInput(image);
gradientMagnitudeImageFilter->Update();
// Custom parameters
PerformSegmentation(gradientMagnitudeImageFilter->GetOutput(), threshold, level);
return EXIT_SUCCESS;
}
void
{
// Create a white image with 3 dark regions of different values
start.Fill(0);
size.Fill(200);
itk::ImageRegion<2> region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(255);
while (!imageIterator.IsAtEnd())
{
if (imageIterator.GetIndex()[0] > 20 && imageIterator.GetIndex()[0] < 50 && imageIterator.GetIndex()[1] > 20 &&
imageIterator.GetIndex()[1] < 50)
imageIterator.Set(50);
++imageIterator;
}
imageIterator.GoToBegin();
while (!imageIterator.IsAtEnd())
{
if (imageIterator.GetIndex()[0] > 60 && imageIterator.GetIndex()[0] < 80 && imageIterator.GetIndex()[1] > 60 &&
imageIterator.GetIndex()[1] < 80)
imageIterator.Set(100);
++imageIterator;
}
imageIterator.GoToBegin();
while (!imageIterator.IsAtEnd())
{
if (imageIterator.GetIndex()[0] > 100 && imageIterator.GetIndex()[0] < 130 && imageIterator.GetIndex()[1] > 100 &&
imageIterator.GetIndex()[1] < 130)
imageIterator.Set(150);
++imageIterator;
}
itk::WriteImage(image, "input.png");
}
void
PerformSegmentation(FloatImageType::Pointer image, const float threshold, const float level)
{
auto watershedFilter = MorphologicalWatershedFilterType::New();
watershedFilter->SetLevel(level);
watershedFilter->SetInput(image);
watershedFilter->Update();
auto colormapImageFilter = RGBFilterType::New();
colormapImageFilter->SetInput(watershedFilter->GetOutput());
colormapImageFilter->Update();
std::stringstream ss;
ss << "output_" << threshold << "_" << level << ".png";
itk::WriteImage(colormapImageFilter->GetOutput(), ss.str());
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::GradientMagnitudeImageFilter
Computes the gradient magnitude of an image region at each pixel.
Definition: itkGradientMagnitudeImageFilter.h:42
itkUnaryFunctorImageFilter.h
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:68
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::Size< 2 >
itkScalarToRGBPixelFunctor.h
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::ScalarToRGBColormapImageFilter
Implements pixel-wise intensity->rgb mapping operation on one image.
Definition: itkScalarToRGBColormapImageFilter.h:130
itkImageFileReader.h
itkVectorGradientAnisotropicDiffusionImageFilter.h
itk::ScalarToRGBColormapImageFilterEnums::RGBColormapFilter::Jet
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:272
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itkRescaleIntensityImageFilter.h
itkVectorMagnitudeImageFilter.h
itkImageFileWriter.h
itkScalarToRGBColormapImageFilter.h
itk::MorphologicalWatershedImageFilter
Watershed segmentation implementation with morphological operators.
Definition: itkMorphologicalWatershedImageFilter.h:55
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:256
itkMorphologicalWatershedImageFilter.h
itkGradientMagnitudeImageFilter.h