车牌识别
MATLAB程序如下:(字符模板見附錄)
I=imread('car6.bmp');
%figure(1),imshow(I);title('original image');%將車牌的原圖顯示出來,結果如下:I1=rgb2gray(I);%將彩圖轉換為灰度圖
% figure(2),subplot(1,2,1),imshow(I1);title('gray image');
% figure(2),subplot(1,2,2),imhist(I1);title('灰度圖直方圖');%繪制灰度圖的直方圖
I2=edge(I1,'canny',0.3);%選擇閾值0.18,用roberts算子進行邊緣檢測
% figure(2),imshow(I2);title('roberts operator edge detection image');
se=[1;1;1];
I3=imerode(I2,se);%對圖像實施腐蝕操作,即膨脹的反操作
% figure(4),imshow(I3);title('corrosion image');
se=strel('rectangle',[25,25]);%構造結構元素以正方形構造一個se
I4=imclose(I3,se);% 圖像聚類、填充圖像
% figure(5),imshow(I4);title('smothing image');
?I5=bwareaopen(I4,2000);% 去除聚團灰度值小于2000的部分
figure(6),imshow(I5);title('remove the small objects'); %用imshow函數顯示濾波后圖像
[y,x,z]=size(I5);%返回I5各維的尺寸,存儲在x,y,z中
myI=double(I5);%將I5轉換成雙精度
tic ? ? ?%tic表示計時的開始,toc表示計時的結束
?Blue_y=zeros(y,1);%產生一個y*1的零陣
?for i=1:y
? ? for j=1:x
? ? ? ? ? ? ?if(myI(i,j,1)==1)?
? %如果myI(i,j,1)即myI的圖像中坐標為(i,j)的點值為1,即該點為車牌背景顏色藍色
? %則Blue_y(i,1)的值加1
? ? ? ? ? ? ? ? Blue_y(i,1)= Blue_y(i,1)+1;%藍色像素點統計?
? ? ? ? ? ? end ?
? ? ?end ? ? ??
?end
?[temp MaxY]=max(Blue_y);%Y方向車牌區域確定
? %temp為向量yellow_y的元素中的最大值,MaxY為該值的索引
?PY1=MaxY;
?while ((Blue_y(PY1,1)>=5)&&(PY1>1))
? ? ? ? PY1=PY1-1;
?end ? ?
?PY2=MaxY;
?while ((Blue_y(PY2,1)>=5)&&(PY2<y))
? ? ? ? PY2=PY2+1;
?end
?IY=I(PY1:PY2,:,:);
?%行方向車牌區域確定
?%%%%%% X方向 %%%%%%%%%
?Blue_x=zeros(1,x);%進一步確定x方向的車牌區域
?for j=1:x
? ? ?for i=PY1:PY2
? ? ? ? ? ? if(myI(i,j,1)==1)
? ? ? ? ? ? ? ? Blue_x(1,j)= Blue_x(1,j)+1; ? ? ? ? ? ? ??
? ? ? ? ? ? end ?
? ? ?end ? ? ??
?end
??
?PX1=1;
?while ((Blue_x(1,PX1)<3)&&(PX1<x))
? ? ? ?PX1=PX1+1;
?end ? ?
?PX2=x;
?while ((Blue_x(1,PX2)<3)&&(PX2>PX1))
? ? ? ? PX2=PX2-1;
?end
?PX1=PX1-1;%對車牌區域的校正
?PX2=PX2+1;
? dw=I(PY1:PY2-4,PX1+4:PX2-4,:);
?t=toc;?
figure(7),subplot(1,2,1),imshow(IY),title('Line direction areas');%行方向車牌區域確定
figure(7),subplot(1,2,2),imshow(dw),title('positioning color ?images');%定位后
imwrite(dw,'dw.jpg');%將彩色車牌寫入dw文件中
a=imread('dw.jpg');%讀取車牌文件中的數據
b=rgb2gray(a);%將車牌圖像轉換為灰度圖
imwrite(b,'gray licence plate.jpg');%將灰度圖像寫入文件中
figure(8);subplot(3,2,1),imshow(b),title('車牌灰度圖像')
g_max=double(max(max(b)));
g_min=double(min(min(b)));
T=round(g_max-(g_max-g_min)/3); % T 為二值化的閾值
[m,n]=size(b);
d=(double(b)>=T); ?% d:二值圖像
imwrite(d,'binary licence plate.jpg');
subplot(3,2,2),imshow(d),title('before filtering binary licence plate')
%均值濾波前
% 濾波
h=fspecial('average',3);
%建立預定義的濾波算子,average為均值濾波,模板的尺寸為3*3
d=im2bw(round(filter2(h,d)));%使用指定的濾波器h對h進行d即均值濾波
imwrite(d,'after average licence plate.jpg');
subplot(3,2,3),imshow(d),title('after average licence plate')
% 某些圖像進行操作
% 膨脹或腐蝕
% se=strel('square',3); ?% 使用一個3X3的正方形結果元素對象對創建的圖像進行膨脹
% 'line'/'diamond'/'ball'...
se=eye(2); % eye(n) returns the n-by-n identity matrix 單位矩陣
[m,n]=size(d);%返回矩陣b的尺寸信息, 并存儲在m,n中
if bwarea(d)/m/n>=0.365 %計算二值圖像中對象的總面積與整個面積的比是否大于0.365
? ? d=imerode(d,se);%如果大于0.365則圖像進行腐蝕
elseif bwarea(d)/m/n<=0.235 %計算二值圖像中對象的總面積與整個面積的比是否小于0.235
? ? d=imdilate(d,se);%如果小于則實現膨脹操作
end
imwrite(d,'expansion or corrosion the licence plate.jpg');
subplot(3,2,4),imshow(d),title('expansion or corrosion the licence plate');
% histcol1=sum(d); %計算垂直投影
% histrow=sum(d'); %計算水平投影
% figure,subplot(2,1,1),bar(histcol1);title('垂直投影(旋轉后)');
% subplot(2,1,2),bar(histrow); ? ? title('水平投影(旋轉后)');
%%%%%%%%%%%%%%%%%%%分割字符%%%%%%%%%%%%%%%%%%%%?
d=Cutting(d);
[m,n]=size(d);
subplot(3,2,5),imshow(d),title(n)
k1=1;k2=1;s=sum(d);j=1;
while j~=n
? ? while s(j)==0
? ? ? ? j=j+1;
? ? end
? ? k1=j;
? ? while s(j)~=0 && j<=n-1
? ? ? ? j=j+1;
? ? end
? ? k2=j-1;
? ? if k2-k1>=round(n/6.5)
? ? ? ? [val,num]=min(sum(d(:,[k1+5:k2-5])));
? ? ? ? d(:,k1+num+5)=0; ?% 分割
? ? end
end
% 再切割
d=Cutting(d);
% 切割出 7 個字符
y1=10;y2=0.25;flag=0;word1=[];
while flag==0
? ? [m,n]=size(d);
? ? left=1;wide=0;
? ? while sum(d(:,wide+1))~=0
? ? ? ? wide=wide+1;
? ? end
? ? if wide<y1 ? % 認為是左側干擾
? ? ? ? d(:,[1:wide])=0;
? ? ? ? d=Cutting(d);
? ? else
? ? ? ? temp=Cutting(imcrop(d,[1 1 wide m]));
? ? ? ? [m,n]=size(temp);
? ? ? ? all=sum(sum(temp));
? ? ? ? two_thirds=sum(sum(temp([round(m/3):2*round(m/3)],:)));
? ? ? ? if two_thirds/all>y2
? ? ? ? ? ? flag=1;word1=temp; ? % WORD 1
? ? ? ? end
? ? ? ? d(:,[1:wide])=0;d=Cutting(d);
? ? end
end
% 分割出第二個字符
[word2,d]=GetSingleStr(d);
% 分割出第三個字符
[word3,d]=GetSingleStr(d);
% 分割出第四個字符
[word4,d]=GetSingleStr(d);
% 分割出第五個字符
[word5,d]=GetSingleStr(d);
% 分割出第六個字符
[word6,d]=GetSingleStr(d);
% 分割出第七個字符
[word7,d]=GetSingleStr(d);
figure(9);
subplot(2,7,1),imshow(word1),title('1');
subplot(2,7,2),imshow(word2),title('2');
subplot(2,7,3),imshow(word3),title('3');
subplot(2,7,4),imshow(word4),title('4');
subplot(2,7,5),imshow(word5),title('5');
subplot(2,7,6),imshow(word6),title('6');
subplot(2,7,7),imshow(word7),title('7');
[m,n]=size(word1);
% 商用系統程序中歸一化大小為 40*20,此處演示
word1=imresize(word1,[32 16]);
word2=imresize(word2,[32 16]);
word3=imresize(word3,[32 16]);
word4=imresize(word4,[32 16]);
word5=imresize(word5,[32 16]);
word6=imresize(word6,[32 16]);
word7=imresize(word7,[32 16]);
figure(9);
subplot(2,7,8),imshow(word1),title('1');
subplot(2,7,9),imshow(word2),title('2');
subplot(2,7,10),imshow(word3),title('3');
subplot(2,7,11),imshow(word4),title('4');
subplot(2,7,12),imshow(word5),title('5');
subplot(2,7,13),imshow(word6),title('6');
subplot(2,7,14),imshow(word7),title('7');
imwrite(word1,'1.jpg');
imwrite(word2,'2.jpg');
imwrite(word3,'3.jpg');
imwrite(word4,'4.jpg');
imwrite(word5,'5.jpg');
imwrite(word6,'6.jpg');
imwrite(word7,'7.jpg');
liccode=char(['0':'9' 'A':'Z' '蘇豫陜魯京遼浙']) ?%建立自動識別字符代碼表 ?
SubBw2=zeros(32,16);
l=1;
for I=1:7
? ? ? ii=int2str(I);
? ? ?t=imread([ii,'.jpg']);
? ? ? SegBw2=imresize(t,[32 16],'nearest');
? ? ? ?SegBw2=double(SegBw2)>150;
? ? ? % figure(10);imshow(SegBw2);
? ? ? ? if l==1 ? ? ? ? ? ? ? ? %第一位漢字識別
? ? ? ? ? ? kmin=37;
? ? ? ? ? ? kmax=43;
? ? ? ? elseif l==2 ? ? ? ? ? ? %第二位 A~Z 字母識別
? ? ? ? ? ? kmin=11;
? ? ? ? ? ? kmax=36;
? ? ? ? else l>=3 ? ? ? ? ? ? ? %第三位以后是字母或數字識別
? ? ? ? ? ? kmin=1;
? ? ? ? ? ? kmax=36;
? ? ? ??
? ? ? ? end
? ? ? ??
? ? ? ? for k2=kmin:kmax
? ? ? ? ? ? fname=strcat('字符模板\',liccode(k2),'.bmp');
? ? ? ? ? ? SamBw2 = imread(fname);
? ? ? ? ? ? %SamBw2=rgb2bw(SamBw2);
? ? ? ? ? ? %SamBw2=double(SamBw2)>200;
? ? ? ? ? ? for ?i=1:32
? ? ? ? ? ? ? ? for j=1:16
? ? ? ? ? ? ? ? ? ? SubBw2(i,j)=SegBw2(i,j)-SamBw2(i,j);
? ? ? ? ? ? ? ? end
? ? ? ? ? ? end
? ? ? ? ? ?% 以上相當于兩幅圖相減得到第三幅圖
? ? ? ? ? ? Dmax=0;
? ? ? ? ? ? for k1=1:32
? ? ? ? ? ? ? ? for l1=1:16
? ? ? ? ? ? ? ? ? ? if ?( SubBw2(k1,l1) > 0 | SubBw2(k1,l1) <0 )
? ? ? ? ? ? ? ? ? ? ? ? Dmax=Dmax+1;
? ? ? ? ? ? ? ? ? ? end
? ? ? ? ? ? ? ? end
? ? ? ? ? ? end
? ? ? ? ? ? Error(k2)=Dmax;
? ? ? ? end
? ? ? ? Error1=Error(kmin:kmax);
? ? ? ? MinError=min(Error1);
? ? ? ? findc=find(Error1==MinError);
? ? ? ? Code(l*2-1)=liccode(findc(1)+kmin-1);
? ? ? ? Code(l*2)=' ';
? ? ? ? l=l+1;
end
figure(10),imshow(dw),title (['車牌號碼:', Code],'Color','b');
界面如下:
總結
- 上一篇: 第八周---FPGA流水灯显示和串口循环
- 下一篇: pro4重影花屏 surface_【图】