ITK/Examples/Iterators/ShapedNeighborhoodIteratorManual: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 6: Line 6:
#include "itkShapedNeighborhoodIterator.h"
#include "itkShapedNeighborhoodIterator.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIterator.h"
#include <itkImageToVTKImageFilter.h>
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"


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


   typedef itk::ShapedNeighborhoodIterator<ImageType> IteratorType;
   typedef itk::ShapedNeighborhoodIterator<ImageType> IteratorType;
   IteratorType iterator(radius, image, image->GetLargestPossibleRegion());
    
 
  /*
   // a smaller region
   // a smaller region
   ImageType::RegionType region;
   ImageType::RegionType region;
Line 44: Line 33:
   region.SetIndex(start);
   region.SetIndex(start);
   IteratorType iterator(radius, image, region);
   IteratorType iterator(radius, image, region);
  */


  //This is a weird way to activate offsets//
  /*
   IteratorType::OffsetType top = {{0,-1}};
   IteratorType::OffsetType top = {{0,-1}};
   IteratorType::OffsetType bottom = {{0,1}};
   IteratorType::OffsetType bottom = {{0,1}};
   IteratorType::OffsetType left = {{-1,0}};
   IteratorType::OffsetType left = {{-1,0}};
   IteratorType::OffsetType right = {{1,0}};
   IteratorType::OffsetType right = {{1,0}};
  IteratorType::OffsetType center = {{0,0}};
   iterator.ActivateOffset(top);
   iterator.ActivateOffset(top);
   iterator.ActivateOffset(bottom);
   iterator.ActivateOffset(bottom);
   iterator.ActivateOffset(left);
   iterator.ActivateOffset(left);
   iterator.ActivateOffset(right);
   iterator.ActivateOffset(right);
   */
   iterator.ActivateOffset(center);


   //This way seems more appropriate//
   iterator.GoToBegin();
  for (float y = -radius; y <= radius; y++)
  iterator.IsAtEnd();
      {
      for (float x = -radius; x <= radius; x++)
        {
        IteratorType::OffsetType off;
       
        float dis = vcl_sqrt( x*x + y*y );
        if (dis <= radius)
          {
          off[0] = static_cast<int>(x);
          off[1] = static_cast<int>(y);
          iterator.ActivateOffset(off);
          }
        }
      }


   for(iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator) // Crashes here!
   for(iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator) // Crashes here!
  //The for loop syntax is correct, the crash is caused by referencing the out of bound pixels//
  //which should have been handled with boundary checking, the best way to handle boundary pixels in ITK is to use FaceList//
     {
     {
    //The offset "top" is out of bound in the first iteration and when you iterate over the first row of the image//
     std::cout << "top: " << iterator[top] << std::endl;
     std::cout << "top: " << iterator[top] << std::endl;
    //The offset "bottom" is out of bound when you iterate over the last row of the image//
     std::cout << "bottom: " << iterator[bottom] << std::endl;
     std::cout << "bottom: " << iterator[bottom] << std::endl;
    //The offset "left" is out of bound in the first iteration and when you iterate over the first column of the image//
     std::cout << "left: " << iterator[left] << std::endl;
     std::cout << "left: " << iterator[left] << std::endl;
    //The offset "bottom" is out of bound when you iterate over the last column of the image//
     std::cout << "right: " << iterator[right] << 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;
   return EXIT_SUCCESS;
Line 126: Line 61:
void CreateImage(ImageType::Pointer image)
void CreateImage(ImageType::Pointer image)
{
{
   // Create an image
   // Create an image with 2 connected components
   ImageType::RegionType region;
   ImageType::RegionType region;
   ImageType::IndexType start;
   ImageType::IndexType start;
Line 133: Line 68:


   ImageType::SizeType size;
   ImageType::SizeType size;
   size[0] = 20;
   size[0] = 100;
   size[1] = 30;
   size[1] = 200;


   region.SetSize(size);
   region.SetSize(size);
Line 150: Line 85:
     ++imageIterator;
     ++imageIterator;
     }
     }
 
 
}
}
</source>
</source>

Revision as of 13:08, 14 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"

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;
 
 // 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;
 IteratorType::OffsetType center = Template:0,0;
 iterator.ActivateOffset(top);
 iterator.ActivateOffset(bottom);
 iterator.ActivateOffset(left);
 iterator.ActivateOffset(right);
 iterator.ActivateOffset(center);
 iterator.GoToBegin();
 iterator.IsAtEnd();
 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;
   }
 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;
 size[0] = 100;
 size[1] = 200;
 region.SetSize(size);
 region.SetIndex(start);
 image->SetRegions(region);
 image->Allocate();
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 while(!imageIterator.IsAtEnd())
   {
   imageIterator.Set(0);
   ++imageIterator;
   }
 

} </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>