車牌識別中的車牌定位

該程序參考的是一篇碩士論文:王璐《基於MATLAB的車牌識別系統研究》,並參考了百百度文庫中的作者成果,鏈接爲http://wenku.baidu.com/view/23dde718fc4ffe473368abb8.html 

以下是程序,每一步都標識出操作目的,做起來很流暢,但是經過多方嘗試,發覺這段代碼只能識別出畫質清晰,車牌面積比重較大的情況下,所以目前的工作還有待改進。

close all
clear all
clc
%%%3.1 車輛圖像預處理
I=imread('C:\Users\Administrator\Desktop\car1.jpg');
gray=rgb2gray(I);
gray=histeq(gray);
gray=medfilt2(gray);
s=strel('disk',5);
Bgray=imopen(gray,s);
Egray=imsubtract(gray,Bgray);

%%%3.2 車牌邊緣提取(小波變換)
Egray=double(Egray);
[c,s]=wavedec2(Egray,2,'db1');
cv2=detcoef2('v',c,s,2);

%%%3.3.1結構元素提取
a1=imclearborder(cv2,8);
st=ones(1,14);
bg1=imclose(a1,st);
bg2=imopen(bg1,st);
bg3=imopen(bg2,[1,1,1,1,1,1,1]');
bg=uint8(bg3);
level = graythresh(bg);
bw2=im2bw(bg,level);
se=strel('disk',3);
bw=imopen(bw2,se);
figure(1)
subplot(1,3,1);imshow(Egray);
subplot(1,3,2);imshow(bg3,[]);
subplot(1,3,3);imshow(bw,[]);

%%%3.3.2提取候選區域
[L,num]=bwlabel(bw,8);
feature=regionprops(L,'basic');
area=[feature.Area];
boundbox=[feature.BoundingBox];
RGB=label2rgb(L,'spring','k','shuffle');
lx=0;
for l=1:1:num
col=boundbox((l-1)*4+1);     
row=boundbox((l-1)*4+2); 
width=boundbox((l-1)*4+3);
hight=boundbox((l-1)*4+4);
rato=width/hight;    
if (row>5)&(col>5)&(rato>2)&(rato<4)
lx=lx+1;break;
end
end

%%顯示車牌區域
bwD=bw((row-1):(row+hight-1),(col-1):(col+width-1));
grayD=gray((4*row-20):(4*row+4*hight+10),(4*col-20):(4*col+4*width+10));
figure(2);
subplot(1,2,1);imshow(bwD);
subplot(1,2,2);imshow(grayD);

%%%3.4.1 車牌水平方向定位算法
%%水平方向一階差分
[m,n]=size(grayD);
f=zeros(m,n-1);
for i=1:1:m
line=grayD(i,:);
f(i,:)=diff(line);
f(i,:)=abs(f(i,:));
end
%水平投影
F=sum(f,2);
w=fspecial('average',[3,1]);
Th=imfilter(F,w);
Th=uint8(Th);
Th=im2bw(Th,0.6);

for i=1:1:m
if (Th(i+1,1)-Th(i,1))==1
strow=i+1;
else if (Th(i+1,1)-Th(i,1))==-1
endrow=i;
break;
end
end
end

%%豎直方向一階差分
g=zeros(m-1,n);
for j=1:1:n
line=grayD(:,j);
g(:,j)=diff(line);
g(:,j)=abs(g(:,j));
end
%豎直投影
G=sum(g);
w=fspecial('average',[1,3]);
Tv=imfilter(G,w);
Tv=uint8(Tv);
Tv=im2bw(Tv,0.4);

for j=1:1:n
if (Tv(1,j+1)-Tv(1,j))==1
strcol=j+1;
else if (Tv(1,j+1)-Tv(1,j))==-1
endcol=j;
break;
end
end
end

%%%車牌精確定位後的圖像
Des=grayD(strow:endrow,strcol:endcol);
figure(3);
subplot(2,1,1);imshow(grayD);
subplot(2,1,2);imshow(Des);


原圖


figure(1)


figure(2)


figure(3)


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