You are on page 1of 14

# Hi...

I am doing a project on the concept of AUTOMATIC NUMBER PLATE RECOGNITION (ANPR) using matlab using
artificial neural network for OCR(Optical Character Recognition). here we initially take an image of car number plate
or license plate and perform Image enhancement, Image Segmentation and Character Recognition process to
display the license plate characters as output of matlab code. I have executed half of the matlab code till dilation
process and have got output successfully.. now I have the entire code of the project but I am getting an error and I
am unable to remove it.. so can u please suggest ways to remove it or can u please correct that code... or can u
please help me in writing a new code regarding this project... or if u have any ideas.. can u please send me the
code...
%imshow(fi)
fin = rgb2gray(fi);
imshow(fin);
d=double(fin)
%imshow(fin)
[r c]= size(d)
% Mexican filter operator
filter = [ 0 0 0 -1 -1 -1 0 0 0 ;
0 -1 -1 -3 -3 -3 -1 -1 0;
0 -1 -3 -3 -1 -3 -3 -1 0;
-1 -3 -3 6 13 6 -3 -3 -1;
-1 -3 -1 13 24 13 -1 -3 -1;
-1 -3 -3 -6 13 6 -3 -3 -1;
0 -1 -3 -3 -1 -3 -3 -1 0;
0 -1 -1 -3 -3 -3 -1 -1 0;
0 0 0 -1 -1 -1 0 0 0 ];
% creating image matrix for mexican hat operator
gm = zeros(r,c);
for i=5:2:r-5
for j=5:2:c-5
gm(i,j) = sum(sum(double(fin(i-4:i+4,j-4:j+4)).*filter,2));
end;
end;
% removing the unwanted edges by using a threshold
fh = gm>1200;
%Dilation operation
x = 1;
y =1;
fs = double(fh);
se = ones(3,3);

for x= 3:3:r-20
for y = 3:3:c-20
if(x+50<=r)
xend = x+50;
else
xend = r;
end;
if(y+100<=r)
yend = y + 150;
else
yend = c;
end;
if(sum(fh(x:xend,y))<=35||sum (fh(x,y:yend,2)<=60))
if(sum(fh(x,y:y+3),2)<=3) &&

(sum(fh(x,y:y+3),2)>2)

fs(x-2:x+2,y-2:y+2)=bwmorph(fh(x-2:x+2,y-2:y+2),'dilate',se);
end;
end;
end;
end;
%imshow(fin)
%image with dilation performed
f=double(fs);
[row col]=size(f);
%initialising a matrix for a segmented image
g=zeros(row,col);
gl=zeros(row,col);
label=1;
n=1;
x=1;
iter=[];
it=0;
ss_prev=0;
nn=[];
sss_mat=[];
for i=1:2:row
for j=1:2:col
r_pt=i;

c_pt=j;
if(g(r_pt,c_pt)==0)
while(true)
|%using 4 neighbour rule|
if(f(r_pt(n),c_pt(n))==1 && g(r_pt(n),c_pt(n))==0)
g(r_pt(n),c_pt(n))=label;
if(r_pt(n)+1<=row)
if(f(r_pt(n)+1,c_pt(n))==1)
r_pt=[r_pt r_pt(n)+1];
c_pt=[c_pt c_pt(n)];
x=x+1;
end;
end;
if(c_pt(n)-1>=1)
if(f(r_pt(n),c_pt(n)-1)==1)
r_pt=[r_pt r_pt(n)];
c_pt=[c_pt c_pt(n)-1];
x=x+1;
end;
end;
if(c_pt(n)+1<=col)
if(f(r_pt(n),c_pt(n)+1)==1)
r_pt=[r_pt r_pt(n)];
c_pt=[c_pt c_pt(n)+1];
x=x+1;
end;
end;
if(r_pt(n)-1>=1)
if(f(r_pt(n)-1,c_pt(n))==1)
r_pt=[r_pt r_pt(n)-1];
c_pt=[c_pt c_pt(n)];
x=x+1;
end;
end;
end;
if(n>=x)
break;

end;
n=n+1;
end;
y1=min(r_pt);
y2=max(r_pt);
x1=min(c_pt);
x2=max(c_pt);
a1=g(min(r_pt):max(r_pt),min(c_pt):max(c_pt));
f1=d(min(r_pt):max(r_pt),min(c_pt):max(c_pt));
[ra ca]=size(a1);
|

if(n>=50)|
b1=bwlabel(a1);
ss=regionprops(b1,'euler number');
sss=struct2array(ss);
sss=min(sss);
sss_mat=[sss_mat sss];

if(sss<ss_prev && sss<0 && ca <=190 && ra<=60 && ca>=50 && ra >=15 &&
mean(mean(f1))<=220)
x_cor1=x1;
y_cor1=y1;
x_cor2=x2;
y_cor2=y2;
ss_prev=sss;
end;
label=label+1;
else
g(r_pt,c_pt)=0;
end;
end;
x=1;
n=1;
it=1;
end;
end;
if(exist('y_cor1')==1)
d(y_cor1:y_cor1+2,x_cor1:x_cor2)=255;
d(y_cor2:y_cor2+2,x_cor1:x_cor2)=255;

d(y_cor1:y_cor2,x_cor1:x_cor1+2)=255;
d(y_cor1:y_cor2,x_cor2:x_cor2+2)=255;
end;
% Segmented licence plate image
d=mat2gray(d);
|lp=d(y_cor1:y_cor2,x_cor1:x_cor2);|
%%% 2. Character Segmentation
%License plate image, characters of wcich are to be segmented
lp1 = d(y_cor1:y_cor2,x_cor1:x_cor2);
[rl cl] = size(lp1);
% Median Filtering
lp = medfilt2(lp1,[3 3]);
% Contrast Enhancement
%creating output image matrix
output= zeros(rl,cl);
% Window for local threshold operation
dis = round(cl/7);
% Local threshold operation
for i=1:dis:cl
if(i+dis-1<=cl)
t=threshcal(lpf(:,i:i+dis-1),a);
for i=1:dis:cl
if(i+dis-1<=cl)
t=threshcal(lpf(:,i:i+dis-1),a);
output(:,i:i+dis-1)=lpf(:,i:i+dis-1)<=t;
else
t=threshcal(lpf(:,i:cl),a);
for z1=2:rl-1
for z2=i+5:cl-5
if(mean(mean(lpf(z1-1:z1+1,z2-5:z2+5)))<=t)
output(z1,z2)=1;
end;
end;
end;
output(:,i:cl)=lpf(:,i:cl)<=t;
end;

end;
end;
end;
% Structuring element for erosion operation
se = [1 1 1

1 1 1 1 1 1 1 1 1

1 1 1]

## output = output - imerode(output,se);

[of lab lpdet] = reggrowl(logical(output),number);
% Segmented characters
lpdet = logical(lpdet);
% Character Recognition
% output String giving licence plate information
lpstr=[];
for i= 1:lab-1
R = lpdet(:,st:st+9);
st = st+10;
b = bwlabel(R);
% Feature extraction
ar = struct2array(regionprops(b,'area'));
or = struct2aarray(regionprops(b,'orientation'))/90;
eu = struct2array(regionprops(b,'eulernumber'))/10;
pe = struct2array(regionprops(b,'perimeter'));
mi = struct2array(regionprops(b,'minoraxislength'));
ma = struct2array(regionprops(b,'majoraxislength'));
temp = logical(R);
% Reflection X and Y coefficient determination
v1 = temp;
v1(:,6:10)=flipdim(temp(:,1:5),2);
vx = (v1 + temp)/2;
vx = vx>=0.5;
xcoef = sum(sum(temp),2)/sum(sum(vx),2);
v2 = temp;
v2(1:12,:) = flipdim(temp(13:24,:),1);
vy = (v2 + temp)/2;
vy = vy >= 0.5;
ycoef = sum(sum(temp),2)/sum(sum(vy),2);
ed = struct2array(regionprops(b,'equivdiameter'))/100;
[val pos] = max(fa);

vcoeff = pe(pos)/ar(pos);
mcoeff = ed(pos);
Rp = [xcoef/ycoef;pe(pos)/ar(pos);mi(pos)/ma(pos)];
if(i<=numel(lpnum))
numrc = numrc+1;
else
answ = find(alphamat==lpnum(i));
err(answ) = err(answ) + 1;
end;
end;
end;
numc = numc + numel(lpnum);
if(strcmp(lpstr,lpnum)==1)
tr = tr + 1;
sr = strcat(num2str(num),'/',num2str(1),'//');
casep = [casep sr];
else
fr = fr +1;
casen = [casen sr];
end;

Olthohoyh
Answer by mahjoub el attar on 4 Apr 2012

%imshow(fi)
fin = rgb2gray(fi);
imshow(fin);
d=double(fin)
%imshow(fin)
[r c]= size(d)
% Mexican filter operator
filter = [ 0 0 0 -1 -1 -1 0 0 0 ; 0 -1 -1 -3 -3 -3 -1 -1 0; 0 -1 -3 -3 -1 -3 -3 -1 0;
-1 -3 -3 6 13 6 -3 -3 -1; -1 -3 -1 13 24 13 -1 -3 -1; -1 -3 -3 -6 13 6 -3 -3 -1; 0 -1
-3 -3 -1 -3 -3 -1 0; 0 -1 -1 -3 -3 -3 -1 -1 0; 0 0 0 -1 -1 -1 0 0 0 ];
% creating image matrix for mexican hat operator
gm = zeros(r,c);
for i=5:2:r-5
for j=5:2:c-5
gm(i,j) = sum(sum(double(fin(i-4:i+4,j-4:j+4)).*filter,2));
end;
end;
% removing the unwanted edges by using a threshold
fh = gm>1200;
%Dilation operation
x = 1;
y =1;
fs = double(fh);
se = ones(3,3);
for x= 3:3:r-20
for y = 3:3:c-20
if(x+50<=r)
xend = x+50;
else
xend = r;
end;
if(y+100<=r)
yend = y + 150;
else

yend = c;
end;
if(sum(fh(x:xend,y))<=35||sum (fh(x,y:yend,2)<=60))
if(sum(fh(x,y:y+3),2)<=3) &&

(sum(fh(x,y:y+3),2)>2)

fs(x-2:x+2,y-2:y+2)=bwmorph(fh(x-2:x+2,y-2:y+2),'dilate',se);
end;
end;
end;
end;
%imshow(fin)
%image with dilation performed
f=double(fs);
[row col]=size(f);
%initialising a matrix for a segmented image
g=zeros(row,col);
gl=zeros(row,col);
label=1;
n=1;
x=1;
iter=[];
it=0;
ss_prev=0;
nn=[];
sss_mat=[];
for i=1:2:row
for j=1:2:col
r_pt=i;
c_pt=j;
if(g(r_pt,c_pt)==0)
while(true)
%using 4 neighbour rule
if(f(r_pt(n),c_pt(n))==1 && g(r_pt(n),c_pt(n))==0)
g(r_pt(n),c_pt(n))=label;
if(r_pt(n)+1<=row)
if(f(r_pt(n)+1,c_pt(n))==1)
r_pt=[r_pt r_pt(n)+1];
c_pt=[c_pt c_pt(n)];

x=x+1;
end;
end;
if(c_pt(n)-1>=1)
if(f(r_pt(n),c_pt(n)-1)==1)
r_pt=[r_pt r_pt(n)];
c_pt=[c_pt c_pt(n)-1];
x=x+1;
end;
end;
if(c_pt(n)+1<=col)
if(f(r_pt(n),c_pt(n)+1)==1)
r_pt=[r_pt r_pt(n)];
c_pt=[c_pt c_pt(n)+1];
x=x+1;
end;
end;
if(r_pt(n)-1>=1)
if(f(r_pt(n)-1,c_pt(n))==1)
r_pt=[r_pt r_pt(n)-1];
c_pt=[c_pt c_pt(n)];
x=x+1;
end;
end;
end;
if(n>=x)
break;
end;
n=n+1;
end;
y1=min(r_pt);
y2=max(r_pt);
x1=min(c_pt);
x2=max(c_pt);
a1=g(min(r_pt):max(r_pt),min(c_pt):max(c_pt));
f1=d(min(r_pt):max(r_pt),min(c_pt):max(c_pt));
[ra ca]=size(a1);

if(n>=50)|
b1=bwlabel(a1);
ss=regionprops(b1,'euler number');
sss=struct2array(ss);
sss=min(sss);
sss_mat=[sss_mat sss];

if(sss<ss_prev && sss<0 && ca <=190 && ra<=60 && ca>=50 && ra >=15 &&
mean(mean(f1))<=220)
x_cor1=x1;
y_cor1=y1;
x_cor2=x2;
y_cor2=y2;
ss_prev=sss;
end;
label=label+1;
else
g(r_pt,c_pt)=0;
end;
end;
x=1;
n=1;
it=1;
end;
end;
if(exist('y_cor1')==1)
d(y_cor1:y_cor1+2,x_cor1:x_cor2)=255;
d(y_cor2:y_cor2+2,x_cor1:x_cor2)=255;
d(y_cor1:y_cor2,x_cor1:x_cor1+2)=255;
d(y_cor1:y_cor2,x_cor2:x_cor2+2)=255;
end;
% Segmented licence plate image
d=mat2gray(d);
lp=d(y_cor1:y_cor2,x_cor1:x_cor2);
%%% 2. Character Segmentation
%License plate image, characters of wcich are to be segmented
lp1 = d(y_cor1:y_cor2,x_cor1:x_cor2);
[rl cl] = size(lp1);

% Median Filtering
lp = medfilt2(lp1,[3 3]);
% Contrast Enhancement
%creating output image matrix
output= zeros(rl,cl);
% Window for local threshold operation
dis = round(cl/7);
% Local threshold operation
for i=1:dis:cl
if(i+dis-1<=cl)
t=threshcal(lpf(:,i:i+dis-1),a);
for i=1:dis:cl
if(i+dis-1<=cl)
t=threshcal(lpf(:,i:i+dis-1),a);
output(:,i:i+dis-1)=lpf(:,i:i+dis-1)<=t;
else
t=threshcal(lpf(:,i:cl),a);
for z1=2:rl-1
for z2=i+5:cl-5
if(mean(mean(lpf(z1-1:z1+1,z2-5:z2+5)))<=t)
output(z1,z2)=1;
end;
end;
end;
output(:,i:cl)=lpf(:,i:cl)<=t;
end;
end;
end;
end;
% Structuring element for erosion operation
se = [1 1 1

1 1 1 1 1 1 1 1 1

1 1 1]

## output = output - imerode(output,se);

[of lab lpdet] = reggrowl(logical(output),number);
% Segmented characters
lpdet = logical(lpdet);
% Character Recognition

## % output String giving licence plate information

lpstr=[];
for i= 1:lab-1
R = lpdet(:,st:st+9);
st = st+10;
b = bwlabel(R);
% Feature extraction
ar = struct2array(regionprops(b,'area'));
or = struct2aarray(regionprops(b,'orientation'))/90;
eu = struct2array(regionprops(b,'eulernumber'))/10;
pe = struct2array(regionprops(b,'perimeter'));
mi = struct2array(regionprops(b,'minoraxislength'));
ma = struct2array(regionprops(b,'majoraxislength'));
temp = logical(R);
% Reflection X and Y coefficient determination
v1 = temp;
v1(:,6:10)=flipdim(temp(:,1:5),2);
vx = (v1 + temp)/2;
vx = vx>=0.5;
xcoef = sum(sum(temp),2)/sum(sum(vx),2);
v2 = temp;
v2(1:12,:) = flipdim(temp(13:24,:),1);
vy = (v2 + temp)/2;
vy = vy >= 0.5;
ycoef = sum(sum(temp),2)/sum(sum(vy),2);
ed = struct2array(regionprops(b,'equivdiameter'))/100;
[val pos] = max(fa);
vcoeff = pe(pos)/ar(pos);
mcoeff = ed(pos);
Rp = [xcoef/ycoef;pe(pos)/ar(pos);mi(pos)/ma(pos)];
if(i<=numel(lpnum))
numrc = numrc+1;
else
answ = find(alphamat==lpnum(i));
err(answ) = err(answ) + 1;

end;
end;