Follow by Email

Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Recent Posts

Otsu’s thresholding without using MATLAB function graythresh

                To perform the thresholding I followed these steps:
a.       Reshape the 2 dimensional grayscale image to 1 dimensional.
b.      Find the histogram of the image using  ‘hist’ function.
c.       Initialize a matrix with values from 0 to 255
d.      Find the weight , mean and the variance for the foreground and background
e.      calculate weight of foreground* variance of foreground + weight of background* variance of background.
f.       Find the minimum value.
%To threshold image without using graythresh function
function mygraythresh
global H Index;

Here I converted the 2d matrix to 1d matrix.

The histogram of the values from 0 to 255 is stored.
For instance, G(1) contains the number of occurrence of the value zero in the image.
 'index' is a 1 dimensional matrix ranging between 0 and 255
 result=zeros(size([1 256]));

To avoid many for loops I used only 1 for loop and a function to calculate the weight, mean and variance.

Let me explain the foreground and the background for a value of ‘i’.
if ‘i’ value is 5 then the foreground values will be 0,1,2,3,4,5
and the background values will be 6 to 255.

for i=0:255
After calculating the weights and the variance, the final computation is stored in the array ‘result’.
 %Find the minimum value in the array.                   [threshold_value,val]=min(result);
Now convert the image to binary with the calculated threshold value.
 function [weight,var]=calculate(m,n)
%Weight Calculation
%Mean Calculation
%Variance calculation.
Threshold value:0.3242

like button Like "IMAGE PROCESSING" page


r00se said... Reply to comment

hi pleasssssssssssssssssse i need you help i need the answer of this Q.
use a thresholding program (or write your own
and see an image at many thresholding levels .find a suitable thresholding to get the best representation of an object in the image .now select another object and find the best thresholding for it.repeat this experiment with several images
pleasssssssssssse help me before next sunday
my regard

HumptyDumpty said... Reply to comment

very nice code

abcxyz said... Reply to comment

the threshold value is not being displayed. can you please tell what is the mistake

Aaron Angel said... Reply to comment


The threshold value is stored in tval.
Use 'display tval' to check the threshold value.

Roger Gonçalves said... Reply to comment

Hi, I'm having trouble with this part:

for i=0:255



this function does not work! How should I use it? grateful

Dattatreya mankame said... Reply to comment

threshold value is not displayed even after I gave display tval. Can u suggest me what to do

Jarcc Soulsay said... Reply to comment

Hi help me please i can't do it i have trouble

for i=0:255



this function does not work! How should I use it? grateful

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping