汽車出出入庫計時計費系統的核心技術就是車牌識別系統,

圖一:車牌漢字字符模板
圖二:車牌英文及數字字符模板
圖三:系統車輛入庫界面

圖五:車輛車牌過遠識別失敗

圖六:車輛車牌識別成功

圖七:車牌在一定傾角范圍內識別也可以成功

圖七:可自定義計費規則

圖八:車輛未在庫內提示
車牌定位代碼:
function platerect = platelocation(I)
%carparkenter第113行呼叫
%carparkout第105行呼叫
%這個函式 定位車牌,
%I 原始影像
%platerect 回傳的車牌區域
platerect = [];
grayI=rgb2gray(I); %灰度影像
[height,width] = size(grayI);%新的影像尺寸
% SobelI = double(grayI);
hx = [-1 0 1;-2 0 2;-1 0 1];
gradx=filter2(hx,grayI,'same');
%hx為濾波器.grayI為要濾波的資料,這里將hx放在grayI上,一個一個移動進行模板濾波,'sama'表示從左上角開始;
gradx=abs(gradx); %計算影像的sobel水平梯度
%abs()計算的是數值的絕對值和復數的幅值
% figure
% imshow(gradx,[]);
% title('影像的sobel水平梯度');
hy = hx';
grady=filter2(hy,grayI,'same');
grady=abs(grady); %計算影像的sobel垂直梯度
% figure
% imshow(grady,[]);
% title('影像的sobel垂直梯度');
grad = gradx + 0.3*grady;
grad = uint8(mat2gray(grad)*255);
%函式mat2gray()可以把任意任意型別影像矩陣轉換為取值范圍為[0,1]的歸一化double類陣列
%uint8的范圍是0-255
% figure
% imshow(grad)
% title('綜合邊緣檢測');
level =graythresh(grad);
edgeI = im2bw(grad,level);
%函式graythresh使用最大類間方差法找到圖片的一個合適的閾值
%再利用im2bw(將灰度影像轉換為二值影像)函式,將找到的閾值輸入,就可以把原圖變為一個二值圖
% figure
% imshow(edgeI)
% title('邊緣二值化');
% I2=edge(I1,'sobel',0.1,'vertical');%水平方向sobel濾波
se=[1;1;1];
bwfilterIedge=imerode(edgeI,se); %腐蝕,洗掉雜點砸線
%imerode函式其中edgeI是待處理的影像,se是結構元素物件
se=strel('rectangle',[4,18]);
%創建一個寬4長18的矩形
bwfilterIedge=imclose(edgeI,se); %閉操作,其實就是先膨脹再腐蝕,使車牌區域連接起來
% figure
% imshow(bwfilterIedge)
% title('邊緣檢測形態學處理')
%顏色檢測
[h,s,v] = rgb2hsv(I);
%H引數表示色彩資訊,即所處的光譜顏色的位置,
%該引數用一角度量來表示,紅、綠、藍分別相隔120度,互補色分別相差180度,
%純度S為一比例值,范圍從0到1,它表示成所選顏色的純度和該顏色最大的純度之間的比率,
%S=0時,只有灰度, V表示色彩的明亮程度,范圍從0到1,有一點要注意:它和光強度之間并沒有直接的聯系
h = h.*180;
blueI = zeros(height,width);%白色檢測結果
%生成0矩陣
for i = 1:height
for j = 1:width
if (h(i,j) >= 100 && h(i,j) <= 124 && s(i,j) >= 0.25 && v(i,j) >= 0.3 ) %藍色
blueI(i,j) = 1;
end
end
end
blueI = logical(blueI);
%logical(x)將把x中的非0的值 變成1,把所有的數值0值變成邏輯0 ,
% figure
% imshow(blueI)
% title('藍色檢測')
se=strel('rectangle',[10,15]);
bwfilterIcolor=imclose(blueI,se); %閉操作,其實就是先膨脹再腐蝕,使車牌區域連接起來
% figure
% imshow(bwfilterIcolor)
% title('藍色檢測形態學處理')
%邊緣 &&顏色結合
bwfilterI = bwfilterIedge & bwfilterIcolor;
% figure
% imshow(bwfilterI)
% title('邊緣&&顏色檢測結合')
[L,num] = bwlabel(bwfilterI,8);%計算連通區域
% [L,num] = bwlabel(BW,n)這里num回傳的就是BW中連通區域的個數,n為4連通或8連通,num為連通區域個數
STATS = regionprops(L,'BoundingBox','Centroid','Orientation');%取得每個連通區域的特性
%regionprops即用來度量影像區域屬性的函式
fitstas = [];%適合的檢測目標
for i=1:num
sta = STATS(i);
if (sta.Orientation < -30 || sta.Orientation > 30)%判斷角度不能太大
%'Orientation':是標量,與區域具有相同標準二階中心矩的橢圓的長軸與x軸的交角(度),
%本屬性只支持二維標注矩陣,
continue
end
% disp(sta.MajorAxisLength)
% disp(sta.MinorAxisLength)
if checkplatesize(sta.BoundingBox(4),sta.BoundingBox(3)) == 1
%呼叫checkplatesize函式
%BoundingBox:是1行ndims(L)*2列的向量,即包含相應區域的最小矩形,
%BoundingBox 形式為 [ul_corner, width],
%這里 ul_corner 以 [x y z ...] 的坐標形式給出邊界盒子的左上角、
%boxwidth 以 [x_width y_width ...] 形式指出邊界盒子沿著每個維數方向的長度
fitstas = [fitstas;sta] ;
end
end
% figure
% imshow(I)
% for i=1:length(fitstas)
% sta = fitstas(i);
% rectangle('Position',sta.BoundingBox,'EdgeColor','r','LineWidth',2);%檢測的矩形框,藍色
% end
% title('可疑車牌檢測')
%選擇一個最合適的出來
beststa = selectbest(fitstas,bwfilterI);%選出的最佳
%呼叫selectbest函式
bdetect = 0;
% figure
% imshow(I)
if isempty(beststa)
bdetect = 0;
% title('沒檢測出車牌')
else
bdetect = 1;
% rectangle('Position',beststa.BoundingBox,'EdgeColor','r','LineWidth',2);%檢測的矩形框,藍色
% title('最終車牌檢測')
end
if bdetect == 1
platerect = beststa.BoundingBox;%車牌區域
% plateI = imcrop(I,beststa.BoundingBox);%截取出來
% % [pathstr, name, ext] = fileparts(strfullname);
% % imwrite(plateI,[name,'.bmp'])
% figure
% imshow(plateI)
% title('最終剪切后的車牌')
end
字符切割代碼:
function [charimglist,bwplateimg] = charcut(plateI)
%carparkerenter第127行呼叫
%carparkout第121行呼叫
%這個函式對車牌,進行分割
%plateI 是傳入的車牌圖片
%charimglist 存放著分割出的7個字符
%bwplateimg 車牌區域二值化等預處理后的圖
grayplateI = rgb2gray(plateI);%轉為灰度影像
grayplateI= imadjust(grayplateI,stretchlim(grayplateI));
%灰度變換,影像增強對比度
% figure
% imshow(grayplateI)
% title('車牌灰度影像')
th = graythresh(grayplateI);
%graythresh這個函式中,是使用最大類間方差法找到圖片的一個合適的閾值
d = im2bw(grayplateI,th);
%利用im2bw(將灰度影像轉換為二值影像)函式,將找到的閾值輸入,就可以把原圖變為一個二值圖
minarea = round(size(d,1)*size(d,2)*0.001);
%r=size(A,1)該陳述句回傳的時矩陣A的行數, c=size(A,2) 該陳述句回傳的時矩陣A的列數,
%round函式是一個四舍五入的函式
d=bwareaopen(d,minarea); %洗掉面積過小的區域
%洗掉二值影像d中面積小于minarea的物件,默認情況下使用8鄰域
%呼叫qiege.m
d = qiege(d);
% figure
% imshow(d)
% title('車牌二值影像')
theta=60:120;
% bw 表示需要變換的影像,theta 表示變換的角度
% 回傳值 r 表示的列中包含了對應于 theta中每一個角度的 Radon 變換,(每個角度下每個線歷經的白點數)
% 向量 xp 包含相應的沿 x軸的坐標
[r,xp]=radon(d,theta);
[value,index] = max(r(:));%找出白點最多的
[y,x]=ind2sub(size(r),index); %這個對應的偏移和角度
%ind2sub把陣列或者矩陣的線性索引轉化為相應的下標
x = theta(1)+x-1;
d=imrotate(d,90-x); % 旋轉影像
% figure
% imshow(d)
% title('旋轉后的車牌二值影像')
% 去除上下邊框
% STEP 1 黑白跳變小于閾值則被視為背景
% 上面 2/5
[m,n] = size(d);
y1=13; % y1: 跳變閾值
for i=1:round(m/5*2)
count=0;jump=0;temp=0;
for j=1:n
%這一次的點是temp,上一次的點是jump,然后比較是否跳變了,
if d(i,j)==1
temp=1;
else
temp=0;
end
if temp==jump
count=count;
else
count=count+1; %跳變加1
end
jump=temp; %保存這一次的值
end
if count<y1 %如果跳變很小,那么要去除
d(i,:)=0;
end
end
% 下面 2/5
for i=3*round(m/5):m
count=0;jump=0;temp=0;
for j=1:n
%這一次的點是temp,上一次的點是jump,然后比較是否跳變了,
if d(i,j)==1
temp=1;
else
temp=0;
end
if temp==jump
count=count;
else
count=count+1; %跳變加1
end
jump=temp;
end
if count<y1 %如果跳變很小,那么要去除
d(i,:)=0;
end
end
minarea = round(size(d,1)*size(d,2)*0.001);
d=bwareaopen(d,minarea); %洗掉面積過小的區域
d = qiege(d);
bwplateimg = d;
% figure
% imshow(d)
% title('去除上下邊界后')
%尋找連通區域
[L,num] = bwlabel(d,8);%計算連通區域
%回傳一個和d大小相同的L矩陣,包含了標記了d中每個連通區域的類別標簽,8連通
STATS = regionprops(L,'BoundingBox');%取得每個連通區域的特性
%存放字符矩形
vecRects = [];
for i=1:num
sta = STATS(i);
height = sta.BoundingBox(4);
%BoundingBox中的四個引數[x,y,width,height]
width = sta.BoundingBox(3);
charAspect= width/height;%寬高比
hratio = height/size(d,1);%和車牌的高度比例
if (charAspect>0.05 && charAspect<0.85 && hratio>0.5)
rect = sta.BoundingBox;
vecRects = [vecRects;rect];%存盤起來
end
end
xs = vecRects(:,1);%每個矩形的左x
[sortxs,sortindex] = sort(xs);%從小到大排序,也就是從左到右吧
%sortx值,sortindex索引
vecRects = vecRects(sortindex,:);%矩形重新排序
vecnum = size(vecRects,1);%矩形個數,n=1行尺寸,n=2列尺寸
specIndex = getspecificindex(vecRects,size(d,2));%尋找城市字符位置
%呼叫getspecificindex函式
rectSpe = vecRects(specIndex,:);%城市字符位置
chineserect = GetChineseRect(rectSpe);%漢字字符位置
%呼叫GetChineseRect函式
%重新構建下字符矩形位置
resultvecRects = chineserect;
for i = specIndex:vecnum %從城市字符后面找6個出來
r = vecRects(i,:);
resultvecRects = [resultvecRects;r];%存到resultvecRects內
if size(resultvecRects,1) >= 7 %最多總共7個矩形
break
end
end
% %遍歷顯示切割出的每個字符
% figure
% charnum = size(resultvecRects,1);
% for i=1:charnum
% charimg = imcrop(d,resultvecRects(i,:));
% subplot(1,charnum,i);
% imshow(charimg)
% strtitle = sprintf('%d',i);
% title(strtitle)
% end
% set(gcf,'name','字符分割結果')
charnum = size(resultvecRects,1);
charimglist = {};
for i=1:charnum
charimg = imcrop(d,resultvecRects(i,:));
%imcrop是一個函式,在MATLAB中,該函式用于回傳影像的一個裁剪區域
charimglist = [charimglist,charimg];%字符存到陣列內
% strname = sprintf('%d.bmp',i);
% imwrite(charimg,strname)
end
字符識別代碼:
function [platenum] = charrec(charimglist)
%carparkenter第134行呼叫
%carparkout第128行呼叫
% charimglist 7個字符圖串列
% platenum 識別的車牌
load ('../train/chinesechar.mat') %漢字識別
load ('../train/char.mat')%字母識別
load ('../train/charnum.mat') %字母數字識別
platenum = '';%車牌號碼
%第一個字
word1 = charimglist{1};
xx = getwordfeature(word1);
%呼叫getwordfeature()函式
samplenum = size(chinesetraindata,2);%樣本的個數
xx = repmat(xx,[1,samplenum]);%平鋪開samplenum列
err = chinesetraindata-xx;%相減,以便看誤差最小的
tmp = sum(abs(err));
[minvalue,minindex] = min(tmp);%找出誤差最小的那個
index = chinesetrainlabels(minindex);%最小的那個對應的類別
character = chinesecharnames{index};%找出對應的字符
% platenum(1) = character;%存入platenum中
platenum = sprintf('%s%s',platenum,character);
%第二個字
word2 = charimglist{2};
xx = getwordfeature(word2);
samplenum = size(chartraindata,2);%樣本的個數
xx = repmat(xx,[1,samplenum]);%平鋪開samplenum列
err = chartraindata-xx;%相減,以便看誤差最小的
tmp = sum(abs(err));
[minvalue,minindex] = min(tmp);%找出誤差最小的那個
index = chartrainlabels(minindex);%最小的那個對應的類別
character = charnames{index};%找出對應的字符
% platenum(2) = character;%存入platenum中
platenum = sprintf('%s%s',platenum,character);
%34567用字母數字分類器
for i=3:7
word = charimglist{i};
xx = getwordfeature(word);
samplenum = size(charnumtraindata,2);%樣本的個數
xx = repmat(xx,[1,samplenum]);%平鋪開samplenum列
err = charnumtraindata-xx;%相減,以便看誤差最小的
tmp = sum(abs(err));
[minvalue,minindex] = min(tmp);%找出誤差最小的那個
index = charnumtrainlabels(minindex);%最小的那個對應的類別
character = charnumnames{index};%找出對應的字符
% platenum(i) = character;%存入platenum中
platenum = sprintf('%s%s',platenum,character);
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/345584.html
標籤:其他
