# How to derive quantile value of a particular threshold value

**URL:** <https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199>\
**Category:** Beginner Questions\
**Created:** [July 20, 2022, 3:42am UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199 "2022-07-20T03:42:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 3:42am UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/1 "2022-07-20T03:42:20Z")

</div>

Hi,

I am using ITK C++, and I have applied Otsu thresholding to derive a threshold for a particular image. Now, I want to know the quantile this threshold is set. What would be the best way to do this? Also, ultimately, I want to use this quantile value to threshold other images, which I believe I should use itk::Histogram. Any suggestions appreciated.

---

<div class="post-metadata">

**Author:** ![crossmanith](https://discourse.itk.org/user_avatar/discourse.itk.org/crossmanith/32/569_2.png) [@crossmanith](https://discourse.itk.org/u/crossmanith)\
**Post date:** [July 20, 2022, 9:34am UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/2 "2022-07-20T09:34:17Z")

</div>

You could create a histogram of your image with the same number of bins as Otsu thresholding used. Then translate the Otsu-threshold to a bin index. Sum up all frequencies for bins up to the threshold bin and divide by the total frequency. That should give you the corresponding quantile.

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 3:12pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/3 "2022-07-20T15:12:19Z")

</div>

Thank you so much @crossmanith !

Is there an Example that you could recommend to create a histogram of an image? I could only find [https://itk.org/Doxygen/html/Examples\_2Statistics\_2Histogram\_8cxx-example.html](https://itk.org/Doxygen/html/Examples_2Statistics_2Histogram_8cxx-example.html) , which does not show image → histogram conversion as an example.

P.S. Looks like I have to read Book2 the relevant section. Will go through p.482+ and come back!

---

<div class="post-metadata">

**Author:** ![crossmanith](https://discourse.itk.org/user_avatar/discourse.itk.org/crossmanith/32/569_2.png) [@crossmanith](https://discourse.itk.org/u/crossmanith)\
**Post date:** [July 20, 2022, 3:39pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/4 "2022-07-20T15:39:44Z")

</div>

[ITK: itk::Statistics::ImageToHistogramFilter\< TImage \> Class Template Reference](https://itk.org/Doxygen/html/classitk_1_1Statistics_1_1ImageToHistogramFilter.html)

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 5:49pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/5 "2022-07-20T17:49:35Z")

</div>

Thank you so much! This helped me to find the relevant example associated to the filter!

[https://itk.org/Doxygen/html/Examples\_2Statistics\_2ImageHistogram3\_8cxx-example.html](https://itk.org/Doxygen/html/Examples_2Statistics_2ImageHistogram3_8cxx-example.html)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 20, 2022, 6:13pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/6 "2022-07-20T18:13:11Z")

</div>

The examples are better viewed on GitHub, as there is access to history and the rest of the source code. This particular example is here: [https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/Statistics/ImageHistogram3.cxx](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/Statistics/ImageHistogram3.cxx).

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 6:14pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/7 "2022-07-20T18:14:49Z")

</div>

Thank you very much! A lot of learning, this is definitely helpful!!!

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 6:49pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/8 "2022-07-20T18:49:51Z")

</div>

Now that I can compute the percentile and also apply thresholds for each individual z-slices, I am having trouble of assembling that back to a 3D image.

I was basing my program with the following example:  
[https://examples.itk.org/src/filtering/imagegrid/stack2dimagesinto3dimage/documentation](https://examples.itk.org/src/filtering/imagegrid/stack2dimagesinto3dimage/documentation)

But that only extracts one slice and paste(?)s. Is there a good example for applying a filter to each slice of a 3D image?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 20, 2022, 6:56pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/9 "2022-07-20T18:56:14Z")

</div>

[SliceBySliceImageFilter](https://itk.org/Doxygen/html/classitk_1_1SliceBySliceImageFilter.html)

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 20, 2022, 7:21pm UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/10 "2022-07-20T19:21:29Z")

</div>

Thank you so much!

Just to make sure I understand this correctly - I have to apply different thresholds for each slices (it maintains the percentile, but not the actual value) - I can calculate these values by extracting the slice, but would it be possible to apply this to the slicebysliceImagefilter?

---

<div class="post-metadata">

**Author:** ![chlee](https://discourse.itk.org/user_avatar/discourse.itk.org/chlee/32/3788_2.png) [@chlee](https://discourse.itk.org/u/chlee)\
**Post date:** [July 23, 2022, 1:51am UTC](https://discourse.itk.org/t/how-to-derive-quantile-value-of-a-particular-threshold-value/5199/11 "2022-07-23T01:51:00Z")

</div>

Thank you everyone, just to follow up, I was able to create a custom filter which could be feed in to the slicebysliceImageFilter that @zivy pointed to! (Learned how to make a custom filter!).
