ITK  5.2.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
*
* 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"
#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;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
using OutputImageType = itk::Image<unsigned short, Dimension>;
using ConnectedComponentImageFilterType = itk::ConnectedComponentImageFilter<ImageType, OutputImageType>;
ConnectedComponentImageFilterType::Pointer connected = ConnectedComponentImageFilterType::New();
connected->SetInput(reader->GetOutput());
connected->Update();
std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;
using LabelShapeKeepNObjectsImageFilterType = itk::LabelShapeKeepNObjectsImageFilter<OutputImageType>;
LabelShapeKeepNObjectsImageFilterType::Pointer labelShapeKeepNObjectsImageFilter =
LabelShapeKeepNObjectsImageFilterType::New();
labelShapeKeepNObjectsImageFilter->SetInput(connected->GetOutput());
labelShapeKeepNObjectsImageFilter->SetBackgroundValue(0);
labelShapeKeepNObjectsImageFilter->SetNumberOfObjects(1);
labelShapeKeepNObjectsImageFilter->SetAttribute(
LabelShapeKeepNObjectsImageFilterType::LabelObjectType::NUMBER_OF_PIXELS);
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(itk::NumericTraits<PixelType>::max());
rescaleFilter->SetInput(labelShapeKeepNObjectsImageFilter->GetOutput());
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddImage(reader->GetOutput(), 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;
}
itk::LabelShapeKeepNObjectsImageFilter
keep N objects according to their shape attributes
Definition: itkLabelShapeKeepNObjectsImageFilter.h:52
itkLabelShapeKeepNObjectsImageFilter.h
itkImageFileReader.h
itkImage.h
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
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:58
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:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
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