英文字符识别案例

时间:2024-04-02 08:27:25

  以前处理过相似问题,对英文文章单词做标注,然后通过二值化处理可以对字符位置定位。这个案例结合图像识别理论,进行英文定位和识别的开发。附上效果图:英文字符识别案例

function MainForm
global bw;
global bl;
global bll;
global s;
global fontSize;
global charpic;
global hMainFig;
global pic;
global hText;

%判断文件夹pic是否存在,不存在则创建
clc; close all; warning off all;
if ~exist(fullfile(pwd, 'pic'), 'dir')
    mkdir(fullfile(pwd, 'pic'));
end

%读取图片并灰度化、二值化
picname = fullfile(pwd, 'image.jpg');
pic = imread(picname);
s = size(pic);
if length(s) == 3
    pic = rgb2gray(pic);
end
bw = im2bw(pic, 0.7);
bw = ~bw;

%获取字符高度
for i = 1 : s(1)
    if sum(bw(i,:) ~=0) > 0
        FontSize_s = i;
        break;
    end
end
for i = FontSize_s : s(1)
    if sum(bw(i,:) ~=0) == 0
        FontSize_e = i;
        break;
    end
end
FontSizeT = FontSize_e - FontSize_s;
fontName = '宋体';
fontSize = FontSizeT;


%对字符进行形态学处理
bw1 = imclose(bw, strel('line', 4, 90));

%bwareaopen:对区域小于20的进行删除,目的是去除标点符号
bw2 = bwareaopen(bw1, 20);

%bwselect:该函数是进行二进制图像处理的一个函数,在数学形态学处理中很常用,其作用是在二进制图像中选择指定坐标点所在的对象。其中坐标点的值存在r,c两个具有相等长度的矢量中
bwi2 = bwselect(bw2, 368, 483, 4);
bw2(bwi2) = 0;
bw3 = bw .* bw2;
bw4 = imclose(bw3, strel('square', 4));

%区域分割
[Lbw4, numbw4] = bwlabel(bw4);
stats = regionprops(Lbw4);
for i = 1 : numbw4

    %字符定位
    tempBound = stats(i).BoundingBox;

    %字符裁剪
    tempPic = imcrop(pic, tempBound);

    %保存字符到指定文件夹
    tempStr = fullfile(pwd, sprintf('pic\\%03d.jpg', i));
    imwrite(tempPic, tempStr);
end

[bl, num] = bwlabel(bw1, 4);


%创建字符库,用于模式识别
chars = [char(uint8('A'):uint8('Z')), uint8('a'):uint8('z'), uint8('0'):uint8('9')];
eleLen = length(chars);

%cell:creates a cell array of empty matrices
charpic = cell(1,eleLen);
hf1 = figure('Visible', 'Off');imshow(zeros(32,32));

h = text(15, 15, 'a', 'Color', 'w', 'Fontname', fontName, 'FontSize', fontSize);
%遍历字符库,获取帧图像并进行处理

for p = 1 : eleLen
    set(h, 'String', chars(p));
    fh = getframe(hf1, [85, 58, 30, 30]);
    temp = fh.cdata;
    temp = im2bw(temp, graythresh(temp));
    [f1, f2] = find(temp == 1);
    temp = temp(min(f1)-1:max(f1)+1,min(f2)-1:max(f2)+1);
    charpic{p} = temp;
end

%字符识别
delete(hf1);
bll = zeros(size(bl));
for i = 1:num
    [f1, f2] = find(bl == i);
    bll(min(f1):max(f1), min(f2):max(f2)) = i;
end
hMainFig = figure(1);
imshow(picname, 'Border', 'loose'); hold on;
for i = 1 : numbw4

    tempBound = stats(i).BoundingBox;

    %字符定位
    rectangle('Position', tempBound, 'EdgeColor', 'r');
end
hText = axes('Units', 'Normalized', 'Position', [0 0 0.1 0.1]); axis off;
%绑定鼠标函数WindowButtonMotionFcn

set(hMainFig, 'WindowButtonMotionFcn', @ShowPointData);
end


function ShowPointData(hObject, eventdata, handles)

global bw;
global bl;
global bll;
global s;
global charpic;
global hMainFig;
global pic;
global hText;

%获取鼠标当前位置坐标
p = get(gca,'currentpoint');
x = p(3);
y = p(1);
if x<1 || x>s(1) || y<1 || y>s(2)
    return;
end
curlabel = bll(uint32(x), uint32(y));
if curlabel ~= 0
    [f1, f2] = find(bl == curlabel);
    minx = min(f1);
    maxx = max(f1);
    miny = min(f2);
    maxy = max(f2);
    tempic = pic(minx:maxx, miny:maxy);
    temp = bw(minx:maxx, miny:maxy);
    tempIm = zeros(round(size(temp)*2)); tempIm = logical(tempIm);
    tempIm(round((size(tempIm, 1)-size(temp, 1))/2):round((size(tempIm, 1)-size(temp, 1))/2)+size(temp, 1)-1, ...
        round((size(tempIm, 2)-size(temp, 2))/2):round((size(tempIm, 2)-size(temp, 2))/2)+size(temp, 2)-1) = temp;
    set(0, 'CurrentFigure', hMainFig);
    imshow(tempIm, [], 'Parent', hText);
    mincost = 100000;
    mark = 1;
    for i = 1 : length(charpic)
        temp1 = charpic{i};
        ss = size(temp);

        %规则化
        temp1 = imresize(temp1, ss);

        %欧氏距离衡量相似性
        tempcost = sum(sum(abs(temp - temp1)));
        if tempcost < mincost
            mincost = tempcost;
            mark = i;
        end
    end
end
end


 字符识别的流程:

1.字符分割

2.创建字符库

3.绑定鼠标函数

4.利用欧氏距离进行字符识别