Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

OCR licence plate detection using matlab

Status
Not open for further replies.

magvitron

Active Member
function for determining the band of letters
Code:
[CODE]function output = recognitionLetter (inputCropImg)
image = rgb2gray((inputCropImg));
subplot(1,2,1) | imshow(image);
%figure = imshow(image);
%image=imread(image);
%url = 'http://10.10.1.2:8080/shot.jpg';
%ss  = imread(url);

%fh = ss(ss);
%while(1)
%ss  = imread(url);
% set(fh,'CData',ss);
%drawnow;
%end

%impimg=imresize(image,[300 600]);%------resize the image to 600x300-------
%grimg=rgb2gray(impimg);
gradj=imadjust(image);
gradj2=medfilt2(gradj,[2 2]);
ejdet=edge(gradj2,'sobel',[],'vertical');
%imshow(ejdet);
[imy,imx]=size(gradj2);
%disp(imx);
%disp(imy);
%imshow(gradj2);
%imx=width/number of columns
%imy=height/number of rows
mn=[10 20];
se=strel('rectangle',mn);
ejdet=imdilate(ejdet,se);
vert=mean(ejdet,1);     %mean of columns gives a row with values of all columns
horz=mean(ejdet,2);     %mean of rows gives a column with values of all rows
h = fspecial('average', 20);
g = fspecial('average', 200);
horz=filter2(g,horz);
vert=filter2(g,vert);
%subplot(1,2,1)|plot(horz);subplot(1,2,2)|plot(vert);
for i=1:imy
    if(horz(i)<0.000050)
        horz(i)=0;
        %  else
        %      horz(i)=1;
    end
end
for i=1:imx
    if(vert(i)<0.000100)
        vert(i)=0;
    end
end

mask=zeros(imy,imx);
for i=1:imy
    if(horz(i)~=0)
        mask(i,:)=1;
    end
end
mask2=zeros(imy,imx);
for j=1:imx
    if(vert(j)~=0)
        mask2(:,j)=1;
    end
end

for i=1:imy
    if(horz(i)~=0)
        gradj2(~mask)=0;
    end
end

for i=1:imx
    if(vert(i)~=0)
        gradj2(~mask2)=0;
    end
end
bwimg=im2bw(gradj2);
bwn=imfill(bwimg,[1 1],8);      %fills the background
bwn=~bwn;%complements the image   if(vert(i)~=0)


bwop=bwareaopen(bwn,500);   %remove small objects less than 500 pixels
%se=strel('disk',5);
%bwer=imerode(bwn,se);
subplot(1,2,2) | imshow(bwop);
%----------------segmentation starts-----------
stats = regionprops(bwop);          %computes area boundingbox and centroid of the image according to the area of regions
len=size(stats,1);
index1 = 1;
for index=1:length(stats)
    if stats(index).Area > 400 && stats(index).BoundingBox(3)*stats(index).BoundingBox(4) < 30000;   %checks the image to segment
        
        x=ceil(stats(index).BoundingBox(1));
        y=ceil(stats(index).BoundingBox(2));
        widthX=floor(stats(index).BoundingBox(3)-1);
        widthY=floor(stats(index).BoundingBox(4)-1);
        subimage(index1)={bwop(y:y+widthY,x:x+widthX)};
        a(index1)=subimage(index1);
        index1 = index1+1;
        %figure,imshow(a{index});
    else
         a(index1)=subimage(index1);
    end
end

%********************OCR(Optical Character Recognition)*****************
len=size(a,2);
%len =10;
load templates;
global templates;

% disp(size(templates));
num_of_leters=35;

% disp(num_of_leters);
%Opens text.txt as file for write
fid = fopen('text1.txt', 'a');
word=[ ];
comp=[ ];
for i=1:len
    img_r=imresize((a{i}),[42 24]);
    letters=read_letter(img_r,num_of_leters);    %convert the image to text
    word=[word letters];    %concatenate the letters
end

output = word;
fprintf(fid,'\r\n %s \n\r',word);   %Write 'word' in text file (upper)
disp(text.txt);  % print the most prominent character in the text file
%close the file
fclose(fid);
%Open 'text.txt' file
% winopen('text.txt');
Code:
read letter
function letter=read_letter(img_r,num_of_leters)
% Computes the correlation between template and input image
% and its output is a string containing the letter.
% Size of 'imagn' must be 42 x 24 pixels
% Example:
% imagn=imread('D.bmp');
% letter=read_letter(imagn)
global templates
comp=[ ];
%img_r=imread('/letters_numbers/0.bmp');
%num_of_leters=size(templates,2);
for n=1:num_of_leters
    sem=corr2(img_r,templates{1,n});
    comp=[comp sem];
end
%compmax=max(comp);
%for i=1:num_of_leters
%	if(comp(i)==compmax)
%		vd=i;
%	end
%end
% disp(comp);
vd=find(comp==max(comp));
%*-*-*-*-*-*-*-*-*-*-*-*-*-
if vd==27
    letter='1';
elseif vd==28
    letter='2';
elseif vd==29
    letter='3';
elseif vd==30
    letter='4';
elseif vd==31
    letter='5';
elseif vd==32
    letter='6';
elseif vd==33
    letter='7';
elseif vd==34
    letter='8';
elseif vd==35
    letter='9';
elseif vd==36
    letter='0';
elseif vd==1
    letter='A';
elseif vd==2
    letter='B';
elseif vd==3
    letter='C';
elseif vd==4
    letter='D';
elseif vd==5
    letter='E';
elseif vd==6
    letter='F';
elseif vd==7
    letter='G';
elseif vd==8
    letter='H';
elseif vd==9
    letter='I';
elseif vd==10
    letter='J';
elseif vd==11
    letter='K';
elseif vd==12
    letter='L';
elseif vd==13
    letter='M';
elseif vd==14
    letter='N';
elseif vd==15
    letter='O';
elseif vd==16
    letter='P';
elseif vd==17
    letter='Q';
elseif vd==18
    letter='R';
elseif vd==19
    letter='S';
elseif vd==20
    letter='T';
elseif vd==21
    letter='U';
elseif vd==22
    letter='V';
elseif vd==23
    letter='W';
elseif vd==24
    letter='X';
elseif vd==25
    letter='Y';
elseif vd==26
    letter='Z';
else
    letter='err';
    %*-*-*-*-*
end
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top