ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/LabelMap/ShapeAttributesForBinaryImage/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"
template <typename TImage>
static void
CreateImage(TImage * image);
int
main(int argc, char * argv[])
{
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using LabelType = unsigned short;
using InputImageType = itk::Image<PixelType, Dimension>;
using OutputImageType = itk::Image<LabelType, Dimension>;
using ShapeLabelObjectType = itk::ShapeLabelObject<LabelType, Dimension>;
std::string fileName;
if (argc < 2)
{
CreateImage(image.GetPointer());
fileName = "Generated image";
}
else
{
fileName = argv[1];
image = itk::ReadImage<InputImageType>(fileName);
}
connected->SetInput(image);
connected->Update();
auto i2l = I2LType::New();
i2l->SetInput(connected->GetOutput());
i2l->SetComputePerimeter(true);
i2l->Update();
LabelMapType * labelMap = i2l->GetOutput();
std::cout << "File "
<< "\"" << fileName << "\""
<< " has " << labelMap->GetNumberOfLabelObjects() << " labels." << std::endl;
// Retrieve all attributes
for (unsigned int n = 0; n < labelMap->GetNumberOfLabelObjects(); ++n)
{
ShapeLabelObjectType * labelObject = labelMap->GetNthLabelObject(n);
std::cout << "Label: " << itk::NumericTraits<LabelMapType::LabelType>::PrintType(labelObject->GetLabel())
<< std::endl;
std::cout << " BoundingBox: " << labelObject->GetBoundingBox() << std::endl;
std::cout << " NumberOfPixels: " << labelObject->GetNumberOfPixels() << std::endl;
std::cout << " PhysicalSize: " << labelObject->GetPhysicalSize() << std::endl;
std::cout << " Centroid: " << labelObject->GetCentroid() << std::endl;
std::cout << " NumberOfPixelsOnBorder: " << labelObject->GetNumberOfPixelsOnBorder() << std::endl;
std::cout << " PerimeterOnBorder: " << labelObject->GetPerimeterOnBorder() << std::endl;
std::cout << " FeretDiameter: " << labelObject->GetFeretDiameter() << std::endl;
std::cout << " PrincipalMoments: " << labelObject->GetPrincipalMoments() << std::endl;
std::cout << " PrincipalAxes: " << labelObject->GetPrincipalAxes() << std::endl;
std::cout << " Elongation: " << labelObject->GetElongation() << std::endl;
std::cout << " Perimeter: " << labelObject->GetPerimeter() << std::endl;
std::cout << " Roundness: " << labelObject->GetRoundness() << std::endl;
std::cout << " EquivalentSphericalRadius: " << labelObject->GetEquivalentSphericalRadius() << std::endl;
std::cout << " EquivalentSphericalPerimeter: " << labelObject->GetEquivalentSphericalPerimeter() << std::endl;
std::cout << " EquivalentEllipsoidDiameter: " << labelObject->GetEquivalentEllipsoidDiameter() << std::endl;
std::cout << " Flatness: " << labelObject->GetFlatness() << std::endl;
std::cout << " PerimeterOnBorderRatio: " << labelObject->GetPerimeterOnBorderRatio() << std::endl;
}
return EXIT_SUCCESS;
}
template <typename TImage>
void
CreateImage(TImage * const image)
{
// Create an image with 2 objects
typename TImage::IndexType start = { { 0, 0 } };
start[0] = 0;
start[1] = 0;
typename TImage::SizeType size;
unsigned int NumRows = 200;
unsigned int NumCols = 300;
size[0] = NumRows;
size[1] = NumCols;
typename TImage::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
// Make a square
for (typename TImage::IndexValueType r = 20; r < 80; ++r)
{
for (typename TImage::IndexValueType c = 30; c < 100; ++c)
{
typename TImage::IndexType pixelIndex = { { r, c } };
image->SetPixel(pixelIndex, 255);
}
}
// Make another square
for (typename TImage::IndexValueType r = 100; r < 130; ++r)
{
for (typename TImage::IndexValueType c = 115; c < 160; ++c)
{
typename TImage::IndexType pixelIndex = { { r, c } };
image->SetPixel(pixelIndex, 255);
}
}
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itkLabelImageToShapeLabelMapFilter.h
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::IndexValueType
long IndexValueType
Definition: itkIntTypes.h:90
itk::LabelMap
Templated n-dimensional image to store labeled objects.
Definition: itkLabelMap.h:70
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::NumericTraits::PrintType
T PrintType
Definition: itkNumericTraits.h:69
itk::ShapeLabelObject
A Label object to store the common attributes related to the shape of the object.
Definition: itkShapeLabelObject.h:43
itk::LabelImageToShapeLabelMapFilter
Converts a label image to a label map and valuates the shape attributes.
Definition: itkLabelImageToShapeLabelMapFilter.h:50
itkConnectedComponentImageFilter.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ConnectedComponentImageFilter
Label the objects in a binary image.
Definition: itkConnectedComponentImageFilter.h:59
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44