車牌識別的matlab程序_第1頁
車牌識別的matlab程序_第2頁
車牌識別的matlab程序_第3頁
車牌識別的matlab程序_第4頁
車牌識別的matlab程序_第5頁
已閱讀5頁,還剩11頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、附錄車牌識別程序clear ;close all;%Step1 獲取圖像裝入待處理彩色圖像并顯示原始圖像Scolor = imread('3.jpg'%imread函數讀取圖像文件%將彩色圖像轉換為黑白并顯示Sgray = rgb2gray(Scolor;%rgb2gray轉換成灰度圖figure,imshow(Scolor,title('原始彩色圖像'%figure命令同時顯示兩幅圖figure,imshow(Sgray,title('原始黑白圖像'%Step2 圖像預處理對Sgray 原始黑白圖像進行開操作得到圖像背景s=strel('

2、;disk',13;%strel函數Bgray=imopen(Sgray,s;%打開sgray s圖像figure,imshow(Bgray;title('背景圖像'%輸出背景圖像%用原始圖像與背景圖像作減法,增強圖像Egray=imsubtract(Sgray,Bgray;%兩幅圖相減figure,imshow(Egray;title('增強黑白圖像'%輸出黑白圖像%Step3 取得最佳閾值,將圖像二值化fmax1=double(max(max(Egray;%egray的最大值并輸出雙精度型fmin1=double(min(min(Egray;%egr

3、ay的最小值并輸出雙精度型level=(fmax1-(fmax1-fmin1/3/255;%獲得最佳閾值bw22=im2bw(Egray,level;%轉換圖像為二進制圖像bw2=double(bw22;%Step4 對得到二值圖像作開閉操作進行濾波figure,imshow(bw2;title('圖像二值化'%得到二值圖像grd=edge(bw2,'canny'%用canny算子識別強度圖像中的邊界figure,imshow(grd;title('圖像邊緣提取'%輸出圖像邊緣bg1=imclose(grd,strel('rectangl

4、e',5,19;%取矩形框的閉運算figure,imshow(bg1;title('圖像閉運算5,19'%輸出閉運算的圖像bg3=imopen(bg1,strel('rectangle',5,19;%取矩形框的開運算figure,imshow(bg3;title('圖像開運算5,19'%輸出開運算的圖像bg2=imopen(bg3,strel('rectangle',19,1;%取矩形框的開運算figure,imshow(bg2;title('圖像開運算19,1'%輸出開運算的圖像%Step5 對二值圖像進

5、行區域提取,并計算區域特征參數。進行區域特征參數比較,提取車牌區域L,num = bwlabel(bg2,8;%標注二進制圖像中已連接的部分Feastats = imfeature(L,'basic'%計算圖像區域的特征尺寸Area=Feastats.Area;%區域面積BoundingBox=Feastats.BoundingBox;%x y width height車牌框架大小RGB = label2rgb(L, 'spring', 'k', 'shuffle' %標志圖像向RGB圖像轉換figure,imshow(RGB;t

6、itle('圖像彩色標記'%輸出框架的彩色圖像lx=0;for l=1:numwidth=BoundingBox(l-1*4+3;%框架寬度的計算hight=BoundingBox(l-1*4+4;%框架高度的計算if (width>98 & width<160 & hight>25 & hight<50%框架的寬度和高度的范圍lx=lx+1;Getok(lx=l;endendfor k= 1:lxl=Getok(k;startcol=BoundingBox(l-1*4+1-2;%開始列startrow=BoundingBox(l

7、-1*4+2-2;%開始行width=BoundingBox(l-1*4+3+8;%車牌寬hight=BoundingBox(l-1*4+4+2;%車牌高rato=width/hight;%計算車牌長寬比if rato>2 & rato<4break;endendsbw1=bw2(startrow:startrow+hight,startcol:startcol+width-1; %獲取車牌二值子圖subcol1=Sgray(startrow:startrow+hight,startcol:startcol+width-1;%獲取車牌灰度子圖figure,subplot(2

8、,1,1,imshow(subcol1;title('車牌灰度子圖'%輸出灰度圖subplot(2,1,2,imshow(sbw1;title('車牌二值子圖'%輸出車牌的二值圖%Step6 計算車牌水平投影,并對水平投影進行峰谷分析histcol1=sum(sbw1; %計算垂直投影histrow=sum(sbw1' %計算水平投影figure,subplot(2,1,1,bar(histcol1;title('垂直投影(含邊框'%輸出垂直投影subplot(2,1,2,bar(histrow; title('水平投影(含邊框&

9、#39;%輸出水平投影figure,subplot(2,1,1,bar(histrow; title('水平投影(含邊框'%輸出水平投影subplot(2,1,2,imshow(sbw1;title('車牌二值子圖'%輸出二值圖%對水平投影進行峰谷分析meanrow=mean(histrow;%求水平投影的平均值minrow=min(histrow;%求水平投影的最小值levelrow=(meanrow+minrow/2;%求水平投影的平均值count1=0;l=1;for k=1:hightif histrow(k<=levelrowcount1=cou

10、nt1+1;elseif count1>=1markrow(l=k;%上升點markrow1(l=count1;%谷寬度(下降點至下一個上升點l=l+1;endcount1=0;endendmarkrow2=diff(markrow;%峰距離(上升點至下一個上升點m1,n1=size(markrow2;n1=n1+1;markrow(l=hight;markrow1(l=count1;markrow2(n1=markrow(l-markrow(l-1;l=0;for k=1:n1markrow3(k=markrow(k+1-markrow1(k+1;%下降點markrow4(k=mark

11、row3(k-markrow(k;%峰寬度(上升點至下降點markrow5(k=markrow3(k-double(uint16(markrow4(k/2;%峰中心位置end%Step7 計算車牌旋轉角度%(1在上升點至下降點找第一個為1的點m2,n2=size(sbw1;%sbw1的圖像大小m1,n1=size(markrow4;%markrow4的大小maxw=max(markrow4;%最大寬度為字符if markrow4(1 = maxw%檢測上邊ysite=1;k1=1;for l=1:n2for k=1:markrow3(ysite%從頂邊至第一個峰下降點掃描if sbw1(k,l

12、=1xdata(k1=l;ydata(k1=k;k1=k1+1;break;endendendelse %檢測下邊ysite=n1;if markrow4(n1 =0if markrow4(n1-1 =maxwysite= 0; %無下邊elseysite= n1-1;endendif ysite =0k1=1;for l=1:n2k=m2;while k>=markrow(ysite %從底邊至最后一個峰的上升點掃描if sbw1(k,l=1xdata(k1=l;ydata(k1=k;k1=k1+1;break;endk=k-1;endendendend%(2線性擬合,計算與x夾角fr

13、esult = fit(xdata',ydata','poly1' %poly1 Y = p1*x+p2p1=fresult.p1;angle=atan(fresult.p1*180/pi; %弧度換為度,360/2pi, pi=3.14%(3旋轉車牌圖象subcol = imrotate(subcol1,angle,'bilinear','crop' %旋轉車牌圖象sbw = imrotate(sbw1,angle,'bilinear','crop'%旋轉圖像figure,subplot(2,1,

14、1,imshow(subcol;title('車牌灰度子圖'%輸出車牌旋轉后的灰度圖像標題顯示車牌灰度子圖subplot(2,1,2,imshow(sbw;title(''%輸出車牌旋轉后的灰度圖像title('車牌旋轉角: ',num2str(angle,'度' ,'Color','r'%顯示車牌的旋轉角度%Step8 旋轉車牌后重新計算車牌水平投影,去掉車牌水平邊框,獲取字符高度histcol1=sum(sbw; %計算垂直投影histrow=sum(sbw' %計算水平投影figure

15、,subplot(2,1,1,bar(histcol1;title('垂直投影(旋轉后'subplot(2,1,2,bar(histrow; title('水平投影(旋轉后'figure,subplot(2,1,1,bar(histrow; title('水平投影(旋轉后'subplot(2,1,2,imshow(sbw;title('車牌二值子圖(旋轉后'%去水平(上下邊框,獲取字符高度maxhight=max(markrow2;findc=find(markrow2=maxhight;rowtop=markrow(findc;

16、rowbot=markrow(findc+1-markrow1(findc+1;sbw2=sbw(rowtop:rowbot,:; %子圖為(rowbot-rowtop+1行maxhight=rowbot-rowtop+1; %字符高度(rowbot-rowtop+1%Step9 計算車牌垂直投影,去掉車牌垂直邊框,獲取車牌及字符平均寬度histcol=sum(sbw2; %計算垂直投影figure,subplot(2,1,1,bar(histcol;title('垂直投影(去水平邊框后'%輸出車牌的垂直投影圖像subplot(2,1,2,imshow(sbw2; %輸出垂直投

17、影圖像title('車牌字符高度:',int2str(maxhight,'Color','r'%輸出車牌字符高度%對垂直投影進行峰谷分析meancol=mean(histcol;%求垂直投影的平均值mincol=min(histcol;%求垂直投影的平均值levelcol=(meancol+mincol/4;%求垂直投影的1/4count1=0;l=1;for k=1:widthif histcol(k<=levelcolcount1=count1+1;elseif count1>=1markcol(l=k; %字符上升點markco

18、l1(l=count1; %谷寬度(下降點至下一個上升點l=l+1;endcount1=0;endendmarkcol2=diff(markcol;%字符距離(上升點至下一個上升點m1,n1=size(markcol2;n1=n1+1;markcol(l=width;markcol1(l=count1;markcol2(n1=markcol(l-markcol(l-1;%Step10 計算車牌上每個字符中心位置,計算最大字符寬度maxwidth l=0;for k=1:n1markcol3(k=markcol(k+1-markcol1(k+1;%字符下降點markcol4(k=markcol3

19、(k-markcol(k; %字符寬度(上升點至下降點markcol5(k=markcol3(k-double(uint16(markcol4(k/2;%字符中心位置endmarkcol6=diff(markcol5; %字符中心距離(字符中心點至下一個字符中心點maxs=max(markcol6; %查找最大值,即為第二字符與第三字符中心距離findmax=find(markcol6=maxs;markcol6(findmax=0;maxwidth=max(markcol6;%查找最大值,即為最大字符寬度%Step11 提取分割字符,并變換為22行 14列標準子圖l=1;m2,n2=size

20、(subcol;figure;for k=findmax-1:findmax+5cleft=markcol5(k-maxwidth/2;cright=markcol5(k+maxwidth/2-2;if cleft<1cleft=1;cright=maxwidth;endif cright>n2cright=n2;cleft=n2-maxwidth;endSegGray=sbw(rowtop:rowbot,cleft:cright;SegBw1=sbw(rowtop:rowbot,cleft:cright;SegBw2 = imresize(SegBw1,22 14;%變換為22行

21、14列標準子圖subplot(2,n1,l,imshow(SegGray;if l=7title('車牌字符寬度:',int2str(maxwidth,'Color','r'endsubplot(2,n1,n1+l,imshow(SegBw2;fname=strcat('c:worksamimage',int2str(k,'.jpg'imwrite(SegBw2,fname,'jpg'l=l+1;end%Step12 將計算計算獲取的字符圖像與樣本庫進行匹配,自動識別出字符代碼。liccode=ch

22、ar('0':'9' 'A':'Z' '粵桂海云貴川京津滬' %建立自動識別字符代碼表SubBw2=zeros(22,14;l=1;m2,n2=size(sbw;for k=findmax-1:findmax+5cleft=markcol5(k-maxwidth/2;cright=markcol5(k+maxwidth/2-2;if cleft<1cleft=1;cright=maxwidth;endif cright>n2cright=n2;cleft=n2-maxwidth; endSegBw1=sbw(rowtop:rowbot,cleft:cright;Seg

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論