[Insight-developers] Minor Bug in IsoContourDistanceImageFilter

Zachary Pincus zpincus at stanford.edu
Fri Feb 4 05:27:56 EST 2005


Hello,

There's a minor bug in itkIsoContourDistanceImageFilter.txx that causes  
it to fail when the input image type is unsigned. (A potentially common  
case, as this filter can be used as a step in turning a binary mask  
into a signed distance map.)

Basically, the filter attempts to perform all its calculations in terms  
of the output pixel type, which is appropriate. However, a few lines  
squeaked through where the right hand side of the equation was  
performed with only terms of the input pixel type. These lines  
(subtractions) don't work right when the input pixel type is unsigned  
and the result should be negative.(*)

The solution is to promote at least one term on the right hand side of  
all the calculations to the output pixel type, which only makes sense  
as a float or double for this filter. Then the calculation is performed  
correctly.

Here's the patch. Sorry this is so close to the 1.10 deadline (or  
after! oh well if so).

diff itkIsoContourDistanceImageFilter.txx  
itkIsoContourDistanceImageFilter_orig.txx
266c266
<      val0 =  
inNeigIt.GetPixel(center)-static_cast<PixelType>(m_LevelSetValue);
---
 >      val0 = inNeigIt.GetPixel(center)-m_LevelSetValue;
272c272
<          
grad0[ng]=inNeigIt.GetNext(ng,1)- 
static_cast<PixelType>(inNeigIt.GetPrevious(ng,1));
---
 >         grad0[ng]=inNeigIt.GetNext(ng,1)-inNeigIt.GetPrevious(ng,1);
278c278
<        val1 =  
inNeigIt.GetPixel(center+stride[n])- 
static_cast<PixelType>(m_LevelSetValue);
---
 >        val1 = inNeigIt.GetPixel(center+stride[n])-m_LevelSetValue;
286c286
<                       
static_cast<PixelType>(inNeigIt.GetPixel(center+stride[n]-stride[ng]));
---
 >                      inNeigIt.GetPixel(center+stride[n]-stride[ng]);


Zach Pincus
Department of Biochemistry and Program in Biomedical Informatics
Stanford University School of Medicine

(*) Well, actually, on my little-endian platform with gcc, "float a =  
(unsigned char) 5 - (unsigned char) 10" gives a = -5 , but "float a =  
(unsigned int) 5 - (unsigned int) 10" blows up. I suspect a strange  
interaction with unsigned underflow and how the cast operator was  
implemented. At any rate, this means that this filter, through  
basically luck, works with input image type unsigned char, but not with  
unsigned int.



More information about the Insight-developers mailing list