#include "itkImageFileReader.h" #include "itkBinaryThresholdImageFunction.h" #include "itkImageLinearIteratorWithIndex.h" #include "itkFloodFilledImageFunctionConditionalConstIterator.h" #include "itkShapedFloodFilledImageFunctionConditionalConstIterator.h" int main(int argc, char **argv) { if (argc != 2) return EXIT_FAILURE; try { const unsigned int ImageDimension = 2; typedef unsigned char PixelType; typedef itk::Image ImageType; typedef ImageType::RegionType RegionType; typedef ImageType::IndexType IndexType; typedef itk::BinaryThresholdImageFunction FunctionType; typedef itk::ShapedFloodFilledImageFunctionConditionalConstIterator ShapedFloodFilledIteratorType; typedef itk::ImageFileReader ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName(argv[1]); reader->Update(); const IndexType index = {29, 47}; std::vector seedList; seedList.push_back(index); RegionType region = reader->GetOutput()->GetBufferedRegion(); FunctionType::Pointer function = FunctionType::New(); function->SetInputImage ( reader->GetOutput() ); function->ThresholdAbove ( 1 ); // >= 1 ShapedFloodFilledIteratorType shapedFloodIt(reader->GetOutput(), function, seedList); shapedFloodIt.SetFullyConnected(true); // 8-connected, default ImageType::Pointer visitedImage = ImageType::New(); visitedImage->SetRegions(region); visitedImage->Allocate(); visitedImage->FillBuffer(0); for (; !shapedFloodIt.IsAtEnd() ; ++shapedFloodIt) { const IndexType& index = shapedFloodIt.GetIndex(); visitedImage->SetPixel(index, 255); } typedef itk::ImageRegionConstIterator ConstIteratorType; ConstIteratorType inIt(reader->GetOutput(), region); ConstIteratorType outIt(visitedImage, region); for (; !inIt.IsAtEnd() ; ++inIt, ++outIt) { if (inIt.Get() != outIt.Get()) return EXIT_FAILURE; } } catch (itk::ExceptionObject& e) { e.Print(std::cerr); return EXIT_FAILURE; } catch (...) { return EXIT_FAILURE; } return EXIT_SUCCESS; }