hysegment.m

来自「本人改进的关于自适应分割方法」· M 代码 · 共 69 行

M
69
字号
% image_in : input image
% S : the factor to improve the threshold T
% the method of segment with threshold
% S =  0.6;
%--------------------------------------------------------------------------
[ filename, pathname ] = uigetfile( '*', 'Select an image to segment' );
% if user cancelled dialog box then exit
if( filename == 0 )
  return;
end
I0 = imread( strcat( pathname, filename ) );
S = input('Enter the parameter for improve threshold(S):');
%--------------------------------------------------------------------------
%function Segment2(image_in,S)
tic
D = 0.58;
I00 = medfilt2(I0);
I = double(I00);
[x,y] = size(I);
T = otsu_threshold(I0);
TT = 0;
s0 = 0; n0 = 0;
s1 = 0; n1 = 0;
allow = 0.05;
d  = abs(T-TT);
count = 0;
while(d>=allow)
    count = count + 1;
    for i = 1 : x
        for j = 1 : y
            if(I(i,j)>=T)
                s0 = s0 + I(i,j);
                n0 = n0 + 1;
            end
            if(I(i,j)<T)
                s1 = s1 + I(i,j);
                n1 = n1 + 1;
            end
        end
    end
    T0 = s0/n0;
    T1 = s1/n1;
    TT = D * T0 + (1-D) * T1;
    TT = S * T + (1-S) * TT;
    d = abs(T-TT);
    T = TT;
end
seg = zeros(x,y);
foreimage = zeros(x,y);
for i = 1 : x
    for j = 1 : y
        if(I(i,j)>=T)
            seg(i,j) = 1;
            foreImage(i,j) = I0(i,j);
        end
    end
end
SI = 1 - seg;
se1 = strel('square',3);
SI1 = imerode(SI,se1);
BW = SI - SI1;
t = toc
figure,imshow(BW);
figure,imshow(seg);
%imwrite(seg,'hyresult.bmp');
figure,imshow(foreImage);

            
    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?