[ITK] 3D iterator goes out of bounds but still works

Nirvedh Meshram nmeshram at wisc.edu
Fri Apr 21 21:49:39 EDT 2017


Hello All,


I am working on a Code which is using an iterator which access pixels beyond the region boundary. However, this is not causing an error as I would expect it to. I need to use version 4.0  for legacy reasons. I am attaching a verifiable code and its output with this issue. Let me know if this is a bug in my ITK version or I am missing something and this is correct behavior.


#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkVersion.h"
int main( int argc, char * argv[] )
{
        typedef    float    InputPixelType;
        typedef itk::Image< InputPixelType,  3 >   InputImageType;

        InputImageType::Pointer ip=InputImageType::New();
        InputPixelType *input;
        InputImageType::RegionType region,region1;
        InputImageType::SizeType size,size1;
        InputImageType::IndexType index,index1;
        size[0]=5;
        size[1]=5;
        size[2]=5;
        index[0]=0;
        index[1]=0;
        index[2]=0;

        region.SetSize(size);
        region.SetIndex(index);
        std::cout<<region;
        ip->SetRegions(region);
        ip->Allocate();
        size[2]=1;
        region.SetSize(size);
        std::cout<<region;
        typedef itk::ImageRegionIterator<InputImageType> Iteratortype;
        Iteratortype It(ip,region);
        int count=0;
        for(It.GoToBegin();count<25*5;count++,++It)
        {
                It.Set(27);
        }
        input=ip->GetBufferPointer();
        for(int k=0;k<5;k++)
        {
                for(int i=0;i<5;i++)
                {
                        for(int j=0;j<5;j++)
                        {
                                std::cout<<input[k*25+j+5*i]<<" ";
                        }
                        std::cout<<"\n";

                }
                std::cout<<"\n";
        }
        itk::Version *Ver=itk::Version::New();
        std::cout<<Ver->GetITKMajorVersion()<<"."<<Ver->GetITKMinorVersion()<<"\n";



        return EXIT_SUCCESS;
}

Output:


ImageRegion (0x7ffcfc489560)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [5, 5, 5]
ImageRegion (0x7ffcfc489560)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [5, 5, 1]
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27

27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27

27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27

27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27

27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27
27 27 27 27 27

4.0



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20170422/c3aa83b6/attachment.html>


More information about the Community mailing list