[Insight-users] Problems with image corruption : Shouldn't use LargestPossibleRegion for Iterators.

Luis Ibanez luis . ibanez at kitware . com
Wed, 17 Dec 2003 14:19:01 -0500


Hi Radhika,

It seems that the error is related to the use of
"GetLargestPossibleRegion()" in order to define
the iterators in line 230 of your code:


>     ImageType2D::RegionType region = binimg->GetLargestPossibleRegion();
>     ConstIteratorType maskit (mask, region);
>     ConstIteratorType binimgit( binimg, region );
>     IteratorType newmaskit(newmask,region);
> 
>    maskit.GoToBegin();
>    binimgit.GoToBegin();
>    newmaskit.GoToBegin();
>    while( ! maskit.IsAtEnd() && ! binimgit.IsAtEnd() && ! newmaskit.IsAtEnd())
>      {
> 	 newmaskit.Set(binimgit.Get());
>        if( maskit.Get() == 0 )   // if mask pixel is off
>          {
> 		newmaskit.Set(0);
>          }
>        ++maskit;
> 	 ++binimgit;
> 	 ++newmaskit;
>      }

In general you shouldn't use "LargestPossibleRegion"
since there is no guarranty that all this data is
going to be in memory.  The safe region to use is the
"Buffered" region.

Just to make sure that this is the problem, please
insert the line:

            binimg->Print( std::cout )

in line 229, run the program and look at the printed
values of the image regions. There will be: Requested,
Buffered and LargestPossible region.

My guess is that the largest possible region is
larger than the buffered region and your iterators
are stepping out of the image buffers, therefore
corrupting the memory.

BTW the process that you are attempting with the
iterators is done by the MaskImageFilter
http://www . itk . org/Insight/Doxygen/html/classitk_1_1MaskImageFilter . html

You may simply replace this while loop with a
MaskImageFilter.



Please let us know what you find.


Thanks


   Luis



------------------------------------
Radhika Sivaramakrishna wrote:
> Hi Luis,
> I am having some problems with my program. I have enclosed a portion of 
> my code here where the problem is. I am not able to figure it out. 
> Basically, "binimg" in my code is the output of the relabel filter. At 
> that point when I write it out, it is correct. However, further down the 
> line, when I try to write out the output of the relabel filter, it is 
> corrupted. In fact, all are corrupted and they all have the same values 
> which is that of the result of the mask filter. Even "gs" the output of 
> the extractimagefilter is wrong at the point where I am writing it out. 
> Could you help me figure out the problem? Maybe there is something wrong 
> in the way I am setting the region of iteration for each or is there a 
> problem in reusing the extractimagefilter for first extracting a 
> greyscale slice and then using the same thing to extract a mask?
>  
> Thanks
> Radhika
>