ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageIntensity/InverseOfMaskToImage/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 "itkImage.h"
using ImageType = itk::Image<unsigned char, 2>;
static void
CreateHalfMask(ImageType::Pointer image, ImageType::Pointer mask);
static void
CreateImage(ImageType::Pointer image);
int
main(int, char *[])
{
ImageType::Pointer image = ImageType::New();
CreateImage(image);
ImageType::Pointer mask = ImageType::New();
CreateHalfMask(image, mask);
using MaskNegatedImageFilterType = itk::MaskNegatedImageFilter<ImageType, ImageType>;
MaskNegatedImageFilterType::Pointer maskNegatedImageFilter = MaskNegatedImageFilterType::New();
maskNegatedImageFilter->SetInput(image);
maskNegatedImageFilter->SetMaskImage(mask);
maskNegatedImageFilter->Update();
;
using FileWriterType = itk::ImageFileWriter<ImageType>;
FileWriterType::Pointer writer = FileWriterType::New();
writer->SetFileName("output.png");
writer->SetInput(maskNegatedImageFilter->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}
void
CreateHalfMask(ImageType::Pointer image, ImageType::Pointer mask)
{
ImageType::RegionType region = image->GetLargestPossibleRegion();
mask->SetRegions(region);
mask->Allocate();
ImageType::SizeType regionSize = region.GetSize();
itk::ImageRegionIterator<ImageType> imageIterator(mask, region);
// Make the left half of the mask white and the right half black
while (!imageIterator.IsAtEnd())
{
if (static_cast<unsigned int>(imageIterator.GetIndex()[0]) > regionSize[0] / 2)
{
imageIterator.Set(0);
}
else
{
imageIterator.Set(1);
}
++imageIterator;
}
}
void
CreateImage(ImageType::Pointer image)
{
start.Fill(0);
size.Fill(100);
ImageType::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(122);
}
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itkMaskNegatedImageFilter.h
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:78
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::MaskNegatedImageFilter
Mask an image with the negation (or logical compliment) of a mask.
Definition: itkMaskNegatedImageFilter.h:139
itkImageFileWriter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::Size::GetSize
const SizeValueType * GetSize() const
Definition: itkSize.h:169