ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/ExtractGivenLabelObject/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.
*
*=========================================================================*/
#include "itkLabelObject.h"
#include "itkLabelMap.h"
int
main(int argc, char * argv[])
{
if (argc != 5)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName1> <OutputFileName2> <Label>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
const char * inputFileName = argv[1];
const char * outputFileName[2];
outputFileName[0] = argv[2];
outputFileName[1] = argv[3];
const auto label = static_cast<PixelType>(std::stoi(argv[4]));
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);
SelectorType::Pointer selector = SelectorType::New();
selector->SetInput(labelMapConverter->GetOutput());
selector->SetLabel(label);
for (int i = 0; i < 2; i++)
{
using LabelMapToLabelImageFilterType = itk::LabelMapToLabelImageFilter<LabelMapType, ImageType>;
LabelMapToLabelImageFilterType::Pointer labelImageConverter = LabelMapToLabelImageFilterType::New();
labelImageConverter->SetInput(selector->GetOutput(i));
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName[i]);
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::LabelSelectionLabelMapFilter
remove the objects according to the value of their attribute
Definition: itkLabelSelectionLabelMapFilter.h:55
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
itkLabelSelectionLabelMapFilter.h
itkLabelMap.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
itkLabelObject.h
itkImageFileWriter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
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