|
|
Line 1: |
Line 1: |
| <div class="floatcenter">[[File:ITK_Examples_Baseline_ImageSegmentation_TestExtractLargestConnectedComponentFromBinaryImage.png]]</div>
| | {{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}} |
| ==ExtractLargestConnectedComponentFromBinaryImage.cxx==
| |
| <source lang="cpp">
| |
| #include "itkImage.h"
| |
| #include "itkImageFileReader.h"
| |
| #include "itkConnectedComponentImageFilter.h"
| |
| #include "itkLabelShapeKeepNObjectsImageFilter.h"
| |
| #include "itkRescaleIntensityImageFilter.h"
| |
| | |
| #include "itksys/SystemTools.hxx"
| |
| #include <sstream>
| |
| | |
| #include "QuickView.h"
| |
| | |
| int main( int argc, char *argv[])
| |
| { | |
| if( argc < 2 )
| |
| {
| |
| std::cout << "Usage:" << std::endl;
| |
| std::cout << argv[0] << " InputFileName" << std::endl;
| |
| }
| |
| const unsigned int Dimension = 2;
| |
| typedef unsigned char PixelType;
| |
| typedef itk::Image< PixelType, Dimension > ImageType;
| |
| | |
| typedef itk::ImageFileReader< ImageType > ReaderType;
| |
| ReaderType::Pointer reader = ReaderType::New();
| |
| reader->SetFileName( argv[1] );
| |
| | |
| typedef itk::Image< unsigned short, Dimension > OutputImageType;
| |
| | |
| typedef itk::ConnectedComponentImageFilter <ImageType, OutputImageType >
| |
| ConnectedComponentImageFilterType;
| |
| | |
| ConnectedComponentImageFilterType::Pointer connected = ConnectedComponentImageFilterType::New ();
| |
| connected->SetInput(reader->GetOutput());
| |
| connected->Update();
| |
| | |
| std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;
| |
| | |
| typedef itk::LabelShapeKeepNObjectsImageFilter< OutputImageType > LabelShapeKeepNObjectsImageFilterType;
| |
| LabelShapeKeepNObjectsImageFilterType::Pointer labelShapeKeepNObjectsImageFilter = LabelShapeKeepNObjectsImageFilterType::New();
| |
| labelShapeKeepNObjectsImageFilter->SetInput( connected->GetOutput() );
| |
| labelShapeKeepNObjectsImageFilter->SetBackgroundValue( 0 );
| |
| labelShapeKeepNObjectsImageFilter->SetNumberOfObjects( 1 );
| |
| labelShapeKeepNObjectsImageFilter->SetAttribute( LabelShapeKeepNObjectsImageFilterType::LabelObjectType::NUMBER_OF_PIXELS);
| |
| | |
| typedef itk::RescaleIntensityImageFilter< OutputImageType, ImageType > RescaleFilterType;
| |
| RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
| |
| rescaleFilter->SetOutputMinimum(0);
| |
| rescaleFilter->SetOutputMaximum(itk::NumericTraits<PixelType>::max());
| |
| rescaleFilter->SetInput(labelShapeKeepNObjectsImageFilter->GetOutput());
| |
| | |
| 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(); | |
| | |
| return EXIT_SUCCESS;
| |
| }
| |
| </source>
| |
| | |
| {{ITKVTKCMakeLists|{{SUBPAGENAME}}}}
| |