ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/ApplyMorphologicalClosingOnSpecificLabelObject/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 != 5)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName> <label> <radius>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const auto label = static_cast<PixelType>(std::stoi(argv[3]));
const unsigned int radiusValue = std::stoi(argv[4]);
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);
auto selector = SelectorType::New();
selector->SetInput(labelMapConverter->GetOutput());
selector->SetLabel(label);
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(selector->GetOutput(0));
objectByObjectLabelMapFilter->SetBinaryInternalOutput(true);
objectByObjectLabelMapFilter->SetFilter(closingFilter);
using MergeLabelFilterType = itk::MergeLabelMapFilter<LabelMapType>;
auto merger = MergeLabelFilterType::New();
merger->SetInput(0, objectByObjectLabelMapFilter->GetOutput(0));
merger->SetInput(1, selector->GetOutput(1));
using UniqueLabelMapFilterType = itk::LabelUniqueLabelMapFilter<LabelMapType>;
unique->SetInput(merger->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::LabelSelectionLabelMapFilter
remove the objects according to the value of their attribute
Definition: itkLabelSelectionLabelMapFilter.h:54
itk::LabelImageToLabelMapFilter
convert a labeled image to a label collection image
Definition: itkLabelImageToLabelMapFilter.h:49
itkImageFileReader.h
itkMergeLabelMapFilter.h
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itk::MergeLabelMapFilterEnums::ChoiceMethod::KEEP
itkLabelSelectionLabelMapFilter.h
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
itk::MergeLabelMapFilter
Merges several Label Maps.
Definition: itkMergeLabelMapFilter.h:101