ITK  6.0.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
*
* 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 "itkImage.h"
using UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;
static void
CreateImage(UnsignedCharImageType::Pointer image);
int
main()
{
CreateImage(image);
using ApproximateSignedDistanceMapImageFilterType =
ApproximateSignedDistanceMapImageFilterType::Pointer approximateSignedDistanceMapImageFilter =
approximateSignedDistanceMapImageFilter->SetInput(image);
approximateSignedDistanceMapImageFilter->SetInsideValue(255);
approximateSignedDistanceMapImageFilter->SetOutsideValue(0);
approximateSignedDistanceMapImageFilter->Update();
using ContourExtractor2DImageFilterType = itk::ContourExtractor2DImageFilter<FloatImageType>;
auto 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
{
// 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);
}
itk::WriteImage(image, "image.png");
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itkApproximateSignedDistanceMapImageFilter.h
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:68
itk::Size< 2 >
itk::ContourExtractor2DImageFilter
Computes a list of PolyLineParametricPath objects from the contours in a 2D image.
Definition: itkContourExtractor2DImageFilter.h:108
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itkImage.h
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:272
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itkContourExtractor2DImageFilter.h
itkImageFileWriter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ApproximateSignedDistanceMapImageFilter
Create a map of the approximate signed distance from the boundaries of a binary image.
Definition: itkApproximateSignedDistanceMapImageFilter.h:76
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:256