%读取文件
[input1, input2, input3] = textread('Char_Index.txt','%d %d %s',1000, 'headerlines',1);
indexFileName = input3;
for k=1:1000
A=imread(strcat('Char_Image/',char(indexFileName(k,1))));
t=graythresh(A);%设置阈值
B=im2bw(A,t);%将灰度图转化为二值图
%以图像左上角为原点,向下为x轴,向右为y轴,四个角的值大于等于2,即有2及以上为白点的,进行反转
[a,b]=size(B);
if(B(1,1)+B(1,b)+B(a,1)+B(a,b)>=2)
for i=1:a
for j=1:b
B(i,j)=1-B(i,j);
end
end
end
imwrite(B,strcat('Char_Image_Binary/',char(indexFileName(k,1))));
end
[input] = textread('Char_Index_Err.txt','%s',14);
indexFileName = input;
for k=1:14
A=imread(strcat('Char_Image_Binary/',char(indexFileName(k,1))));
t=graythresh(A);
B=im2bw(A,t);
[a,b]=size(B);
for i=1:a
for j=1:b
B(i,j)=1-B(i,j);
end
end
imwrite(B,strcat('Char_Image_Binary/',char(indexFileName(k,1))));
end
2.特征提取
2.1 每一行和每一列的白点数
读取1000张图片,并将每一行和每一列的白点计数存于featrue1.txt中,代码如下:
[input1, input2, input3] = textread('Char_Index_kuochong.txt','%d %d %s',1000, 'headerlines',1);
indexFileName = input3;
fid=fopen('feature1.txt','w+');
for k=1:1000
A=imread(strcat('Char_Image_Binary/',char(indexFileName(k,1))));
t=graythresh(A);
B=im2bw(A,t);
[a,b]=size(B);
C=zeros(1,a+b);
for i=1:a
for j=1:b
if(B(i,j)==1)
C(1,i)=C(1,i)+1;
end
end
end
for j=1:b
for i=1:a
if(B(i,j)==1)
C(1,a+j)=C(1,a+j)+1;
end
end
end
end
fclose(fid);
[input1, input2, input3] = textread('Char_Index.txt','%d %d %s',1000, 'headerlines',1);
indexFileName = input3;
fid=fopen('feature2.txt','w+');
for k=1:1000
A=imread(strcat('Char_Image_Binary/',char(indexFileName(k,1))));
t=graythresh(A);
B=im2bw(A,t);
[a,b]=size(B);
C=zeros(1,6*12);
l=1;
for i=1:8:a
for j=1:8:b
for m=i:min(i+7,a)
for n=j:min(j+7,b)
if(B(m,n)==1)
C(1,l)=C(1,l)+1;
end
end
end
l=l+1;
end
end
end
fclose(fid);
[input1, input2, input3] = textread('Char_Index.txt','%d %d %s',1000, 'headerlines',1);
indexFileName = input3;
fid=fopen('feature3.txt','w+');
for k=1:1000
A=imread(strcat('Char_Image_Binary/',char(indexFileName(k,1))));
t=graythresh(A);
B=im2bw(A,t);
[a,b]=size(B);
C=zeros(1,a*2+b*2);
for i=1:a
for j=1:b
if(B(i,j)==1)
C(1,i)=j-1;
break;
end
if(j==b && B(i,j)==0)
C(1,i)=b;
end
end
end
for i=1:a
for j=b:-1:1
if(B(i,j)==1)
C(1,a+i)=b-j;
break;
end
if(j==1 && B(i,j)==0)
C(1,a+i)=b;
end
end
end
for j=1:b
for i=1:a
if(B(i,j)==1)
C(1,a*2+j)=i-1;
break;
end
if(i==a && B(i,j)==0)
C(1,a*2+j)=a;
end
end
end
for j=1:b
for i=a:-1:1
if(B(i,j)==1)
C(1,a*2+b+j)=a-i;
break;
end
if(i==1 && B(i,j)==0)
C(1,a*2+b+j)=a;
end
end
end
end
fclose(fid);
[input1, input2, input3] = textread('Char_Index.txt','%d %d %s',1000, 'headerlines',1);
indexFileName = input3;
fid=fopen('feature4.txt','w+');
for k=1:1000
A=imread(strcat('Char_Image_Binary/',char(indexFileName(k,1))));
t=graythresh(A);
B=im2bw(A,t);
[a,b]=size(B);
C=zeros(1,a+b);
for i=1:a
for j=1:b-1
if(B(i,j)~=B(i,j+1) && B(i,j+1)==1)
C(1,i)=C(1,i)+1;
end
end
end
for j=1:b
for i=1:a-1
if(B(i,j)~=B(i+1,j) && B(i+1,j)==1)
C(1,a+j)=C(1,a+j)+1;
end
end
end
end
fclose(fid);
请发表评论