matlabはOtsuグローバルしきい値処理を実現する

482 ワード

img=imread('C:\Users\Administrator\Desktop\lwx.JPG');
im=rgb2gray(img);

[row,rol] = size(im);
h = imhist(im);
%   
p = h / (row*rol);
%     
m = zeros(256,1);
%   
for i = 2:256
    m(i) = m(i-1)+p(i)*(i-1);
    p(i) = p(i-1)+p(i);
    %     0    
    if(p(i-1) == 0)
        p(i-1) = 0.0001;
    end
end
%     
mg = sum(sum(im))/row/rol;
p1 = ones(256,1);

%     
a = ((mg*p-m).^2)./(p.*(p1-p));
[max_a,yu] = max(a);
yu-1
graythresh(im)*255