%Thinning clc clear all; I=rgb2gray(imread('imgImageProcess_blk2.jpg')); % I=rgb2gray(imread('imghorse.png')); subplot(121),imshow(I);title('Original'); Ibw=im2bw(I); %binaryzation subplot(222),imshow(Ibw);title('binaryzation'); [row,col]=size(I); % for i=1:row % for j=1:col % % if I(i,j)>100 % Ibw(i,j)=1; % else % Ibw(i,j)=0; % end % % end % end % subplot(122),imshow(Ibw);title('Original'); for i=3:row-2 for j=3:col-2 p0=Ibw(i,j); p1=Ibw(i,j+1); p2=Ibw(i-1,j+1); p3=Ibw(i-1,j); p4=Ibw(i-1,j-1); p5=Ibw(i,j-1); p6=Ibw(i+1,j-1); p7=Ibw(i+1,j); p8=Ibw(i+1,j+1); p9=Ibw(i+1,j+2); p10=Ibw(i,j+2); p11=Ibw(i-1,j+2); p12=Ibw(i-2,j+2); p13=Ibw(i-2,j+1); p14=Ibw(i-2,j); p15=Ibw(i-2,j-1); p16=Ibw(i-2,j-2); p17=Ibw(i-1,j-2); p18=Ibw(i,j-2); p19=Ibw(i+1,j-2); p20=Ibw(i+2,j-2); p21=Ibw(i+2,j-1); p22=Ibw(i+2,j); p23=Ibw(i+2,j+1); p24=Ibw(i+2,j+2); Np0=p1+p2+p3+p4+p5+p6+p7+p8; pCenter0=[p1,p2,p3,p4,p5,p6,p7,p8,p1]; pCenter5=[p0,p3,p4,p17,p18,p19,p6,p7,p0]; pCenter7=[p8,p1,p0,p5,p6,p21,p22,p23,p8]; for m=1:8 if (pCenter0(m)==0)&&(pCenter0(m+1)==1) TCenter0(m)=1; else TCenter0(m)=0; end if (pCenter5(m)==0)&&(pCenter5(m+1)==1) TCenter5(m)=1; else TCenter5(m)=0; end if (pCenter7(m)==0)&&(pCenter7(m+1)==1) TCenter7(m)=1; else TCenter7(m)=0; end end Sp0=sum(TCenter0); %求Sp0 Sp5=sum(TCenter5); %求Sp5 Sp7=sum(TCenter7); %求Sp7 if (2<=Np0&&Np0<=6)&& (Sp0==1)&&(p1*p3*p7==0||Sp5~=1)&&(p3*p5*p7==0||Sp7~=1) p0=0; Ibw(i,j)=0; end end end subplot(122),imshow(~Ibw);title('Ibw');