ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageFilterBase/ApplyKernelToEveryPixelInNonZeroImage/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"
namespace
{
using UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;
} // namespace
static void
CreateImage(UnsignedCharImageType::Pointer image);
static void
CreateHalfMask(UnsignedCharImageType::Pointer image, UnsignedCharImageType::Pointer mask);
int
main(int, char *[])
{
UnsignedCharImageType::Pointer image = UnsignedCharImageType::New();
CreateImage(image);
UnsignedCharImageType::Pointer mask = UnsignedCharImageType::New();
CreateHalfMask(image, mask);
using SobelOperatorType = itk::SobelOperator<float, 2>;
SobelOperatorType sobelOperator;
itk::Size<2> radius;
radius.Fill(1); // a radius of 1x1 creates a 3x3 operator
sobelOperator.SetDirection(0); // Create the operator for the X axis derivative
sobelOperator.CreateToRadius(radius);
// Visualize mask image
using MaskNeighborhoodOperatorImageFilterType =
MaskNeighborhoodOperatorImageFilterType::Pointer maskNeighborhoodOperatorImageFilter =
MaskNeighborhoodOperatorImageFilterType::New();
maskNeighborhoodOperatorImageFilter->SetInput(image);
maskNeighborhoodOperatorImageFilter->SetMaskImage(mask);
maskNeighborhoodOperatorImageFilter->SetOperator(sobelOperator);
maskNeighborhoodOperatorImageFilter->Update();
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetInput(maskNeighborhoodOperatorImageFilter->GetOutput());
rescaleFilter->Update();
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("output.png");
writer->SetInput(rescaleFilter->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}
void
CreateHalfMask(UnsignedCharImageType::Pointer image, UnsignedCharImageType::Pointer mask)
{
itk::ImageRegion<2> region = image->GetLargestPossibleRegion();
mask->SetRegions(region);
mask->Allocate();
mask->FillBuffer(0);
itk::Size<2> regionSize = region.GetSize();
// 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;
}
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetInput(mask);
rescaleFilter->Update();
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("mask.png");
writer->SetInput(rescaleFilter->GetOutput());
writer->Update();
}
void
CreateImage(UnsignedCharImageType::Pointer image)
{
start.Fill(0);
size.Fill(100);
itk::ImageRegion<2> region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
// Make a square
for (unsigned int r = 30; r < 70; r++)
{
for (unsigned int c = 30; c < 70; c++)
{
pixelIndex[0] = r;
pixelIndex[1] = c;
image->SetPixel(pixelIndex, 255);
}
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("input.png");
writer->SetInput(image);
writer->Update();
}
itk::SobelOperator
A NeighborhoodOperator for performing a directional Sobel edge-detection operation at a pixel locatio...
Definition: itkSobelOperator.h:98
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::Size< 2 >
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itkImage.h
itk::ImageRegion::GetSize
const SizeType & GetSize() const
Definition: itkImageRegion.h:181
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
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:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::MaskNeighborhoodOperatorImageFilter
Applies a single NeighborhoodOperator to an image, processing only those pixels that are under a mask...
Definition: itkMaskNeighborhoodOperatorImageFilter.h:54
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itkSobelOperator.h
itkRescaleIntensityImageFilter.h
itkMaskImageFilter.h
itkImageFileWriter.h
itkMaskNeighborhoodOperatorImageFilter.h
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86