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.
MATLAB CODE:
%To threshold image without using graythresh function
function mygraythresh
global H Index;
B=imread('tire.tif');

Here I converted the 2d matrix to 1d matrix.
V=reshape(B,[],1);

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.
G=hist(V,0:255);
H=reshape(G,[],1);
 'index' is a 1 dimensional matrix ranging between 0 and 255
 Ind=0:255;
 Index=reshape(Ind,[],1);
 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
     [wbk,varbk]=calculate(1,i);
     [wfg,varfg]=calculate(i+1,255);
    
After calculating the weights and the variance, the final computation is stored in the array ‘result’.
result(i+1)=(wbk*varbk)+(wfg*varfg);
    
    
 end
 %Find the minimum value in the array.                   [threshold_value,val]=min(result);
    
     tval=(val-1)/256;
     
Now convert the image to binary with the calculated threshold value.
bin_im=im2bw(B,tval);
     figure,imshow(bin_im);
 function [weight,var]=calculate(m,n)
%Weight Calculation
     weight=sum(H(m:n))/sum(H);
    
%Mean Calculation
     value=H(m:n).*Index(m:n);
     total=sum(value);
     mean=total/sum(H(m:n));
     if(isnan(mean)==1)
         mean=0;
     end
%Variance calculation.
    value2=(Index(m:n)-mean).^2;
     numer=sum(value2.*H(m:n));
     var=numer/sum(H(m:n));
     if(isnan(var)==1)
         var=0;
     end
    
 end
 end
 
                     
                   
                       
      
     
Threshold value:0.3242

13 comments:

  1. 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

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

    ReplyDelete
  3. @abcxyz

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

    ReplyDelete
  4. Hi, I'm having trouble with this part:

    for i=0:255

    [wbk,varbk]=calculate(1,i);

    [wfg,varfg]=calculate(i+1,255);

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

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

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

    for i=0:255

    [wbk,varbk]=calculate(1,i);

    [wfg,varfg]=calculate(i+1,255);

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

    ReplyDelete
  7. function mygraythresh
    global H Index;
    B=imread('C:\Users\h141\Desktop\MPS\rice.png');
    V=reshape(B,[],1);
    G=hist(V,0:255);
    H=reshape(G,[],1);

    Ind=0:255;
    Index=reshape(Ind,[],1);
    result=zeros(size([1 256]));

    for i=0:255
    [wbk,varbk]=calculate(1,i);
    [wfg,varfg]=calculate(i+1,255);
    result(i+1)=(wbk*varbk)+(wfg*varfg);
    end
    %Find the minimum value in the array.
    [threshold_value,val]=min(result);
    tval=(val-1)/256
    bin_im=im2bw(B,tval);
    figure,imshow(bin_im);

    function [weight,var]=calculate(m,n)
    %Weight Calculation
    weight=sum(H(m:n))/sum(H);
    %Mean Calculation
    value=H(m:n).*Index(m:n);
    total=sum(value);
    mean=total/sum(H(m:n));
    if(isnan(mean)==1)
    mean=0;
    end

    %Variance calculation.
    value2=(Index(m:n)-mean).^2;
    numer=sum(value2.*H(m:n));
    var=numer/sum(H(m:n));
    if(isnan(var)==1)
    var=0;
    end
    end
    end

    ReplyDelete
  8. Algorithm 1. The proposed inpainting procedure
    I(x,y): Original Image, IM(x,y): Inpainting Mask
    Pi(x,y): Inpainting result of pass i, BG(x,y): Estimated
    Background
    Ix, Iy: Image Width and Height
    xstart[4] = 0,0,Ix,Ix, xend[4]=Ix,Ix,0,0
    ystart[4] = 0,Iy,0,Iy, yend[4]=Iy,0,Iy,0
    for i ¼ 1 ! 4 do
    M ¼ IM
    for y ¼ ystart½i ! yend½i do
    for x ¼ xstart½i ! xend½i do
    if M(x,y) = 0 then
    Pi(x,y) = Average (I(x 1,y) M(x 1,y),I(x,y 1)
    M(x,y 1),
    I(x + 1,y) M(x + 1,y),I(x,y + 1) M(x,y + 1))
    I(x, y) = Pi(x, y)
    M(x,y) = 1
    end if
    end for
    end for
    end for
    for y ¼ ystart½1 ! yend½1 do
    for x ¼ xstart½1 ! xend½1 do
    BG(x,y) = min(Pi(x,y)), i ¼ 1; ... ; 4
    end for
    end for
    How to implement source in matlab? Please help me .

    ReplyDelete
  9. @Krzysztof Pastuszak

    Thank-you Mr. Krzysztof Pastuszak for posting this.. It works really well and helped me alot...Keep it up..!
    (Y)

    ReplyDelete
  10. very clear and user friendly coding!

    ReplyDelete
  11. i cant view the final image.... could someone please help me with that???

    ReplyDelete