Index out of range error in LiThresholdCalculator

I’ve added automatic thresholding to 3D Slicer’s segment editor and while testing it, I’ve got index out of range errors due to writing past the last element in a vector in LiThresholdCalculator<THistogram, TOutput>::GenerateData(void) in this line 94.

Exception thrown at 0x00007FFBF90CF218 in SlicerApp-real.exe: Microsoft C++ exception: itk::InvalidArgumentError at memory location 0x0000008369534A38.

For example, size of histogram was 64 (value of size variable was 64) and when I got the error when ih variable value was 64.

I don’t have time to further analyze this, but solution could be to add check to make sure that ih is lower that size (it is done later in the code). Or, maybe ih <= histthresh should be changed to ih < histthresh?

1 Like

Thanks for reporting the issue. Anything odd about the input data? Perhaps a constant image or something generated?

The error does not happen with all data sets, but it happens with completely normal images (for example, CTChest 3D Slicer sample data set).


One more question, are you using the Calculator directly? And computing the histogram bounds yourself?

If you could provide a self contained example or reference the code that produces the problem, that would be great!. I’d like to closely look at this case to determine if the problem is just the access or if there a erroneous computation before hand, or if this is some type of degenerate case that need a special case.


I’ve noticed the error when used the calculator directly (in Segment editor in Slicer), but you can reproduce it when using through LiThresholdImageFilter (Simple Filters module) in debug mode.

How to reproduce:

Thank you!

Great! I’ll look into this issue this week and dig into the formulation to see if there a a numeric error or some type of off by one error in the implementation.

1 Like

Your image has a negative mean. The calculator tries to take the log of the foreground and background.

So that is not going to work…

I believe the method was published here:


1 Like