//This function removes all objects that are <= minObjSize from the foreground. //The foreground remains grayscale after this filter void Preprocess::RemoveConnectedComponents(int minObjSize) { typedef itk::Image< unsigned short, 3 > ShortImageType; typedef itk::ConnectedComponentImageFilter< ImageType3D, ShortImageType > CCFilterType; typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType; CCFilterType::Pointer ccfilter = CCFilterType::New(); RelabelType::Pointer relabel = RelabelType::New(); ccfilter->SetInput( myImg ); ccfilter->FullyConnectedOn(); relabel->SetInput( ccfilter->GetOutput() ); relabel->SetMinimumObjectSize( minObjSize ); relabel->InPlaceOn(); try { relabel->Update(); } catch( itk::ExceptionObject & excep ) { std::cerr << "Relabel: exception caught !" << std::endl; std::cerr << excep << std::endl; return; } unsigned short initNumObjects = ccfilter->GetObjectCount(); unsigned short finalNumObjects = relabel->GetNumberOfObjects(); unsigned short removedNumObjects = initNumObjects - finalNumObjects; std::cout << "Removed " << removedNumObjects << " of " << initNumObjects << " objects..."; ShortImageType::Pointer ccImage = relabel->GetOutput(); //Use connected component image as a mask: itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() ); itk::ImageRegionIterator< ImageType3D > itr2( myImg, myImg->GetLargestPossibleRegion() ); for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2) { if(itr1.Get() == 0) { itr2.Set( 0 ); } } }
//This function removes all objects that are <= minObjSize from the foreground. //The foreground remains grayscale after this filter bool VolumeProcess::MaskSmallConnComp(int minObjSize) { typedef itk::Image< unsigned short, Dimension > ShortImageType; typedef itk::ConnectedComponentImageFilter< ImageType, ShortImageType > CCFilterType; typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType; CCFilterType::Pointer ccfilter = CCFilterType::New(); RelabelType::Pointer relabel = RelabelType::New(); ccfilter->SetInput (m_outputImage); ccfilter->FullyConnectedOn(); relabel->SetInput( ccfilter->GetOutput() ); relabel->SetMinimumObjectSize( minObjSize ); relabel->InPlaceOn(); try { relabel->Update(); } catch( itk::ExceptionObject & excep ) { std::cerr << "Relabel: exception caught !" << std::endl; std::cerr << excep << std::endl; return false; } unsigned short numObjects = relabel->GetNumberOfObjects(); if(debug) std::cerr << "Connected components = " << numObjects << std::endl; ShortImageType::Pointer ccImage = relabel->GetOutput(); //Use connected component image as a mask: itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() ); itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion() ); for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2) { if(itr1.Get() == 0) { itr2.Set( 0 ); } } return true; }