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

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

    合着代碼進行相關講解。框架

   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. 對上面過程當中獲取的圖像進行邊沿豎直方向檢測,再利用形態學的開閉運算擴大每一個豎直方向比較豐富的區域。最後對不符合區域進行篩選。部分代碼:spa

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('從對象中移除小對象');  

  獲取的篩選結果:code

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

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

%利用長寬比進行區域篩選
[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

獲取區域:it

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

相關文章
相關標籤/搜索