ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/Path/ExtractContoursFromImage/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 UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;
static void
CreateImage(UnsignedCharImageType::Pointer image);
int
main(int, char *[])
{
UnsignedCharImageType::Pointer image = UnsignedCharImageType::New();
CreateImage(image);
using ApproximateSignedDistanceMapImageFilterType =
ApproximateSignedDistanceMapImageFilterType::Pointer approximateSignedDistanceMapImageFilter =
ApproximateSignedDistanceMapImageFilterType::New();
approximateSignedDistanceMapImageFilter->SetInput(image);
approximateSignedDistanceMapImageFilter->SetInsideValue(255);
approximateSignedDistanceMapImageFilter->SetOutsideValue(0);
approximateSignedDistanceMapImageFilter->Update();
using ContourExtractor2DImageFilterType = itk::ContourExtractor2DImageFilter<FloatImageType>;
ContourExtractor2DImageFilterType::Pointer contourExtractor2DImageFilter = ContourExtractor2DImageFilterType::New();
contourExtractor2DImageFilter->SetInput(approximateSignedDistanceMapImageFilter->GetOutput());
contourExtractor2DImageFilter->SetContourValue(0);
contourExtractor2DImageFilter->Update();
std::cout << "There are " << contourExtractor2DImageFilter->GetNumberOfOutputs() << " contours" << std::endl;
for (unsigned int i = 0; i < contourExtractor2DImageFilter->GetNumberOfOutputs(); ++i)
{
std::cout << "Contour " << i << ": " << std::endl;
ContourExtractor2DImageFilterType::VertexListType::ConstIterator vertexIterator =
contourExtractor2DImageFilter->GetOutput(i)->GetVertexList()->Begin();
while (vertexIterator != contourExtractor2DImageFilter->GetOutput(i)->GetVertexList()->End())
{
std::cout << vertexIterator->Value() << std::endl;
++vertexIterator;
}
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
void
CreateImage(UnsignedCharImageType::Pointer image)
{
// Create an image
start.Fill(0);
size.Fill(100);
itk::ImageRegion<2> region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
// Create a line of white pixels
for (unsigned int i = 40; i < 60; ++i)
{
pixel.Fill(i);
image->SetPixel(pixel, 255);
}
// Create another line of pixels
for (unsigned int i = 10; i < 20; ++i)
{
pixel[0] = 10;
pixel[1] = i;
image->SetPixel(pixel, 255);
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("image.png");
writer->SetInput(image);
writer->Update();
}
itkApproximateSignedDistanceMapImageFilter.h
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::Size< 2 >
itk::ContourExtractor2DImageFilter
Computes a list of PolyLineParametricPath objects from the contours in a 2D image.
Definition: itkContourExtractor2DImageFilter.h:99
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itkImage.h
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itkContourExtractor2DImageFilter.h
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:87
itkImageFileWriter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::ApproximateSignedDistanceMapImageFilter
Create a map of the approximate signed distance from the boundaries of a binary image.
Definition: itkApproximateSignedDistanceMapImageFilter.h:76