# 实验一：骨架抽取

Modified: 2014/03/10 16:56 by admin - Uncategorized
```% 生成实验用图像：矩形
rec = zeros(100,100);
for x=30:70
for y=40:60
rec(y,x)=1;
end
end

% 生成实验用图像：三角形
triangle = zeros(100,100);
for x=30:70
for y=30:x
triangle(y,x)=1;
end
end

% 选择要使用的图像
pic = rec;
imshow(pic);

% 计算距离图像
edgedist = pic.*10000;
change = 1;
while change==1
change = 0;
for x=2:99
for y=2:99
if( pic(y,x)==1 )

neighbors = [edgedist(y-1,x-1),edgedist(y-1,x),edgedist(y-1,x+1),edgedist(y,x-1),...
edgedist(y,x+1),edgedist(y+1,x-1),edgedist(y+1,x),edgedist(y+1,x+1)];

min_value = min(neighbors)+1;
if( min_value ~= edgedist(y,x) )
change=1;
end

edgedist(y,x)=min_value;

end
end
end
end
figure;
imshow(edgedist./max(max(edgedist)));

% 计算骨架图像
ske = zeros(100,100);
ske( edgedist==max(max(edgedist)) ) = 2;
change = 1;
while change==1
change = 0;
for x=2:99
for y=2:99
if( ske(y,x)==2 )

if(ske(y-1,x-1)==0 && pic(y-1,x-1)==1)
end
if(ske(y-1,x)==0 && pic(y-1,x)==1)
end
if(ske(y-1,x+1)==0 && pic(y-1,x+1)==1)
end
if(ske(y,x+1)==0 && pic(y,x+1)==1)
end
if(ske(y+1,x+1)==0 && pic(y+1,x+1)==1)
end
if(ske(y+1,x)==0 && pic(y+1,x)==1)
end
if(ske(y+1,x-1)==0 && pic(y+1,x-1)==1)
end
if(ske(y,x-1)==0 && pic(y,x-1)==1)