Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Implementing Otsu binarization for faded images of documents

Date: 2023-01-18 12:55:21

I'm trying to implement Otsu binarization technique on document images such as the one shown:

Could someone please tell me how to implement the code in MATLAB?

 Answers:

Taken from Otsu's method on Wikipedia

 

I = imread('cameraman.tif');

Step 1. Compute histogram and probabilities of each intensity level.

 

nbins = 256; % Number of bins
counts = imhist(I,nbins); % Each intensity increments the histogram from 0 to 255
p = counts / sum(counts); % Probabilities

Step 2. Set up initial omega_i(0) and mu_i(0)

 

omega1 = 0;
omega2 = 1;
mu1 = 0;
mu2 = mean(I(:));

Step 3. Step through all possible thresholds from 0 to maximum intensity (255)

Step 3.1 Update omega_i and mu_i

Step 3.2 Compute sigma_b_squared

 

for t = 1:nbins
    omega1(t) = sum(p(1:t));
    omega2(t) = sum(p(t+1:end));
    mu1(t) = sum(p(1:t).*(1:t)');
    mu2(t) = sum(p(t+1:end).*(t+1:nbins)');
end

sigma_b_squared_wiki = omega1 .* omega2 .* (mu2-mu1).^2; % Eq. (14)
sigma_b_squared_otsu = (mu1(end) .* omega1-mu1) .^2 ./(omega1 .* (1-omega1)); % Eq. (18)

Step 4 Desired threshold corresponds to the location of maximum of sigma_b_squared

 

[~,thres_level_wiki] = max(sigma_b_squared_wiki);
[~,thres_level_otsu] = max(sigma_b_squared_otsu);

There are some differences between the wiki-version eq. (14) in Otsu and the eq. (18), and I don't why. But the thres_level_otsu correspond to the MATLAB's implementation graythresh(I)


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com