ITK/Examples/Iterators/ShapedNeighborhoodIteratorManual: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "How do you specify the neighborhood? ==ShapedNeighborhoodIterator.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkImageFileReader.h" #include "itkShapedNeighborhood...")
 
No edit summary
Line 1: Line 1:
How do you specify the neighborhood?
Crashes when initializing the iterator loop?


==ShapedNeighborhoodIterator.cxx==
==ShapedNeighborhoodIterator.cxx==
<source lang="cpp">
<source lang="cpp">
#include "itkImage.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkShapedNeighborhoodIterator.h"
#include "itkShapedNeighborhoodIterator.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkImageRegionIterator.h"


#include <itkImageToVTKImageFilter.h>
#include <itkImageToVTKImageFilter.h>
Line 17: Line 16:
#include "vtkRenderer.h"
#include "vtkRenderer.h"


int main(int argc, char*argv[])
typedef itk::Image<unsigned char, 2>  ImageType;
{
  if(argc < 2)
    {
    std::cerr << "Required: filename" << std::endl;
    return EXIT_FAILURE;
    }


  typedef itk::Image<unsigned char, 2>  ImageType;
void CreateImage(ImageType::Pointer image);


  typedef itk::ImageFileReader<ImageType> ReaderType;
int main(int, char*[])
  ReaderType::Pointer reader = ReaderType::New();
{
  reader->SetFileName(argv[1]);
   ImageType::Pointer image = ImageType::New();
  reader->Update();
   ImageType::SizeType radius;
 
   radius[0] = 1;
   ImageType::Pointer image = reader->GetOutput();
   radius[1] = 1;
 
   ImageType::SizeType regionSize;
   regionSize[0] = 50;
   regionSize[1] = 50;


   ImageType::IndexType regionIndex;
   typedef itk::ShapedNeighborhoodIterator<ImageType> IteratorType;
  regionIndex[0] = 0;
   IteratorType iterator(radius, image, image->GetLargestPossibleRegion());
   regionIndex[1] = 0;


  /*
  // a smaller region
   ImageType::RegionType region;
   ImageType::RegionType region;
   region.SetSize(regionSize);
   ImageType::IndexType start;
   region.SetIndex(regionIndex);
   start[0] = 5;
  start[1] = 5;


   ImageType::SizeType radius;
   ImageType::SizeType size;
   radius[0] = 1;
   size[0] = 3;
   radius[1] = 2;
   size[1] = 4;


   itk::BinaryBallStructuringElement<unsigned char, 2> ball;
   region.SetSize(size);
  region.SetIndex(start);
  IteratorType iterator(radius, image, region);
  */
    
    
   itk::ShapedNeighborhoodIterator<ImageType> iterator(radius, image,region);
   IteratorType::OffsetType top = {{0,-1}};
   iterator.SetNeighborhood(ball);
  IteratorType::OffsetType bottom = {{0,1}};
  IteratorType::OffsetType left = {{-1,0}};
  IteratorType::OffsetType right = {{1,0}};
  iterator.ActivateOffset(top);
  iterator.ActivateOffset(bottom);
  iterator.ActivateOffset(left);
   iterator.ActivateOffset(right);


   while(!iterator.IsAtEnd())
   for(iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator) // Crashes here!
  {
    {
 
    std::cout << "top: " << iterator[top] << std::endl;
     ++iterator;
    std::cout << "bottom: " << iterator[bottom] << std::endl;
  }
    std::cout << "left: " << iterator[left] << std::endl;
     std::cout << "right: " << iterator[right] << std::endl;
    }


   // Visualize
   // Visualize
Line 96: Line 98:
}
}


void CreateImage(ImageType::Pointer image)
{
  // Create an image with 2 connected components
  ImageType::RegionType region;
  ImageType::IndexType start;
  start[0] = 0;
  start[1] = 0;
  ImageType::SizeType size;
  unsigned int NumRows = 20;
  unsigned int NumCols = 30;
  size[0] = NumRows;
  size[1] = NumCols;
  region.SetSize(size);
  region.SetIndex(start);
  image->SetRegions(region);
  image->Allocate();
  itk::ImageRegionIterator<ImageType> imageIterator(image,region);
  while(!imageIterator.IsAtEnd())
    {
    imageIterator.Set(0);
    ++imageIterator;
    }
  /*
  // Make a square
  for(unsigned int r = 40; r < 100; r++)
    {
    for(unsigned int c = 40; c < 100; c++)
      {
      ImageType::IndexType pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;
      image->SetPixel(pixelIndex, 15);
      }
    }
  */
}
</source>
</source>



Revision as of 14:44, 13 November 2010

Crashes when initializing the iterator loop?

ShapedNeighborhoodIterator.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkShapedNeighborhoodIterator.h"
  3. include "itkImageRegionIterator.h"
  1. include <itkImageToVTKImageFilter.h>
  1. include "vtkImageViewer.h"
  2. include "vtkRenderWindowInteractor.h"
  3. include "vtkSmartPointer.h"
  4. include "vtkImageActor.h"
  5. include "vtkInteractorStyleImage.h"
  6. include "vtkRenderer.h"

typedef itk::Image<unsigned char, 2> ImageType;

void CreateImage(ImageType::Pointer image);

int main(int, char*[]) {

 ImageType::Pointer image = ImageType::New();
 ImageType::SizeType radius;
 radius[0] = 1;
 radius[1] = 1;
 typedef itk::ShapedNeighborhoodIterator<ImageType> IteratorType;
 IteratorType iterator(radius, image, image->GetLargestPossibleRegion());
 /*
 // a smaller region
 ImageType::RegionType region;
 ImageType::IndexType start;
 start[0] = 5;
 start[1] = 5;
 ImageType::SizeType size;
 size[0] = 3;
 size[1] = 4;
 region.SetSize(size);
 region.SetIndex(start);
 IteratorType iterator(radius, image, region);
 */
 
 IteratorType::OffsetType top = Template:0,-1;
 IteratorType::OffsetType bottom = Template:0,1;
 IteratorType::OffsetType left = Template:-1,0;
 IteratorType::OffsetType right = Template:1,0;
 iterator.ActivateOffset(top);
 iterator.ActivateOffset(bottom);
 iterator.ActivateOffset(left);
 iterator.ActivateOffset(right);
 for(iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator) // Crashes here!
   {
   std::cout << "top: " << iterator[top] << std::endl;
   std::cout << "bottom: " << iterator[bottom] << std::endl;
   std::cout << "left: " << iterator[left] << std::endl;
   std::cout << "right: " << iterator[right] << std::endl;
   }
 // Visualize
 typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
 ConnectorType::Pointer connector = ConnectorType::New();
 connector->SetInput(image);
 vtkSmartPointer<vtkImageActor> actor =
   vtkSmartPointer<vtkImageActor>::New();
 actor->SetInput(connector->GetOutput());
 vtkSmartPointer<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow>::New();
 vtkSmartPointer<vtkRenderWindowInteractor> interactor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
 interactor->SetRenderWindow(renderWindow);
 vtkSmartPointer<vtkRenderer> renderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(renderer);
 renderer->AddActor(actor);
 renderer->ResetCamera();
 renderWindow->Render();
 vtkSmartPointer<vtkInteractorStyleImage> style =
   vtkSmartPointer<vtkInteractorStyleImage>::New();
 interactor->SetInteractorStyle(style);
 interactor->Start();
 return EXIT_SUCCESS;

}

void CreateImage(ImageType::Pointer image) {

 // Create an image with 2 connected components
 ImageType::RegionType region;
 ImageType::IndexType start;
 start[0] = 0;
 start[1] = 0;
 ImageType::SizeType size;
 unsigned int NumRows = 20;
 unsigned int NumCols = 30;
 size[0] = NumRows;
 size[1] = NumCols;
 region.SetSize(size);
 region.SetIndex(start);
 image->SetRegions(region);
 image->Allocate();
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 while(!imageIterator.IsAtEnd())
   {
   imageIterator.Set(0);
   ++imageIterator;
   }
 /*
 // Make a square
 for(unsigned int r = 40; r < 100; r++)
   {
   for(unsigned int c = 40; c < 100; c++)
     {
     ImageType::IndexType pixelIndex;
     pixelIndex[0] = r;
     pixelIndex[1] = c;
     image->SetPixel(pixelIndex, 15);
     }
   }
 */

} </source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

PROJECT(ShapedNeighborhoodIterator)

include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)

FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(ShapedNeighborhoodIterator ShapedNeighborhoodIterator.cxx) TARGET_LINK_LIBRARIES(ShapedNeighborhoodIterator vtkHybrid ITKNumerics ITKBasicFilters ITKCommon ITKIO)

</source>