ITK  5.1.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/ApplyMorphologicalClosingOnAllLabelObjects/Code.cxx
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.
*
*=========================================================================*/
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;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
using LabelObjectType = itk::LabelObject<PixelType, Dimension>;
using LabelMapType = itk::LabelMap<LabelObjectType>;
using LabelImageToLabelMapFilterType = itk::LabelImageToLabelMapFilter<ImageType, LabelMapType>;
LabelImageToLabelMapFilterType::Pointer labelMapConverter = LabelImageToLabelMapFilterType::New();
labelMapConverter->SetInput(reader->GetOutput());
labelMapConverter->SetBackgroundValue(itk::NumericTraits<PixelType>::Zero);
using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);
using MorphologicalFilterType =
MorphologicalFilterType::Pointer closingFilter = MorphologicalFilterType::New();
using ObjectByObjectLabelMapFilterType = itk::ObjectByObjectLabelMapFilter<LabelMapType>;
ObjectByObjectLabelMapFilterType::Pointer objectByObjectLabelMapFilter = ObjectByObjectLabelMapFilterType::New();
objectByObjectLabelMapFilter->SetInput(labelMapConverter->GetOutput());
objectByObjectLabelMapFilter->SetBinaryInternalOutput(true);
objectByObjectLabelMapFilter->SetFilter(closingFilter);
using UniqueLabelMapFilterType = itk::LabelUniqueLabelMapFilter<LabelMapType>;
UniqueLabelMapFilterType::Pointer unique = UniqueLabelMapFilterType::New();
unique->SetInput(objectByObjectLabelMapFilter->GetOutput());
using LabelMapToLabelImageFilterType = itk::LabelMapToLabelImageFilter<LabelMapType, ImageType>;
LabelMapToLabelImageFilterType::Pointer labelImageConverter = LabelMapToLabelImageFilterType::New();
labelImageConverter->SetInput(unique->GetOutput());
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(labelImageConverter->GetOutput());
try
{
writer->Update();
}
catch (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:87
itk::BinaryMorphologicalClosingImageFilter
binary morphological closing of an image.
Definition: itkBinaryMorphologicalClosingImageFilter.h:59
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:50
itkImageFileReader.h
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:71
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itkBinaryMorphologicalClosingImageFilter.h
itkLabelUniqueLabelMapFilter.h
itk::LabelObject
The base class for the representation of an labeled binary object in an image.
Definition: itkLabelObject.h:65
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itkImageFileWriter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itkObjectByObjectLabelMapFilter.h
itkFlatStructuringElement.h
itk::LabelUniqueLabelMapFilter
Make sure that the objects are not overlapping.
Definition: itkLabelUniqueLabelMapFilter.h:48
itk::LabelMapToLabelImageFilter
Converts a LabelMap to a labeled image.
Definition: itkLabelMapToLabelImageFilter.h:47
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itkLabelMapToLabelImageFilter.h
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44