英文字符識別案例

  以前處理過相似問題,對英文文章單詞做標註,然後通過二值化處理可以對字符位置定位。這個案例結合圖像識別理論,進行英文定位和識別的開發。附上效果圖:

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.利用歐氏距離進行字符識別


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章