# Thresholding

$$
a = b \*b
$$

$${\label{eq.1}} F=\alpha A+\beta W=\beta(\frac{\alpha}{\beta}A+W)$$

\begin{equation}{\label{eq.1}} F=\alpha A+\beta W=\beta(\frac{\alpha}{\beta}A+W) \end{equation},

**A Short Summary of Thresholding Algorithm**

* The basic concept of thresholding: to segment objects from the background based on intensity values.
* A simple method is making the output result as a binary image as

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-89595cc4c2102af126a3abd77131753b337b095f%2F0.png?alt=media)

* Multiple thresholding

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-094dae2a8b88afa42f49eb43caf4d255f2373905%2F1.png?alt=media)

1. Analyze the intensity histogram and select the initial estimation of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-2a975e958eeb4aa583f12ea66635f2d3a82204f0%2F4%20\(1\).png?alt=media)(usually the mean of the image intensity). Let the intensity of the input image is defined as g(x,y).
2. Segment the image by two groups on the histogram using the value of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-bf1c5ec254570c14cdc0b0aca213e3dde8d15057%2F6.png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-3dad39207107da5ea57a01abb7a09ce8bfe328eb%2F7.png?alt=media)

1. Find the mean of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-8e33ec4a27c821b6876383456aa7c1aa54e31a7f%2F8.png?alt=media)and ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-0a1a44a766ee87ecd3973b9a4055146fae6da65d%2F9.png?alt=media)(i.e. *m1* and *m2*)
2. The new ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-b1788c80845a37ed99f9def8c3fff6ede3109f36%2F10.png?alt=media)value at kth iteration

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-588e9809393bf8f17acfa1b892cba7d78ee5305c%2F11.png?alt=media)

1. repeat from step 2 until ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-6e55a48cfa1003244127ae13795c41351390ddc9%2F12.png?alt=media), where ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-82d20c59be3ae4d5c376fa7922aeb1603bb09b11%2F13.png?alt=media)
2. OTSU’s method.
3. The aim is to maximize the between-class variance based on the histogram of an image
4. First, calculate the normalized histogram ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-5ce94c4eebdb02319b536f6ed11a0e653aba343d%2F14.png?alt=media), with *ni* is the number of pixels with the intensity level *I*, and it should satisfy

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-b28b16effe85901faa3e8bdf425502ff1dc5c586%2F15.png?alt=media)

* Let us define the mean intensity of the entire image as

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-783b00246bf6271c805ff413024d953b87da2ed1%2F16.png?alt=media)

which is equivalent to

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-1ffcb1b5da53ce7c4ddf619ae3dcd7ae3328adc1%2F40%20\(1\).png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-720bd8c13e550b1e1702681138fcac4322289b1b%2F20.png?alt=media)

Probability of![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-486a4a56cde1bc0afc6767755b51fedc184be384%2F21.png?alt=media), given that ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-4f36de56a759ef8464759b3d681125ec9ed22004%2F22.png?alt=media)comes from the class ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-d3b4f9e34f4a7dbc93b631416571c0e6af48d6f6%2F23.png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-cdc52085fe6a44516f5f813a1c38ed316c3e3eca%2F24.png?alt=media) (using Bayes’ formula)

Note: Bayes formula

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-d4f0dea59ee0c5c6a281cbaea38e11fa90a707bd%2F25.png?alt=media)

* \*
* Then, the mean of intensity of class ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-2d20be83592518a98f1608e593e6562a6de4d23c%2F30.png?alt=media) becomes

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-c4b39964f2ec8aa2bf7adfe81d8b9bf65fce2da8%2F31%20\(2\).png?alt=media)

* Similarly, the mean of intensity of class ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-63421ef52b27171cc5164eb21d1ec5f6e962e4ea%2F32.png?alt=media) becomes

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-e52386bae8a9bed558ba239621b7466f0b6f88e4%2F33.png?alt=media)

where ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-d995480c57245112cde991be7eb20a48ba54c3b9%2F34.png?alt=media) and ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-8e1539ff7b2551cc86a537fbe4062e0850ae9969%2F35.png?alt=media)

* The cumulative mean intensity from ‘0’ up to level ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-aeec77f265b4a083b514021b6a130f5cbba67b44%2F36.png?alt=media)is defined as

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-658205352efbeb1b76a849cfbc89160d0cc68052%2F37.png?alt=media) // ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-c4b39964f2ec8aa2bf7adfe81d8b9bf65fce2da8%2F31%20\(2\).png?alt=media)

* Thus, we can express the total mean intensity as

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-c68312939bac6b34e5c7eee52f6760b7461f3e0e%2F39.png?alt=media)

since the total mean intensity is ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-1ffcb1b5da53ce7c4ddf619ae3dcd7ae3328adc1%2F40%20\(1\).png?alt=media)

* To evaluate the ‘goodness’ of the threshold values of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-7918a88b8dff65c52ee6ee82ce0cb49a0933be9c%2F41.png?alt=media), we can design a score

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-7ba7b6c49d0b8ad4cf9b0513506a8527f46bf897%2F42.png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-6eb917d05abbb96479f45aec568464d4b6959dd6%2F43.png?alt=media)is the global variance

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-edec0ae884a7ae1f4a3a2020323bcfb16cf1839d%2F44.png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-b29593e39a6a7720bb642afac700866e31703430%2F45%20\(2\).png?alt=media)is the between-class variance

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-e012ab7ad4f8c9f9e0cf388553fd64ad22b49df4%2F46.png?alt=media)

The further the two means of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-08f92fb6493abcb70eeb90aa6ae3c4a0c7c1778f%2F47.png?alt=media)and ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-10c212ad37080d9da0bbc48c81962136c36ff5ce%2F48.png?alt=media)are from each other, the larger ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-b29593e39a6a7720bb642afac700866e31703430%2F45%20\(2\).png?alt=media) will be

larger value of *η.*

To make the calculation simpler, we transform the formula as

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-93dfb24262d3d98f378a3d05def4942ba453eefc%2F50.png?alt=media)

The Procedure of Otsu Method

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-94e7becbfbe0f612224f9c7f221be3ee15c42f1c%2F51.png?alt=media)

Aim: obtain the maximum ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-597824a656e78cf6ac04d40d05c0f60d7de57ec9%2F62%20\(1\)%20\(2\).png?alt=media) from the calculation of ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-597824a656e78cf6ac04d40d05c0f60d7de57ec9%2F62%20\(1\)%20\(2\).png?alt=media)for all values of *k*

1. Apply an image filter prior to thresholding.
2. Compute the normalized histogram ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-2361ca80cf13eb5a5545611dc53ef2380c45c13a%2F54.png?alt=media)
3. Compute the cumulative sum ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-997716bb7f40b867b6e1c3300d31b10a3146cd6b%2F55.png?alt=media), ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-7a0e7027c206286f4871b9b427ea9e8b91bb7070%2F56%20\(1\)%20\(1\).png?alt=media)to![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-f317109700051e326e982914fb09547a59c3ab1f%2F57%20\(1\)%20\(1\).png?alt=media)
4. Compute the cumulative mean ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-5fee0ac74684bd01b25e015468d11baebb88ff5a%2F58.png?alt=media), ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-7a0e7027c206286f4871b9b427ea9e8b91bb7070%2F56%20\(1\)%20\(1\).png?alt=media)to![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-f317109700051e326e982914fb09547a59c3ab1f%2F57%20\(1\)%20\(1\).png?alt=media)
5. Compute the global intensity mean ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-9e87525816693cbfe6e830db9b50bf148c584e3a%2F61.png?alt=media)
6. Compute ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-597824a656e78cf6ac04d40d05c0f60d7de57ec9%2F62%20\(1\)%20\(2\).png?alt=media), for all ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-4b5eb191a17d6f9b0d8e5b4999c81bd54a18f372%2F63.png?alt=media)
7. Find k\* at which ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-56cb645e6b38d8b65f4b805503b3b56a8d674423%2F64.png?alt=media) is at maximum
8. Apply threshold at ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-71fda88d4365f738ba9b3dd72a7cad3d493f2efe%2F65.png?alt=media)\*
9. Local thresholding

Method 1. Image partitioning

* Subdivide an image into non overlapping rectangles. Apply otsu threshold in each sub division.
* Works well when the objects and background occupy reasonably comparable size.
* But fails if either object or background is small.

Method 2. Based on local image property

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-5ad0b8bd3514258003241229f74687cffd72bcee%2F70.png?alt=media)

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-ec5be94eb0bf4416bfe68b9170d0abcb93cc34c2%2F71.png?alt=media)

preferable if background is nearly uniform.

Method 3. Moving average.

* Scan line by line in zigzag to reduce illumination effect

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-54087485c49994a4cf4c440cd589fa27a78c4c6a%2F72.png?alt=media)

Where ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-e38fca1b1363ba473f8cfcafff76bb239cb5e4d2%2F73.png?alt=media)is intensity of the point at step ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-89c23c9ee949638fd5a729d44716aaf498808fb0%2F74.png?alt=media)in the number of points area in M.A ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-ac40b21873d5b4b1940cb72e99c17c3512be8e60%2F75.png?alt=media)

Use ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-814b943b48b2a0785a145b4867cd23219cd78623%2F76.png?alt=media)

See example with text image corrupted by spot shading

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-dd24f148217654680687fdf184194da475569c1d%2F77.png?alt=media) ![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-7025302014821497d67c507d665dcd3b6a5fa45e%2F78.png?alt=media)
