|
|
Line 1: |
Line 1: |
| This example creates a black image of a 3x3 square. It then uses a flood fill iterator to visit the pixels in this white region (specified by a seed point) and marks them with increasing brightnesses to convey the visit order. | | This is a duplicate of this: |
| | | http://www.itk.org/Wiki/ITK/Examples/WishList/Iterators/FloodFilledImageFunctionConditionalIterator |
| ==FloodFillIterator.cxx==
| |
| <source lang="cpp">
| |
| #include "itkImage.h"
| |
| #include "itkImageFileWriter.h"
| |
| #include "itkFloodFilledImageFunctionConditionalIterator.h"
| |
| #include "itkBinaryThresholdImageFunction.h"
| |
| #include "itkImageFileWriter.h"
| |
| | |
| #include "QuickView.h"
| |
| | |
| typedef itk::Image< unsigned char, 2 > ImageType;
| |
| | |
| static void CreateImage(ImageType::Pointer image);
| |
| | |
| int main( int argc, char *argv[])
| |
| {
| |
| ImageType::Pointer image = ImageType::New();
| |
| CreateImage(image);
| |
| | |
| typedef itk::BinaryThresholdImageFunction< ImageType, double > FunctionType;
| |
| FunctionType::Pointer function = FunctionType::New();
| |
| function->SetInputImage(image);
| |
| function->ThresholdAbove(100); // we are looking to capture 255
| |
|
| |
| typedef itk::FloodFilledImageFunctionConditionalIterator< ImageType, FunctionType > IteratorType;
| |
| | |
| itk::Index<2> seed;
| |
| seed[0] = 5;
| |
| seed[1] = 5;
| |
|
| |
| std::vector<itk::Index<2> > seeds;
| |
| seeds.push_back(seed);
| |
|
| |
| IteratorType it (image, function, seeds);
| |
| it.GoToBegin();
| |
|
| |
| unsigned int counter = 0;
| |
| while ( !it.IsAtEnd() )
| |
| {
| |
| it.Set(static_cast<float>(counter) * 25.);
| |
| ++it;
| |
| counter++;
| |
| }
| |
|
| |
| typedef itk::ImageFileWriter< ImageType > WriterType;
| |
| WriterType::Pointer writer = WriterType::New();
| |
| writer->SetFileName("output.png");
| |
| writer->SetInput(image);
| |
| writer->Update();
| |
|
| |
| | |
| ImageType::Pointer originalImage = ImageType::New();
| |
| CreateImage(originalImage);
| |
|
| |
| QuickView viewer;
| |
| viewer.AddImage(originalImage.GetPointer());
| |
| viewer.AddImage(image.GetPointer());
| |
| viewer.Visualize();
| |
| | |
| return EXIT_SUCCESS;
| |
| }
| |
| | |
| void CreateImage(ImageType::Pointer image)
| |
| {
| |
| itk::Index<2> start;
| |
| start.Fill(0);
| |
| | |
| itk::Size<2> size;
| |
| size.Fill(10);
| |
| | |
| itk::ImageRegion<2> region(start,size);
| |
| image->SetRegions(region);
| |
| image->Allocate();
| |
| image->FillBuffer(0);
| |
| | |
| // Make a square
| |
| for(unsigned int r = 3; r < 6; r++)
| |
| {
| |
| for(unsigned int c = 3; c < 6; c++)
| |
| {
| |
| itk::Index<2> pixelIndex;
| |
| pixelIndex[0] = r;
| |
| pixelIndex[1] = c;
| |
| | |
| image->SetPixel(pixelIndex, 255);
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
| | |
| {{ITKCMakeLists|FloodFillIterator}}
| |