Skip to main content

Implementing Otsu's Method Using MATLAB/Python

Vang-zAlgorithmImage ProcessingMatlabPythonAbout 829 wordsAbout 3 min...

Abstract

The Otsu's methodopen in new window[1] was proposed in 1979, which segmented images by maximizing variance between classes. In other words, the Otsu's method is a nonparametric segmentation method that divides an image into different regions based on the intensity of the pixels. This article will introduce the Otsu's method in two sections. First, we will understand the principles of the Otsu's method by reviewing the mathematical formulas in the literature. Finally, we will implement the details using Matlabopen in new window/Pythonopen in new window.

Formula[2]

Assume that LL is the pixel intensity levels of an image which has size of Mโˆ—NM * N:

n=n0+n1+...+nLโˆ’1 n = n_0 + n_1 + ... + n_{L-1}

Phi=nin,โˆ‘i=0Lโˆ’1Phi=1 Ph_i = \frac{n_i}{n}, \sum_{i=0}^{L-1}Ph_i=1

where nn indicates the total number of image pixels, nin_i is the number of pixels for intensity level ii, and probability distribution of all intensity levels is represented by PhiPh_i.

Assume there is a threshold thth, in which th is between 00 and Lโˆ’1L-1, then the image can be divided into two classes according to thth. The first class, C1C_1, contains all pixels with pixel intensity levels between 00 ~ thth, while C2C_2 contains the rest of the pixels.

ฯ‰1(th)=โˆ‘i=0thPhi,ฯ‰2(th)=โˆ‘i=th+1Lโˆ’1Phi=1โˆ’Ph1(th) \omega_1(th) = \sum_{i=0}^{th}Ph_i, \omega_2(th) = \sum_{i=th+1}^{L-1}Ph_i = 1 - Ph_1(th)

where ฯ‰1(th)\omega_1(th) and ฯ‰2(th)\omega_2(th) represent the cumulative probability distributions for C1C_1 and C2C_2, respectively.

ฮผ1(th)=โˆ‘i=0thiP(iโˆฃC1)=โˆ‘i=0thiP(C1โˆฃi)P(i)P(C1)=1ฯ‰1(th)โˆ‘i=0thiPhi \mu_1(th) = \sum_{i=0}^{th}iP(i|C_1) = \sum_{i=0}^{th}\frac{iP(C_1|i)P(i)}{P(C_1)} = \frac{1}{\omega_1(th)}\sum_{i=0}^{th}iPh_i

ฮผ2(th)=โˆ‘i=th+1Lโˆ’1iP(iโˆฃC2)=โˆ‘i=th+1Lโˆ’1iP(C2โˆฃi)P(i)P(C2)=1ฯ‰2(th)โˆ‘i=th+1Lโˆ’1iPhi \mu_2(th) = \sum_{i=th+1}^{L-1}iP(i|C_2) = \sum_{i=th+1}^{L-1}\frac{iP(C_2|i)P(i)}{P(C_2)} = \frac{1}{\omega_2(th)}\sum_{i=th+1}^{L-1}iPh_i

ฮผth=โˆ‘i=0thiPhi,ฮผT=โˆ‘i=0Lโˆ’1iPhi \mu_th = \sum_{i=0}^{th}iPh_i, \mu_T = \sum_{i=0}^{L-1}iPh_i

where ฮผ1(th)\mu_1(th) and ฮผ2(th)\mu_2(th) indicate the mean intensity levels for C1C_1 and C2C_2, respectively. ฮผth\mu_th denotes the mean intensity level from 00 to thth. ฮผT\mu_T represents the mean intensity level for the whole image. Hence, the objective function of the maximizing variance between classes can be expressed as below:

ฯ‰1(th)+ฯ‰2(th)=1 \omega_1(th) + \omega_2(th) = 1

ฯ‰1(th)โ‹…ฮผ1(th)+ฯ‰2(th)โ‹…ฮผ2(th)=ฮผT \omega_1(th) \cdot \mu_1(th) + \omega_2(th) \cdot \mu_2(th) = \mu_T

ฮดB2(thโˆ—)=max{ฮดB2(th)},0โ‰คthโ‰คLโˆ’1 \delta_B^2(th^*) = max\{\delta_B^2(th)\}, 0 \leq th \leq L-1

Based on the above formula, we can find a thโˆ—th^* in the range of 00 ~ Lโˆ’1L-1 to maximize ฯƒB2ฯƒ_B^2 to achieve a effective image segmentation.

Code

function fit = otsu(N, level, x, prob)
    fit = zeros(1, N);
    for j = 1:N
        fit(j) = sum(prob(1:x(j, 1) - 1)) * (sum((1:x(j, 1) - 1) .* ... 
                 prob(1:x(j, 1) - 1) / sum(prob(1:x(j, 1) - 1))) - ...
                 sum((1:255) .* prob(1:255)) ) ^ 2;
        for jlevel = 2:level - 1
            fit(j) = fit(j) + sum(prob(x(j, jlevel - 1):x(j, jlevel) - 1)) * ...
                     (sum((x(j, jlevel - 1):x(j, jlevel) - 1) .* ...
                     prob(x(j, jlevel - 1):x(j, jlevel) - 1) / ...
                     sum(prob(x(j, jlevel - 1):x(j, jlevel) - 1))) - ...
                     sum((1:255) .* prob(1:255))) ^ 2;
        end
        fit(j) = fit(j) + sum(prob(x(j, level - 1):255)) * ...
                 (sum((x(j, level - 1):255) .* prob(x(j, level - 1):255) / ...
                 sum(prob(x(j, level - 1):255))) - sum((1:255) .* prob(1:255))) ^ 2;
        if isnan(fit(j)) || isinf(fit(j))
            fit(j) = eps;
        end
    end
    fit = fit';
end

References


  1. Otsu, N. A threshold selection method from gray-level histograms. IEEE transactions on systems, 9, 62-66 (1979).open in new window โ†ฉ๏ธŽ

  2. Wang, Z., Mo, Y. & Cui, M. An Efficient Multilevel Threshold Image Segmentation Method for COVID-19 Imaging Using Q-Learning Based Golden Jackal Optimization. J Bionic Eng, 20, 2276โ€“2316 (2023).open in new window โ†ฉ๏ธŽ

Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.15.8