# Index out of range error in LiThresholdCalculator

**URL:** <https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997>\
**Category:** Algorithms\
**Created:** [June 13, 2018, 8:20pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997 "2018-06-13T20:20:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [June 13, 2018, 8:20pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/1 "2018-06-13T20:20:36Z")

</div>

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](https://github.com/InsightSoftwareConsortium/ITK/blob/cac14a73b691290cdf442dfe41839af5e8075757/Modules/Filtering/Thresholding/include/itkLiThresholdCalculator.hxx#L94).

> 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`?

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 13, 2018, 8:25pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/2 "2018-06-13T20:25:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [June 14, 2018, 2:59am UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/3 "2018-06-14T02:59:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 15, 2018, 12:38pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/4 "2018-06-15T12:38:43Z")

</div>

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.

Thanks!

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [June 20, 2018, 5:17am UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/5 "2018-06-20T05:17:01Z")

</div>

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:

- Use LiThresholdImageFilter or calculator in debug mode in VisualStudio (or other compiler/settings that performs index range checks)
- Input volume: CTChest sample ([http://www.slicer.org/slicerWiki/images/3/31/CT-chest.nrrd](http://www.slicer.org/slicerWiki/images/3/31/CT-chest.nrrd))
- Number of bins = 64

Thank you!

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 20, 2018, 1:11pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/6 "2018-06-20T13:11:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 20, 2018, 6:44pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/7 "2018-06-20T18:44:57Z")

</div>

Your image has a negative mean. The [calculator tries](https://github.com/InsightSoftwareConsortium/ITK/blob/cac14a73b691290cdf442dfe41839af5e8075757/Modules/Filtering/Thresholding/include/itkLiThresholdCalculator.hxx#L117) to take the `log` of the foreground and background.

So that is not going to work…

I believe the method was published here:

> **[Minimum cross entropy thresholding](https://www.sciencedirect.com/science/article/abs/pii/003132039390115D?via=ihub)**
>
> The threshold selection problem is solved by minimizing the cross entropy between the image and its segmented version. The cross entropy is formulated…

_digging_

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 20, 2018, 8:20pm UTC](https://discourse.itk.org/t/index-out-of-range-error-in-lithresholdcalculator/997/8 "2018-06-20T20:20:49Z")

</div>

[http://review.source.kitware.com/#/c/23533/](http://review.source.kitware.com/#/c/23533/)
