[Insight-users] Seg fault with PeriodicBoundaryCondition

Emmanuel Christophe melaneum at gmail.com
Wed May 9 11:09:50 EDT 2007


Hi,

I'm trying to use the itk::PeriodicBoundaryCondition with a
NeighborhoodIterator. When I use this Boundary condition, my code (see
below) fails with a "segmentation fault".

However, the same code works perfectly with
itk::ConstantBoundaryCondition or
itk::ZeroFluxNeumannBoundaryCondition.

Do I use the boundaryCondition properly ?

Thanks,
Emmanuel Christophe


--------------------------------------------

//Here is the code

#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkNeighborhoodIterator.h"
#include "itkPeriodicBoundaryCondition.h"
#include "itkConstantBoundaryCondition.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"


int main(int argc, char ** argv)
{
  typedef itk::Image< unsigned char, 2 > ImageType;

  //Create image
  ImageType::Pointer image = ImageType::New();

  ImageType::IndexType start;
  start[0] = 0; // first index on X
  start[1] = 0; // first index on Y

  ImageType::SizeType size;
  size[0] = 100; // size along X
  size[1] = 100; // size along Y

  ImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );

  image->SetRegions( region );
  image->Allocate();


  //Generate a random image
  typedef itk::ImageRegionIterator< ImageType > IteratorType;
  IteratorType it(image, image->GetLargestPossibleRegion());
  srand((unsigned)time(0));
  it.GoToBegin();
  while(!it.IsAtEnd())
  {
    it.Set(rand() % 100);
    ++it;
  }


  //Set boundary condition for the NeighborhoodIterator:
  typedef itk::PeriodicBoundaryCondition< ImageType > BoundaryConditionType;
//   typedef itk::ConstantBoundaryCondition< ImageType > BoundaryConditionType;
//   typedef itk::ZeroFluxNeumannBoundaryCondition< ImageType >
BoundaryConditionType;

  typedef itk::NeighborhoodIterator< ImageType, BoundaryConditionType
> NeighborhoodIteratorType;
  NeighborhoodIteratorType::RadiusType radius;
  radius.Fill(1);
  NeighborhoodIteratorType neighborhoodIt(radius, image,
                                          image-> GetLargestPossibleRegion() );

  NeighborhoodIteratorType::OffsetType offset={{1,0}};

    for(neighborhoodIt.GoToBegin(); !neighborhoodIt.IsAtEnd(); ++neighborhoodIt)
    {
      if (neighborhoodIt.GetCenterPixel() == neighborhoodIt.GetPixel( offset))
      {
        std::cout <<  ".";
      }
    }

    std::cout <<  std::endl;


  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  const char * filenamewriter = argv[1];
  writer->SetFileName( filenamewriter );

  typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleType;
  RescaleType::Pointer rescaleFilter = RescaleType::New();
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);

  rescaleFilter->SetInput( image );
  writer->SetFileName( filenamewriter );
  writer->SetInput( rescaleFilter->GetOutput() );
  writer->Update();

}


More information about the Insight-users mailing list