map1=imread('p.jpg');
[row,col,dep]=size(map1); %行,列,深度值
map=zeros(row,col);
pixsum=row*col; %像素总数
%灰度化
for i=1:row
for j=1:col
map(i,j)=0.11*map1(i,j,1)+0.59*map1(i,j,2)+0.3*map1(i,j,3);
end
end
%灰度化完成,输出灰度图
figure(1)
imshow(map,[])
title('原始灰度图像')
Y=zeros(row,col); %存储输出图像
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5根据通道二值化
for i=1:row
for j=1:col
if map1(i,j,2)>map1(i,j,1)&&map1(i,j,2)>map1(i,j,3)
Y(i,j)=1;
else
Y(i,j)=0;
end
end
end
figure(2)
imshow(Y,[])
title('二值化后图像')
%骨架提取,以前的思路是进行若干次腐蚀,直到腐蚀为空集的前一次,但效果不好。
%现在改成细化法。
skel=zeros(row,col);
%细化索引表
tab=[0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,...
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,...
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,...
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,...
1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,1,...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,...
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,...
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,...
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,0,...
1,1,0,0,1,1,1,0,1,1,0,0,1,0,0,0];
B=[1 2 4;8 0 16;32 64 128];
for i=2:row-1
for j=2:col-1
N=sum(sum(Y(i-1:i+1,j-1:j+1).*B));
skel(i,j)=tab(N+1);
end
end
%腐蚀完成
figure(3)
imshow(skel,[])
title('提取的图像骨架')