gusucode.com > 数字图像处理,分水岭算法matlab源码,可以实现图像分割 > matlabwatershed.txt

    %Tag:matlab 数字图像处理 分水岭算法

% 本程序能够实现分水岭算法
% 数字图像处理作业3

% 分水岭算法
clear, close all;
clc;

% PathName='Andrew_02_0_01BlueLEDg.jpg';%t为自填内容,下面p类似
% FileName=[PathName 'p'];  
Image=imread('Andrew_02_0_01BlueLEDg.jpg');
subplot(2,2,1);subimage(Image);title('原图');;pixval on;

B=[1,1,1;1,1,1;1,1,1];%方形结构元
E8=[-1,0;-1,1;0,1;1,1;1,0;1,-1;0,-1;-1,-1];    % 8-连通结构元坐标
maskLenth=length(E8);       % 结构元点的个数

[X,Y]=size(Image);

%原始图像image 赋值给A1
n=1;
A(:,:,n)=Image;
M=zeros(X,Y);
Mark_Image=zeros(X,Y);

%产生距离图
while sum(sum(A(:,:,n)))~=0
     A(:,:,n+1)= imerode(A(:,:,n),B);
     U(:,:,n)= (A(:,:,n)-A(:,:,n+1))*n;
     M=M+U(:,:,n);
     n=n+1;
end
n=n-1;
subplot(2,2,2);imagesc(M,[0,n]);title('距离图');

% 搜寻局部最大值,将其放入Deal_Image
Deal_Image=zeros(X,Y);
while n>0
    for high=1:X
      for width=1:Y
%********************************************************************
          Mark_Bool=0;
          if M(high,width)==n 
%______________________________________________________________  
            for dot=1:maskLenth
                 i=E8(dot,1); j=E8(dot,2);
                 if high+i>=1 & width+j>=1 & high+i<=X & width+j<=Y & M(high+i,width+j)>M(high,width); 
                      Mark_Bool=1;break;
                 end % if_end
              end % for dot_end
%______________________________________________________________
             if   Mark_Bool==0; 
                 Deal_Image(high,width)=M(high,width); 
             end %if end
%______________________________________________________________
        end %if end
%********************************************************************
      end %for-end
     end %for-end 
    n=n-1;
end % while n=0 end
Deal_Image =[Deal_Image>=1]
subplot(2,2,3);subimage(Deal_Image);title('输出图像');

Mark_Number=1;

while n>0
   for high=1:X
     for width=1:Y
       Mark_Bool=0;
     %********************************************************************
       if M(high,width)==n 
          %______________________________________________________________  
            for dot=1:maskLenth
                i=E8(dot,1); j=E8(dot,2);
                if high+i>=1 & width+j>=1 & high+i<=X & width+j<=Y & Mark_Image(high+i,width+j)>0; 
                     Mark_Image(high,width)=Mark_Image(high+i,width+j);
                     Mark_Bool=1;break;
                end % if_end
             end % for dot_end
           %______________________________________________________________
            if   Mark_Bool==0; 
                Mark_Image(high,width)=Mark_Number; 
                Mark_Number=Mark_Number+1;
             end %if end
           %______________________________________________________________
            pause;
            subplot(2,2,2);imagesc(Mark_Image,[0,Mark_Number]);title('输出图像');
        end %if end
     %********************************************************************
     end %for-end
    end %for-end 
    n=n-1;
end % while n=0 end


subplot(2,2,3);imagesc(Mark_Image,[0,Mark_Number]);title('分割后的图像');

uicontrol('Style','edit','string',['分割出区域:',Num2str(Mark_Number-1),'个'],...
            'Position', [400 0 150 18],'FontSize',12,'FontWeight','light');