% using the Otsu algorithm for binary images
Close All;clear ALL;CLC
%%
I = Imread (' c.jpg ');
Scoreimage0=rgb2gray (I);
Scoreimage=double (SCOREIMAGE0);
[Height,length]=size (Scoreimage);
Totalpixel=height*length;
% Maxpixnumber=max (max (scoreimage));
This place is not calculated for future convenience, but the default maximum is 255.
Pixelcount=zeros (1,256);% counts the number of individual pixel values
% 0-256
For I=1:length
For J=1:height
Number=scoreimage (j,i) +1;
Pixelcount (number) =pixelcount (number) +1;
End
End
% probability
Pf=pixelcount/totalpixel;
% forward cumulative probability density function
Cpf=zeros (1,256);
CPF (1) =PF (1);
For i=2:256
CPF (i) =CPF (i-1) +PF (i);
End
% post-cumulative probability density function
Bpf=zeros (1,256);
BPF (=PF) (256);
For J=256:-1:2
BPF (J-1) =BPF (j) +PF (j-1);
End
% forward Cumulative expectations
Meanforward=zeros (1,256);
Meanforward (1) = 1;
For i=2:256
Meanforward (i) =meanforward (i-1) * (CPF (i-1)/CPF (i)) + (PF (i)/CPF (i)) *i;
End
% back to cumulative expectation
Meanback=zeros (1,256);
Meanback (Max (scoreimage)) =max (Max (scoreimage));
For I=max (Max (scoreimage)): -1:2
Meanback (i-1) =meanback (i) * (BPF (i)/BPF (i-1)) + (PF (i-1)/BPF (i-1)) * (i-1);
End
Delta=cpf.*bpf.* (meanforward-meanback). ^2;
[Value,index]=max (delta);
% Returns a logical image
binariedimage=scoreimage>index;
Figure
Subplot (1,2,1); Imshow (SCOREIMAGE0); Title (' original image ');
Subplot (1,2,2); Imshow (binariedimage); Title (' Processing chart ');
Here is the original: http://blog.sina.com.cn/s/blog_98ddf7cb0101chu3.html
Otsu algorithm (two)