字母识别

  1. ---------------------测试图像预处理及连通区域提取
  2. ---------------------样本库的建立采集feature
  3. ---------------------选择算法输入测试图像进行测试
  4. ---------------------总结

字母识别

1.imgPreProcess(联通区域提取)目录下

conn.m:连通区域提取分割(在原图的基础上进行了膨胀、腐蚀、膨胀的操作使截取的图像更加接近字母)

%%提取数字的边界,生成新的图

clear;

clc;

f=imread('5.jpg');

f=imadjust(f,[0 1],[1 0]);

SE=strel('square',5);  %%膨胀、腐蚀、膨胀

A2=imdilate(f,SE);

SE=strel('disk',3)

f=imerode(A2,SE)

SE=strel('square',3);

f=imdilate(f,SE);

gray_level=graythresh(f);

f=im2bw(f,gray_level);

[l,n]=bwlabel(f,8)  %%8连接的连接分量标注

imshow(f)

hold on 

for k=1:n           %%分割字符子句

    [r,c]=find(l==k);

    rbar=mean(r);

    cbar=mean(c);

    plot(cbar,rbar,'Marker','o','MarkerEdgeColor','g','MarkerFaceColor','y','MarkerSize',10);

%     plot(cbar,rbar,'Marker','*','MarkerEdgecolor','w');

    row=max(r)-min(r)  

    col=max(c)-min(c)

    for i=1:row

        for j=1:col

            seg(i,j)=1;

        end

    end

    con=[r-min(r)+1,c-min(c)+1];

    [a,b]=size(con);

    for i=1:a

        seg(con(i,1),con(i,2))=0;

    end

    imwrite(seg,strcat('seg',int2str(k),'.bmp'));

    %seg=zeros(size(seg));

    clear seg;

end

截取后的图像

  1.  digitalRec目录下进行样本库的的建立并采集feature

在对截取出来的图像进行识别之前要先输入样本并提取特征进入templet.mat:

我自己通过WORLD打出字母后截屏下来获得字母样本并各自命名  .jpg放入digitalRec目录下用以建立样本库获取特征。

对每个字母都进行下面代码的的执行得到新的1x14的pattern得到各个字母(前9个为数字1~9)每个cell内的feature信息,因为样本有限在此每个字母只提取一次feature。

运行digRec01.m

clear all;clc;

load templet;

% A 被分成5*5=25个cell

%注意A的size(长和宽都需被定义成5的倍数,因为后面要被5除)

A=imread('a.jpg');

A=imresize(A,[25 25])   %%将输入图像转化为25*25的尺寸

figure(1),imshow(A)

B=zeros(1,25);           %%初始化一个B用于存放25个cell的feature

[row col] = size(A);    %%得到图像的长宽

cellRow = row/5

cellCol = col/5

count = 0;

currentCell = 1;

for currentRow = 0:4    %%遍历25个cell获取feature

    for currentCol = 0:4

       for i = 1:cellRow

           for j = 1:cellCol

               if(A(currentRow*cellRow+i,currentCol*cellCol+j)==0)

                   count=count+1;

               end

           end

        end

        ratio = count/(cellRow*cellCol);

        B(1,currentCell) = ratio;    %%将feature信息存入B中

        currentCell = currentCell+1;

        count = 0;

     end

end

pattern(11).num=1;                     %%每个字母只有一个样本

pattern(11).feature(:,1)=B';         %%B置入pattern

save templet pattern

pattern、pattern.feature:

       

3.digitalRec目录下进行图像测试

在将截取下来的图像放入 digitalRec目录下 并改名为  .bmp作为测试输入图像

由于用到imshow([num2str(class),'.jpg'])测试后只得到“编号.jpg”所以根据样本库中的字母对应的数字编号新建图像命名为10~14.jpg,用以输出识别出的图像。

选取基于最小距离的数字识别neartemplet.m

function y=neartemplet(sample);

clc;

load templet pattern;

d=0;

min=[inf,0];

for i=1:14               %%遍历14个样本进行feature的测试

    for j=1:pattern(i).num

        d=sqrt(sum((pattern(i).feature(:,j)-sample).^2));

        if min(1)>d

            min(1)=d;

            min(2)=i;

        end

        min

    end

end

y=min(2);

运行neartempletTest.m 输入测试图像调用neartemplet函数

先将测试图像调整为25*25的图像,然后同建立样本库一样的方法取得测试图像的25个cell的feature信息用于与样本库的feature作测试

A=imread('a.bmp');          %待测样本

A=imresize(A,[25 25])       %%调整大小

figure(1),subplot(121),imshow(A),title(['待识别的数:']);

B=zeros(1,25);

[row col] = size(A);        %%获取测试图像特征 

cellRow = row/5

cellCol = col/5

count = 0;

currentCell = 1;

for currentRow = 0:4

    for currentCol = 0:4

       for i = 1:cellRow

           for j = 1:cellCol

               if(A(currentRow*cellRow+i,currentCol*cellCol+j)==0)

                   count=count+1;

               end

           end

        end

        ratio = count/(cellRow*cellCol);

        B(1,currentCell) = ratio;

        currentCell = currentCell+1;

        count = 0;

     end

end

class = neartemplet(B');   %%调用基于最小距离算法的测试函数得到class为测试结

                                %%果对应样本库的编号

subplot(122),imshow([num2str(class),'.jpg']),title(['该数字被识别为:']);                           %%num2str(class)将编号转化为字符串型

分别输入测试图像得到测试结果:a、h 、e能通过neartemplet识别出来r、t识别不出来

bayesBinary.m算法可以识别r、h

t这两种算都没有识别出来应该是样本库里每个字母输入的样本只有一个造成的。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐