ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Segmentation/ConnectedComponents/ExtraLargestConnectComponentFromBinaryImage/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"
#include "itksys/SystemTools.hxx"
#include <sstream>
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cout << "Usage:" << std::endl;
std::cout << argv[0] << " InputFileName" << std::endl;
}
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
const auto input = itk::ReadImage<ImageType>(argv[1]);
using OutputImageType = itk::Image<unsigned short, Dimension>;
using ConnectedComponentImageFilterType = itk::ConnectedComponentImageFilter<ImageType, OutputImageType>;
connected->SetInput(input);
connected->Update();
std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;
using LabelShapeKeepNObjectsImageFilterType = itk::LabelShapeKeepNObjectsImageFilter<OutputImageType>;
LabelShapeKeepNObjectsImageFilterType::Pointer labelShapeKeepNObjectsImageFilter =
labelShapeKeepNObjectsImageFilter->SetInput(connected->GetOutput());
labelShapeKeepNObjectsImageFilter->SetBackgroundValue(0);
labelShapeKeepNObjectsImageFilter->SetNumberOfObjects(1);
labelShapeKeepNObjectsImageFilter->SetAttribute(
LabelShapeKeepNObjectsImageFilterType::LabelObjectType::NUMBER_OF_PIXELS);
auto rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(itk::NumericTraits<PixelType>::max());
rescaleFilter->SetInput(labelShapeKeepNObjectsImageFilter->GetOutput());
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(input.GetPointer(), true, itksys::SystemTools::GetFilenameName(argv[1]));
std::stringstream desc;
desc << "Largest object of " << connected->GetObjectCount() << " objects";
viewer.AddImage(rescaleFilter->GetOutput(), true, desc.str());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::LabelShapeKeepNObjectsImageFilter
keep N objects according to their shape attributes
Definition: itkLabelShapeKeepNObjectsImageFilter.h:51
itkLabelShapeKeepNObjectsImageFilter.h
itkImageFileReader.h
itkImage.h
QuickView.h
QuickView::AddImage
void AddImage(TImage *, bool FlipVertical=true, std::string Description="")
itkRescaleIntensityImageFilter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
itkConnectedComponentImageFilter.h
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:133
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
QuickView::Visualize
void Visualize(bool interact=true)
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