求经典MATLAB分水岭算法源代码

2024-12-03 07:47:42
推荐回答(2个)
回答1:

%%都不知道你是啥样慎戚则的分割 什么图片
clear, close all;
clc;
PathName='d:\';%t为自填内容,下面p类似
FileName=[PathName 'test.jpg'];
Image=imread(FileName);
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');

回答2:

你找到了没有,找到了的话法我一份吧,我也正想做这方面,多谢