% OCR (Optical Character Recognition).
% Author: Ing. Diego Barrag醤 Guerrero 
% e-mail: [email protected]
% For more information, visit: www.matpic.com
%________________________________________
% PRINCIPAL PROGRAM
warning off %#ok<WNOFF>
% Clear all
clc, close all, clear all
% Read image
imagen=imread('IMG_4367.JPG');
% Show image
subplot(3,2,1);
imshow(imagen);
title('原图');
title('INPUT IMAGE WITH NOISE')
% Convert to gray scale
if size(imagen,3)==3 %RGB image
    imagen=rgb2gray(imagen);
end
[r,c]=size(imagen);
% Convert to BW
threshold = graythresh(imagen);
%**********************************************
sum=0;
for i=1:r
    for j=1:c
        if imagen(i,j)>threshold
            sum=sum+1;
        else
            sum=sum-1;
        end
    end
end 
%一般来说,车牌中,背景所占的比例大于前景所占的比例
if sum>=0  %sum大于0.说明背景的像素值偏大
    imagen=im2bw(imagen,threshold);%将图像二值化 [0 threshold]设为1,[threshold
255]设为0. 背景设为黑色,前景设为白色
else
    imagen =~im2bw(imagen,threshold); %将图像二值化  [0 threshold]设为0,[threshold
255]设为1.
end
% %图像形态学处理
SE=strel('arbitrary',[0 1 0;1 1 1;0 1 0]);
imagen=imerode(imagen,SE);
imagen=imdilate(imagen,SE);
%imagen =~im2bw(imagen,threshold);   %将图像二值化
%**************************************************
% Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,30);  %排除小于30像素的连通区域
subplot(3,2,2);
imshow(imagen);
title('二值化结果');

%Storage matrix word from image
word=[ ];
re=imagen;
%Opens text.txt as file for write
fid = fopen('text.txt', 'wt');
% Load templates
load templates
global templates
% Compute the number of letters in template file
num_letras=size(templates,2);
global num_letras;

%********************************************
%采用基于水平与垂直投影的字符分割方法
%计算垂直投影
clear sum;
vertical=sum(imagen,2);
subplot(3,2,3);
plot(vertical);
title('水平投影结果');

%根据垂直投影结果剪切图片
row=uint32(size(vertical,1)/8);
Y1=min(vertical(1:row,1));    
r1=find(vertical(1:row,1)==Y1);
Y2=min(vertical(size(vertical,1)-row:size(vertical,1),1));
r2=find(vertical(size(vertical,1)-row:size(vertical,1),1)==Y2);
imagen=imagen(max(r1):size(vertical,1)-row+min(r2)-1,:);
subplot(3,2,4);
imshow(imagen);
title('剪切得到的结果');

%计算水平投影
Horizon=sum(imagen); %Horizon 记录了imagen的列和
subplot(3,2,5);
plot(Horizon);
title('垂直投影结果');

space=(size(Horizon,2)-0)/7;
i=5;
[L Ne] = bwlabel(Horizon);
figure;
count=1;
global count;
for n=1:Ne                           %Ne 标识连通区域的个数
        [r,c] = find(L==n);
        % Extract letter
        n1=imagen(:,min(c):max(c));  
        if max(c)-min(c)>4*space/3
           letters=[];
           global letters;
           letters=Csegmentation(n1,min(c),max(c),Horizon,space);
           word=[word letters]
        else if max(c)-min(c)>space/6
           img_r=imresize(n1,[180 90]);
           subplot(5,2,count);  
           imshow(img_r);
           imwrite(img_r,strcat(num2str(count),'.bmp'));
           count=count+1;
          % img_r=imresize(n1,[42 24]);     %将分割出来的字符进行归一化
           % Call fcn to convert image to text
           letter=read_letter(img_r,num_letras);
           % Letter concatenation
           word=[word letter];
            end
        end
end

%fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)
% Clear 'word' variable
word=[ ]; 

fclose(fid);
%Open 'text.txt' file
winopen('text.txt')
clear all

仿真结果如下所示:

  

D180

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信