ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/ApplyMorphologicalClosingOnAllLabelObjects/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.
*
*=========================================================================*/
int
main(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName> <radius>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const unsigned int radiusValue = std::stoi(argv[3]);
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
const auto input = itk::ReadImage<ImageType>(inputFileName);
using LabelObjectType = itk::LabelObject<PixelType, Dimension>;
using LabelMapType = itk::LabelMap<LabelObjectType>;
using LabelImageToLabelMapFilterType = itk::LabelImageToLabelMapFilter<ImageType, LabelMapType>;
auto labelMapConverter = LabelImageToLabelMapFilterType::New();
labelMapConverter->SetInput(input);
labelMapConverter->SetBackgroundValue(itk::NumericTraits<PixelType>::Zero);
using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);
using MorphologicalFilterType =
auto closingFilter = MorphologicalFilterType::New();
using ObjectByObjectLabelMapFilterType = itk::ObjectByObjectLabelMapFilter<LabelMapType>;
auto objectByObjectLabelMapFilter = ObjectByObjectLabelMapFilterType::New();
objectByObjectLabelMapFilter->SetInput(labelMapConverter->GetOutput());
objectByObjectLabelMapFilter->SetBinaryInternalOutput(true);
objectByObjectLabelMapFilter->SetFilter(closingFilter);
using UniqueLabelMapFilterType = itk::LabelUniqueLabelMapFilter<LabelMapType>;
unique->SetInput(objectByObjectLabelMapFilter->GetOutput());
using LabelMapToLabelImageFilterType = itk::LabelMapToLabelImageFilter<LabelMapType, ImageType>;
auto labelImageConverter = LabelMapToLabelImageFilterType::New();
labelImageConverter->SetInput(unique->GetOutput());
try
{
itk::WriteImage(labelImageConverter->GetOutput(), outputFileName);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itkLabelImageToLabelMapFilter.h
itk::ObjectByObjectLabelMapFilter
ObjectByObjectLabelMapFilter applies an image pipeline to all the objects of a label map and produce ...
Definition: itkObjectByObjectLabelMapFilter.h:86
itk::BinaryMorphologicalClosingImageFilter
binary morphological closing of an image.
Definition: itkBinaryMorphologicalClosingImageFilter.h:58
itk::FlatStructuringElement
A class to support a variety of flat structuring elements, including versions created by decompositio...
Definition: itkKernelImageFilter.h:27
itk::LabelImageToLabelMapFilter
convert a labeled image to a label collection image
Definition: itkLabelImageToLabelMapFilter.h:49
itkImageFileReader.h
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itkBinaryMorphologicalClosingImageFilter.h
itkLabelUniqueLabelMapFilter.h
itk::LabelObject
The base class for the representation of a labeled binary object in an image.
Definition: itkLabelObject.h:65
itkImageFileWriter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itkObjectByObjectLabelMapFilter.h
itkFlatStructuringElement.h
itk::LabelUniqueLabelMapFilter
Make sure that the objects are not overlapping.
Definition: itkLabelUniqueLabelMapFilter.h:47
itk::LabelMapToLabelImageFilter
Converts a LabelMap to a labeled image.
Definition: itkLabelMapToLabelImageFilter.h:46
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itkLabelMapToLabelImageFilter.h
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