# OTSU Thresholding - an adaptive thresholding way for binarization in image processing. By going through all possible threshold values (from 0 to 255), it can find the optimal threshold value of input image. [[see more](https://medium.com/@hbyacademic/otsu-thresholding-4337710dc519#baf7)] ## Preliminaries - Given an image, threshold $t$ separates all the pixels $P_i$ into $C_1$ and $C_2$, where $C_1 = \{P_i \mid P_i \gt t \}$ and $C_2 = \{P_i \mid P_i \leq t \}$ - $W_1$ and $W_2$ are the probabilities of the two classes separated by $t$, where $W_1+W_2=1$ - $V_T$ : total variance - $V_b$ : between-class variance - $V_w$ : within-class variance - $V_T = V_b + V_w$ - $V_1$ : $C_1$ variance - $V_2$ : $C_2$ variance - $V_w = W_1 \times V_1 + W_2 \times V_2$ - $\mu_T$ : total mean - $\mu_1$ : $C_1$ mean - $\mu_2$ : $C_2$ mean - $W_1 \times \mu_1 + W_2 \times \mu_2 = \mu_T$ - $N$ : total pixel count - $\sum\limits_{i=0}^N{P_i} = N \times \mu_T$ ## $V_T$ and $V_w$ Expansion - $V_t \\ =\frac{\sum\limits_{i=1}^N{(P_i - \mu_t)^2}}{N} = \frac{\sum\limits_{i=0}^N{({P_i}^2 - 2 \times P_i \times \mu_t + {\mu_t}^2)}}{N}\\ =\frac{(\sum\limits_{i=0}^N{P_i}^2) - {N \times \mu_t}^2}{N} =\frac{\sum\limits_{i=0}^N{P_i}^2}{N} - {\mu_t}^2$ - $V_w \\ = W_1 \times V_1 + W_2 \times V_2 \\ = W_1 \times (\frac{\sum\limits_{P_i \in C_1}{(P_i - \mu_1)^2}}{N \times W_1}) + W_2 \times (\frac{\sum\limits_{P_i \in C_2}{(P_i - \mu_2)^2}}{N \times W_2}) \\ = \frac{\sum\limits_{P_i \in C_1}{(P_i - \mu_1)^2}}{N} + \frac{\sum\limits_{P_i \in C_2}{(P_i - \mu_2)^2}}{N} \\ = \frac{(\sum\limits_{P_i \in C_1}{P_i}^2) - N \times W_1 \times {\mu_1}^2}{N} + \frac{(\sum\limits_{P_i \in C_2}{P_i}^2) - N \times W_2 \times {\mu_2}^2}{N} \\ = \frac{\sum\limits_{P_i \in C_1}{P_i}^2}{N} - W_1 \times {\mu_1}^2 + \frac{\sum\limits_{P_i \in C_2}{P_i}^2}{N} - W_2 \times {\mu_2}^2$ ## Vb = ? - $V_b \\ = V_t - V_w \\ = (\frac{\sum\limits_{i=0}^N{P_i}^2}{N} - {\mu_t}^2) - (\frac{\sum\limits_{P_i \in C_1}{P_i}^2}{N} - W_1 \times {\mu_1}^2 + \frac{\sum\limits_{P_i \in C_2}{P_i}^2}{N} - W_2 \times {\mu_2}^2) \\ = W_1 \times {\mu_1}^2 + W_2 \times {\mu_2}^2 {\color{red}{-{\mu_t}^2}} \\ = W_1 \times {\mu_1}^2 + W_2 \times {\mu_2}^2 - {\color{red}{2 \times {\mu_t}^2 + {\mu_t}^2}} \\ {\color{orange}{\because \mu_t = W_1 \times \mu_1 + W_2 \times \mu_2}} \\ {\color{orange}{\because W_1 + W_2 = 1}} \\ = W_1 \times {\mu_1}^2 + W_2 \times {\mu_2}^2 - 2 \times (W_1 \times \mu_1 + W_2 \times \mu_2) \times {\mu_t} + (W_1 + W_2) \times {\mu_t}^2 \\ = W_1 \times ({\mu_1}^2 - 2 \times {\mu_1} \times {\mu_t} + {\mu_t}^2) + W_2 \times ({\mu_2}^2 - 2 \times {\mu_2} \times {\mu_t} + {\mu_t}^2) \\ = {\color{magenta}{W_1 \times (\mu_1 - \mu_t)^2 + W_2 \times (\mu_2 - \mu_t)^2}} \\ {\color{orange}{\because \mu_t = W_1 \times \mu_1 + W_2 \times \mu_2}} \\ = W_1 \times [(1 - W_1) \times \mu_1 - W_1 \times \mu_2]^2 + W_2 \times [W_1 \times \mu_1 - (1 - W_2) \times \mu_2]^2 \\ {\color{orange}{\because W_1 + W_2 = 1}} \\ = W_1 \times [(W_2) \times \mu_1 - W_1 \times \mu_2]^2 + W_2 \times [W_1 \times \mu_1 - (W_1) \times \mu_2]^2 \\ = W_1 \times W_2 \times (\mu_1 - \mu_2)^2 \times (W_2 + W_1) \\ {\color{orange}{\because W_1 + W_2 = 1}} \\ = {\color{blue}{W_1 \times W_2 \times (\mu_1 - \mu_2)^2}}$ - $V_b \\ = {\color{magenta}{W_1 \times (\mu_1 - \mu_t)^2 + W_2 \times (\mu_2 - \mu_t)^2}} \\ = {\color{blue}{W_1 \times W_2 \times (\mu_1 - \mu_2)^2}}$