ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/MaskOneImageGivenLabelMap/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 "itkLabelObject.h"
#include "itkLabelMap.h"
int
main(int argc, char * argv[])
{
if ((argc != 6) && (argc != 9))
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <LabelMapFileName> <OutputFileName> <Label> <Background>";
std::cerr << " [<negated (bool)> <crop (bool)> <border size>]";
std::cerr << std::endl;
return EXIT_FAILURE;
}
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
const char * inputFileName = argv[1];
const char * labelMapFileName = argv[2];
const char * outputFileName = argv[3];
const auto label = static_cast<PixelType>(std::stoi(argv[4]));
const auto background = static_cast<PixelType>(std::stoi(argv[5]));
bool negated = false;
bool crop = false;
ImageType::SizeValueType borderSize = 0;
if (argc == 9)
{
negated = (std::stoi(argv[6]) == 1);
crop = (std::stoi(argv[7]) == 1);
borderSize = static_cast<ImageType::SizeValueType>(std::stoi(argv[8]));
}
const auto input1 = itk::ReadImage<ImageType>(inputFileName);
const auto input2 = itk::ReadImage<ImageType>(labelMapFileName);
using LabelObjectType = itk::LabelObject<PixelType, Dimension>;
using LabelMapType = itk::LabelMap<LabelObjectType>;
// convert the label image into a LabelMap
auto convert = LabelImage2LabelMapType::New();
convert->SetInput(input2);
auto filter = FilterType::New();
filter->SetInput(convert->GetOutput());
filter->SetFeatureImage(input1);
// The label to be used to mask the image is passed via SetLabel
filter->SetLabel(label);
// The background in the output image (where the image is masked)
// is passed via SetBackground
filter->SetBackgroundValue(background);
// The user can choose to mask the image outside the label object
// (default behavior), or inside the label object with the chosen label,
// by calling SetNegated().
filter->SetNegated(negated);
// Finally, the image can be cropped to the masked region, by calling
// SetCrop( true ), or to a region padded by a border, by calling both
// SetCrop() and SetCropBorder().
// The crop border defaults to 0, and the image is not cropped by default.
filter->SetCrop(crop);
border.Fill(borderSize);
filter->SetCropBorder(border);
try
{
itk::WriteImage(filter->GetOutput(), outputFileName);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itkLabelImageToLabelMapFilter.h
itk::LabelImageToLabelMapFilter
convert a labeled image to a label collection image
Definition: itkLabelImageToLabelMapFilter.h:49
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:213
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itk::LabelMapMaskImageFilter
Mask and image with a LabelMap.
Definition: itkLabelMapMaskImageFilter.h:47
itkLabelMap.h
itk::LabelObject
The base class for the representation of a labeled binary object in an image.
Definition: itkLabelObject.h:65
itkLabelObject.h
itkImageFileWriter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itkLabelMapMaskImageFilter.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::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83