資源描述:
《基于matlab的汽車牌照識(shí)別源程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、基于matlab的車牌識(shí)別的源程序clcclearfilename='E:matlab7.0car.jpg';%圖片的路徑I=im2gray(filename);%調(diào)用自編函數(shù)讀取圖像,并轉(zhuǎn)化為灰度圖象;tic%計(jì)時(shí)開(kāi)始[height,width]=size(I);%預(yù)處理I_edge=zeros(height,width);%創(chuàng)建height*width矩陣fori=1:width-1%對(duì)每一列進(jìn)行遍歷I_edge(:,i)=abs(I(:,i+1)-I(:,i));%每列的值賦為原圖像中左右兩列相減的絕對(duì)值(即梯度)end
2、%歸一化處理(0~255)I_edge=(255/(max(max(I_edge))-min(min(I_edge))))*(I_edge-min(min(I_edge)));[I_edge,y1]=select(I_edge,height,width);%%%%%%調(diào)用select函數(shù)選擇圖像的某個(gè)區(qū)域BW2=I_edge;%%%%%%%%%%%%%%%%%一些形態(tài)學(xué)處理SE=strel('rectangle',[10,10]);%創(chuàng)建10*10的建構(gòu)元素IM2=imerode(BW2,SE);%腐蝕IM2=bwareaopen
3、(IM2,20);%刪除小面積IM3=imdilate(IM2,SE);%膨脹%先腐蝕再膨脹,進(jìn)行了開(kāi)運(yùn)算,消除小物體%%%%%%%%%%%%%%%%%%投影以粗略估計(jì)車牌位置p_h=projection(double(IM3),'h');%調(diào)用projection函數(shù),水平方向if(p_h(1)>0)p_h=[0,p_h];endp_v=projection(double(IM3),'v');%調(diào)用projection函數(shù),垂直方向if(p_v(1)>0)p_v=[0,p_v];end%%%%%%p_h=double((p_h>
4、5));%水平方向p_h=find(((p_h(1:end-1)-p_h(2:end))~=0));len_h=length(p_h)/2;%%%%%p_v=double((p_v>5));%垂直方向p_v=find(((p_v(1:end-1)-p_v(2:end))~=0));len_v=length(p_v)/2;%%%%%%%%%%%%%%%%%%%%%%%%%%%%粗略計(jì)算車牌候選區(qū)k=1;fori=1:len_hforj=1:len_vs=IM3(p_h(2*i-1):p_h(2*i),p_v(2*j-1):p_v(2
5、*j));if(mean(mean(s))>0.1)p{k}=[p_h(2*i-1),p_h(2*i)+1,p_v(2*j-1),p_v(2*j)+1];k=k+1;endendendk=k-1;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%進(jìn)一步縮小車牌候選區(qū)fori=1:kedge_IM3=double(edge(double(IM3(p{i}(1):p{i}(2),p{i}(3):p{i}(4))),'canny'));[x,y]=find(edge_IM3==1);p{i}=[
6、p{i}(1)+min(x),p{i}(2)-(p{i}(2)-p{i}(1)+1-max(x)),...p{i}(3)+min(y),p{i}(4)-(p{i}(4)-p{i}(3)+1-max(y))];p_center{i}=[fix((p{i}(1)+p{i}(2))/2),fix((p{i}(3)+p{i}(4))/2)];p_ratio(i)=(p{i}(4)-p{i}(3))/(p{i}(2)-p{i}(1));end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%對(duì)上面參數(shù)和變量的說(shuō)明:p為一胞元,用于
7、存放每個(gè)圖像塊的左上和右下兩個(gè)點(diǎn)的坐標(biāo);%存放格式為:p{k}=[x1,x2,y1,y2];x1,x2分別為行坐標(biāo),y1,y2為列坐標(biāo)%p_center為一胞元,用于存放每個(gè)圖像塊的中心坐標(biāo),p_center{k}=[x,y];x,y分別為行,列坐標(biāo)%p_ratio為一矩陣,用來(lái)存放圖像塊的長(zhǎng)寬比例%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%合并臨近區(qū)域%%%%%%%%如果有多個(gè)區(qū)域則執(zhí)行合并ifk>1n=0;ncount=zeros(1,k);fori=1:k-1%%%需要調(diào)整if條件中的比
8、例%%%需要調(diào)整%檢查是否滿足合并條件if(abs(p{i}(1)+p{i}(2)-p{i+1}(1)-p{i+1}(2))<=height/30&&abs(p{i+1}(3)-p{i}(4))<=width/15)p{i+1}(1)=min(p{