原理见我的博文中的c版本:
clc
clear all
A=imread('D:\test.bmp');
figure(1)
imshow(A)
[w,l]=size(A);
B=zeros(w,l);
%%%%%%%%%对图形进行预处理包括255处理为1,还有加上全1的边框
for i=1:w
for
j=1:l
if(A(i,j)==255)
B(i,j)=1;
else
B(i,j)=0;
end
end
end
mark=zeros(w,l);
temp=0;
for i=2:w-1
for
j=2:l-1
%%step1: weather p is 0,that is ,p is not background
if(B(i,j)~=0)
continue;
end
%
B(i,j)
%
B(i,j+1)
%
B(i+1,j+1)
%
B(i-1,j)
%
B(i-1,j-1)
%
B(i-1,j)
%
B(i+1,j-1)
%
B(i+1,j)
%
B(i+1,j+1)
%%step2:X1,X3,X5,X7 can't be all 0
if(B(i,j+1)==0&B(i-1,j)==0&B(i,j-1)==0&B(i+1,j)==0)
continue;
end
%%step3:X1 to X8 ,at least two 0
count=0;
count=count+B(i,j+1)+B(i-1,j+1)+B(i-1,j)+B(i-1,j-1)+B(i,j-1)+B(i+1,j-1)+B(i+1,j)+B(i+1,j+1);
if count>6
continue;
end
%%step4:the connect's number of p is 1