实验一:骨架抽取

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 )
                grad1 = (edgedist(y,x)-edgedist(y-1,x-1))/sqrt(2);
                grad2 = (edgedist(y,x)-edgedist(y-1,x));
                grad3 = (edgedist(y,x)-edgedist(y-1,x+1))/sqrt(2);
                grad4 = (edgedist(y,x)-edgedist(y,x+1));
                grad5 = (edgedist(y,x)-edgedist(y+1,x+1))/sqrt(2);
                grad6 = (edgedist(y,x)-edgedist(y+1,x));
                grad7 = (edgedist(y,x)-edgedist(y+1,x-1))/sqrt(2);
                grad8 = (edgedist(y,x)-edgedist(y,x-1));
                
                if(ske(y-1,x-1)==0 && pic(y-1,x-1)==1)
                    if( grad1>=0 && grad1<=grad2 && grad1<=grad8 && grad1<=grad3 && grad1<=grad7) ske(y-1,x-1)=3; change = 1; end
                end
                if(ske(y-1,x)==0 && pic(y-1,x)==1)
                    if( grad2>=0 && grad2<=grad1 && grad2<=grad3&& grad2<=grad8 && grad2<=grad4) ske(y-1,x)=3; change = 1;  end
                end
                if(ske(y-1,x+1)==0 && pic(y-1,x+1)==1)
                    if( grad3>=0 && grad3<=grad2 && grad3<=grad4&& grad3<=grad1 && grad3<=grad5) ske(y-1,x+1)=3; change = 1; end
                end
                if(ske(y,x+1)==0 && pic(y,x+1)==1)
                    if( grad4>=0 && grad4<=grad3 && grad4<=grad5&& grad4<=grad2 && grad4<=grad6) ske(y,x+1)=3; change = 1; end
                end
                if(ske(y+1,x+1)==0 && pic(y+1,x+1)==1)
                    if( grad5>=0 && grad5<=grad4 && grad5<=grad6&& grad5<=grad3 && grad5<=grad7) ske(y+1,x+1)=3; change = 1; end
                end
                if(ske(y+1,x)==0 && pic(y+1,x)==1)
                    if( grad6>=0 && grad6<=grad5 && grad6<=grad7&& grad6<=grad4 && grad6<=grad8) ske(y+1,x)=3; change = 1; end
                end
                if(ske(y+1,x-1)==0 && pic(y+1,x-1)==1)
                    if( grad7>=0 && grad7<=grad6 && grad7<=grad8&& grad7<=grad5 && grad7<=grad1) ske(y+1,x-1)=3; change = 1; end
                end
                if(ske(y,x-1)==0 && pic(y,x-1)==1)
                    if( grad8>=0 && grad8<=grad7 && grad8<=grad1&& grad8<=grad6 && grad8<=grad2) ske(y,x-1)=3; change = 1; end
                end
            end
        end
    end
    ske(ske==2)=1;
    ske(ske==3)=2;
end
figure;
imshow(ske);

The end