<a id="easyimage-thresholding"></a>

# Thresholding

![](assets/images/easyimage/switch1.webp)![](assets/images/easyimage/ebw8image11.webp)![](assets/images/easyimage/ebw8imageadaptivethresh.webp)![](assets/images/easyimage/ebw8image11isodata.webp)
* The thresholding transforms an image by classifying the pixel values using these methods (detailed below):
  *  ***Automatic threshold***
  *  ***AutoThreshold***
  *  ***Manual threshold*** using one or two threshold values
  *  ***Histogram based*** (computed before using the thresholding function)
* These functions also return the average gray levels of each pixel below and above the threshold.

<a id="easyimage-thresholding-keys"></a>

## Keys to Successful Thresholding

* Object and background areas should be of uniform color and illumination. Image uniformization may be required prior to thresholding. See the page related to [noise reduction](noise_reduction_and_estimation.html.md).
* The gray level range of the object and background must be sufficiently different (all background pixels should be darker than the darkest object pixel). You must decide if the threshold value should be:
  * Constant: **absolute** threshold
  * Adapted to ambient light intensity: **relative** or **automatic** threshold

<a id="easyimage-thresholding-automatic"></a>

## Automatic Thresholding

The threshold is calculated automatically if you use one of these threshold modes with the [`EasyImage::Threshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#ac6f69c1aac9510ac6cd639acb27979ee) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#ae13e71e4c82873cb82b2b72bb9fb1b7e) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#a0b095d913c4676c6c3be8f73e4c06dea) function.

* [`EThresholdMode_MinResidue`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html): Minimizes the quadratic difference between the source and the resulting image (default behavior if the `Threshold` function is invoked without an argument).
  ![](assets/images/easyimage/ebw8image11minresid.webp)
* [`EThresholdMode_MaxEntropy`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html): Maximizes the entropy (that is, the amount of information) between object and background of the resulting image.
  ![](assets/images/easyimage/ebw8image11maxent.webp)
* [`EThresholdMode_Isodata`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html): Iteratively calculates a threshold $T$ as the midpoint between the mean gray level of pixels below and above $T$:
  $$
  T = \frac{\mu_{\text{below}} + \mu_{\text{above}}}{2}
  $$

  where $\mu_{\text{below}}$ is the mean gray level of pixels with values $< T$, and $\mu_{\text{above}}$ is the mean gray level of pixels with values $\geq T$. The process repeats until $T$ converges.
  ![](assets/images/easyimage/ebw8image11isodata.webp)

<a id="easyimage-thresholding-manual"></a>

## Manual Thresholding

Manual thresholds require that the user supplies one or two threshold values:

* **one** value to the [`EasyImage::Threshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#ac6f69c1aac9510ac6cd639acb27979ee) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#ae13e71e4c82873cb82b2b72bb9fb1b7e) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#a0b095d913c4676c6c3be8f73e4c06dea) function to classify source image pixels into two classes and create a bi-level image. This can be:
  * `relativeThreshold` is the percentage of pixels below the threshold. The Threshold function then computes the appropriate threshold value, or
  * `absoluteThreshold`. This value must be within the range of pixel values in the source image.
* **two** values to the [`EasyImage::DoubleThreshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#a2a224c0765addcb26073a721571da48d) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#a08f4bdaca7cad63ab81d4c16ce161b93) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#ae759f1761e42e4970b80895e723912f2) function to classify source image pixels into three classes and create a tri-level image.
  * `LowThreshold` is the lower limit of the threshold
  * `HighThreshold` is the upper limit of the threshold
* **two** values to the [`EasyImage::Threshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#ac6f69c1aac9510ac6cd639acb27979ee) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#ae13e71e4c82873cb82b2b72bb9fb1b7e) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#a0b095d913c4676c6c3be8f73e4c06dea) functions to classifiy color image pixels into two classes and create a bi-level image.
  * Pixels in the given `min`/`max` range.
  * Pixels outside the given `min`/`max` range.

<a id="easyimage-thresholding-histogram"></a>

## Histogram Based

This is a two-step process: first compute the histogram with [`EasyImage::Histogram`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#a844bd52f1e6c61fe0264412ed74d15de) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#ab9c24a850843e901bd1dc7fc360376b5) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#ad1a8be6eacbbef1eef655ebdfc015aa9) (see [Computing Image Statistics](computing_image_statistics.html.md)), then derive and apply the threshold from it using [`EasyImage::HistogramThreshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#a6d389ca5b5e94ce4dd06b0f9e20fb1e7) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#acdd6c18bf8436cce2f388d464d3f9cd0) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#a63a92b4c45a14a1210e7351d46a1acfb) or [`EasyImage::HistogramThresholdBW16`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#af7b212cbb708338b9f7b1663da19f083) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#ad7aa24cd0d1314803f1c1954cd19ca6c) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#a57651964ed17d6cf96794e60f6e2a178).

The main advantage over **AutoThreshold** is that the histogram can be reused, useful if you need to threshold the same image multiple times or share the histogram with other operations.

These functions also return the average gray levels of each pixel below and above the threshold.

<a id="easyimage-thresholding-autothreshold"></a>

## AutoThreshold

[`EasyImage::AutoThreshold`](../../../doxygen_cpp/classeVision_1_1EasyImage.html#ad57cefa9582d0c69c9f4a9fd45241074) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EasyImage.html#a64b63d28e31b8c9907016038b0a9b8a0) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EasyImage.html#ae0ef49133e1c9938467fc9b58b3f7e1a) is a single-call alternative to the **Histogram Based** approach: it computes the histogram internally, derives the threshold automatically, and applies it, all in one step.

Use it for straightforward thresholding when you do not already have a histogram. If you do have one (for example, to reuse it across multiple operations and avoid redundant computation), use `HistogramThreshold` directly instead.

Supported threshold modes: [`EThresholdMode_Relative`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html), [`EThresholdMode_MinResidue`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html), [`EThresholdMode_MaxEntropy`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html) and [`EThresholdMode_Isodata`](../../../doxygen_cpp/namespaceeVision.html#a7fdafd024ed604588f7a9792905f3e4b) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#a8e31721ead762012592893688fd19af4) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EThresholdMode.html).

<a id="easyimage-thresholding-quick-links"></a>

## See Also

Easy to understand, ready to copy code snippets.

Reduce the noise and improve your segmentation.

Compute image histograms to speed up or inform threshold selection.
