基於matlab的藍色車牌定位與識別---定位


    接着昨天的工作繼續。定位的過程有些是基於車牌的顏色進行定位的,自己則根據數字圖像一些形態學的方法進行定位的。

    合着代碼進行相關講解。

   1.相對彩色圖像進行灰度化,然后對圖像進行開運算。再用小波變換獲取圖像的三個分量。考慮到車牌的豎直分量較為豐富,選用豎直分量進行后續操作。注意下,這里的一些參數可能和你的圖片大小有關,所以要根據實際情況調整。

Image=imread('D:\1_2學習\圖像處理\車牌識別\matlab_car_plate-recognization\car_example\car0.jpg');%獲取圖片
im=double(rgb2gray(Image));
im=imresize(im,[1024,2048]);%重新設置圖片大小  [1024,2048]
% % 灰度拉伸
% % im=imadjust(Image,[0.15 0.9],[]);
  s=strel('disk',10);%strei函數
 Bgray=imopen(im,s);%打開sgray s圖像
% % figure,imshow(Bgray);title('背景圖像');%輸出背景圖像
% %用原始圖像與背景圖像作減法,增強圖像
 im=imsubtract(im,Bgray);%兩幅圖相減
% figure,imshow(mat2gray(im));title('增強黑白圖像');%輸出黑白圖像

[Lo_D,Hi_D]=wfilters('db2','d'); % d Decomposition filters
[C,S]= wavedec2(im,1,Lo_D,Hi_D); %Lo_D  is the decomposition low-pass filter
% decomposition vector C    corresponding bookkeeping matrix S
isize=prod(S(1,:));%元素連乘
%
cA   = C(1:isize);%cA  49152
cH  = C(isize+(1:isize));
cV  = C(2*isize+(1:isize));
cD  = C(3*isize+(1:isize));
%
cA   = reshape(cA,S(1,1),S(1,2));
cH  = reshape(cH,S(2,1),S(2,2));
cV  = reshape(cV,S(2,1),S(2,2));
cD  = reshape(cD,S(2,1),S(2,2));

    獲取結果

2. 對上面過程中獲取的圖像進行邊沿豎直方向檢測,再利用形態學的開閉運算擴大每個豎直方向比較豐富的區域。最后對不符合區域進行篩選。部分代碼:

I2=edge(cV,'sobel',thresh,'vertical');%根據所指定的敏感度閾值thresh,在所指定的方向direction上,
a1=imclearborder(I2,8); %8連通 抑制和圖像邊界相連的亮對象
se=strel('rectangle',[10,20]);%[10,20]
I4=imclose(a1,se);
st=ones(1,8); %選取的結構元素
bg1=imclose(I4,st); %閉運算
bg3=imopen(bg1,st); %開運算
bg2=imopen(bg3,[1 1 1 1]'); 
I5=bwareaopen(bg2,500);%移除面積小於2000的圖案
I5=imclearborder(I5,4); %8連通 抑制和圖像邊界相連的亮對象
 figure,imshow(I5);title('從對象中移除小對象');  

  獲取的篩選結果:

      我們接下來目的就是從這些待選區域中挑選出車牌區域。

3 這里我們就要利用一些關於車牌的先驗知識。2007 年實施的車牌標准規定,車前車牌長440mm,寬140mm。其比例為440 /140 ≈3.15  。根據圖像像素的大小,這里選取篩選條件為寬在70到250之間,高在15到70之間,同時寬高比例應大於0.45,就可以比較准確的得到車牌的大致位置。當然,這里寬高值也是根據你的圖像大小進行設置的,大小不一樣,值略有區別。    

%利用長寬比進行區域篩選
[L,num] = bwlabel(I5,8);%標注二進制圖像中已連接的部分,c3是形態學處理后的圖像
Feastats =regionprops(L,'basic');%計算圖像區域的特征尺寸
Area=[Feastats.Area];%區域面積
BoundingBox=[Feastats.BoundingBox];%[x y width height]車牌的框架大小
RGB = label2rgb(L,'spring','k','shuffle'); %標志圖像向RGB圖像轉換
     
lx=1;%統計寬和高滿足要求的可能的車牌區域個數
Getok=zeros(1,10);%統計滿足要求個數
for l=1:num  %num是彩色標記區域個數
width=BoundingBox((l-1)*4+3);
hight=BoundingBox((l-1)*4+4);
rato=width/hight;%計算車牌長寬比
%利用已知的寬高和車牌大致位置進行確定。
if(width>70 & width<250 & hight>15 & hight<70 &(rato>3&rato<8)&((width*hight)>Area(l)/2))%width>50 & width<1500 & hight>15 & hight<600 Getok(lx)=l; lx=lx+1; end end startrow=1;startcol=1; [original_hihgt original_width]=size(cA);

   當然,這個只是初步確定,經常出現的結果是好幾個待選區域都滿足要求。通過觀察它的直方圖發現車牌區域的直方圖多波峰波谷,變化大,而一般的區域變化較不明顯。因此根據它的方差,波峰波谷值數量來確定車牌區域。實驗結果表明效果還是挺不錯的。

for order_num=1:lx-1 %利用垂直投影計算峰值個數來確定區域
  area_num=Getok(order_num);
  startcol=round(BoundingBox((area_num-1)*4+1)-2);%開始列
  startrow=round(BoundingBox((area_num-1)*4+2)-2);%開始行  
  width=BoundingBox((area_num-1)*4+3)+2;%車牌寬
  hight=BoundingBox((area_num-1)*4+4)+2;%車牌高 
  uncertaincy_area=cA(startrow:startrow+hight,startcol:startcol+width-1); %獲取可能車牌區域
  image_binary=binaryzation(uncertaincy_area);%圖像二值化
  histcol_unsure=sum(uncertaincy_area);%計算垂直投影
   histcol_unsure=smooth(histcol_unsure)';%平滑濾波
      histcol_unsure=smooth(histcol_unsure)';%平滑濾波
      average_vertical=mean(histcol_unsure);
  figure,subplot(2,1,1),bar(histcol_unsure);
  subplot(2,1,2),imshow(mat2gray(uncertaincy_area));
  [data_1 data_2]=size(histcol_unsure);
  peak_number=0; %判斷峰值個數
  for j=2:data_2-1%判斷峰值個數
      if (histcol_unsure(j)>histcol_unsure(j-1))&(histcol_unsure(j)>histcol_unsure(j+1))
          peak_number=peak_number+1;
      end
  end
   valley_number=0; %判斷波谷個數
  for j=2:data_2-1
      if (histcol_unsure(j)<histcol_unsure(j-1))&(histcol_unsure(j)<histcol_unsure(j+1)) &(histcol_unsure(j)<average_vertical)
           %波谷值比平均值小
          valley_number=valley_number+1;
      end
  end
  %peak_number<=15
 if peak_number>=7 & peak_number<=18 &valley_number>=4 & (startcol+width/2)>=original_width/6 &(startcol+width/2)<=5*original_width/6....
     &(startrow+hight/2)>=original_hihgt/6 & (startrow+hight/2)<=original_hihgt*5/6
     %進一步確認可能區域
     select_unsure_area(count)=area_num;
     standard_deviation(count)=std2(histcol_unsure);%計算標准差
     count=count+1;
 end  
end
correct_num_area=0;
max_standard_deviation=0;
if(count<=2) %僅有一個區域
   
    correct_num_area=select_unsure_area(count-1);
else
    for  num=1:count-1
       if(standard_deviation(num)>max_standard_deviation)
           max_standard_deviation=standard_deviation(num);
         correct_num_area=select_unsure_area(num);
       end
    end   
end

獲取區域:

      定位大概就這么多。這里說明一下,上面一些參數和你的圖片規格有關系,不是你隨便拿來一張就可以識別的,要根據你的實際進行調整。大概就這么多,歡迎交流,轉載請注明出處,謝謝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM