車牌識別系統(tǒng)MATLAB源代碼完整解析_第1頁
車牌識別系統(tǒng)MATLAB源代碼完整解析_第2頁
車牌識別系統(tǒng)MATLAB源代碼完整解析_第3頁
車牌識別系統(tǒng)MATLAB源代碼完整解析_第4頁
車牌識別系統(tǒng)MATLAB源代碼完整解析_第5頁
已閱讀5頁,還剩12頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、clc; clear all; close all;filename, pathname, filterindex = uigetfile(*.jpg;*.tif;*.png;*.gif,All Image Files;. *.*,All Files , 選擇待處理圖像, . images01.jpg);file = fullfile(pathname, filename);%文件路徑和文件名創(chuàng)建合成完整文件名id = Get_Id(file);%得到file中的所有對象Img = imread(file);%根據(jù)路徑和文件名讀取圖片到ImgPlate, bw, Loc = Pre_Proce

2、ss(Img); % 車牌區(qū)域預(yù)處理result = Plate_Process(Plate, id); % 車牌區(qū)域二值化處理% 尋找連續(xù)有文字的塊,若長度大于某閾值,則認為該塊有兩個字符組成,需要分割bw = Segmation(result);words = Main_Process(bw); % 主流程處理Write_Mask(words, id); % 寫出到模板庫str = Pattern_Recognition(words); % 識別function id = Get_Id(file)% 獲取圖像id信息% 輸入?yún)?shù):% file圖像路徑% 輸出參數(shù):% id圖像id信息inf

3、o = imfinfo(file);FS = . 98686 . 80558 46208 69947 58110 62115 . 59072 52168 60457 53979 50223;id = find(FS = info.FileSize);if isempty(id) warndlg(未建立該圖像模板庫,可能運行出錯!, 警告); id = 1;endfunction R = Cubic_Spline(P)% 三次樣條插值% 輸入?yún)?shù):% P節(jié)點矩陣% 輸出參數(shù):% R樣條節(jié)點矩陣% 計算相鄰插值點之間的弦長chordlen = sqrt(sum(diff(P, , 1).2,2);

4、% 將弦長參數(shù)歸一化到0,1上chordlen = chordlen/sum(chordlen);% 計算每個插值節(jié)點處的累加弦長,作為給點處的參數(shù)cumarc = 0; cumsum(chordlen);x = cumarc;N = size(P, 1);R = ;% 以下部分為一元三次樣條插值的程序,對于空間三維數(shù)據(jù),以同樣的累加% 弦長作為參數(shù),對x,y,z分量做三次一元樣條插值得到的結(jié)果便是對三維數(shù)據(jù)% 做三次樣條插值for k = 1 : size(P, 2) y = P(:, k); m = zeros(1, N); M = zeros(1, N); n = m; d = m; A

5、 = eye(N); A = 2*A; m(1) = 1; n(N) = 1; m(N) = 1; n(1) = 1; for i = 2:N-1 m(i) = (x(i+1)-x(i)/(x(i+1)-x(i-1); n(i) = 1 - m(i); d(i) = 6*(y(i+1) - y(i)/(x(i+1) - x(i) - (y(i) - y(i-1)/(x(i) - x(i-1)/(x(i+1)-x(i-1); end for j = 1 : N-1 A(j, j+1) = m(j); A(j+1, j) = n(j+1); end p = A(2:N-1, 2:N-1); q =

6、 d(2:N-1); Q = inv(p)*q; M = zeros(1,N); M(1,1) = 0; M(1,N) = 0; M(1,2:N-1)= Q; S = ; temp = ; for i = 1:N-1 % 對每一個分量計算出來的插值曲線進行采樣,以便將其畫出。 s = 50;% 采樣點個數(shù) z = linspace(x(i),x(i+1),s); h = x(i+1)-x(i); for j = 1:length(z) S(j) = M(i)*(x(i+1)-z(j)3)/(6*h)+M(i+1)*(z(j)-x(i)3)/(6*h)+(y(i)-M(i)*(h2)/6)*(x

7、(i+1)-z(j)/h)+(y(i+1)-M(i+1)*h2/6)*(z(j)-x(i)/h); end temp = temp S; end R(:, k) = temp; endfunction mask = Get_PointSplineMask(Img, Ptn)% 獲取封閉有序節(jié)點的蒙板圖像% Img圖像矩陣% Ptn封閉有序節(jié)點% 輸出參數(shù):% mask蒙板圖像if ndims(Img) = 3 I = rgb2gray(Img);else I = Img;endmask = zeros(size(I);Ptn = Cubic_Spline(Ptn); % 樣條插值for i =

8、 1 : size(Ptn, 1)-1 pt1 = Ptn(i, :); % 線段起點 pt2 = Ptn(i+1, :); % 線段終點 x1 = pt1(1); y1 = pt1(2); x2 = pt2(1); y2 = pt2(2); % 直線段參數(shù) A = (y1 - y2)/(x1*y2 - x2*y1); B = (-x1 + x2)/(x1*y2 - x2*y1); % 直線段取點 xk = linspace(min(x1, x2), max(x1, x2); if B = 0 yk = linspace(min(y1, y2), max(y1, y2); else yk =

9、(-1 - A*xk)/B; end % 賦值操作 for j = 1 : length(xk) if isnan(round(yk(j) & isnan(round(xk(j) & . isinf(round(yk(j) & isinf(round(xk(j) & . round(yk(j) 0 & round(xk(j) 0 mask(round(yk(j), round(xk(j) = 1; end endendmask = logical(mask); % 類型轉(zhuǎn)換mask = bwmorph(mask, bridge); % 橋接操作mask = imfill(mask, hole)

10、; % 補洞操作function Im = Image_Rotate(Img, num, flag)% 旋轉(zhuǎn)校正函數(shù)% 輸入函數(shù):% Img圖像矩陣% num圖像序號% flag顯示圖像窗口% 輸出函數(shù):% Im結(jié)果圖像if nargin 3 flag = 0;endrole = 6 0 0 -13 5 10 0 10 0 -5 2 -1 2 2 0 -5 -2 0 6 2;Im = imrotate(Img, role(num), bilinear);if flag figure(2); subplot(1, 2, 1); imshow(Img); title(原圖像); subplot(1

11、, 2, 2); imshow(Im); title(旋轉(zhuǎn)圖像);endfunction words = Main_Process(bw, flag_display)% 主流程處理,分割字符并獲取% 輸入?yún)?shù):% bw車牌區(qū)域圖像% flag_display顯示圖像標(biāo)記% 輸出參數(shù):% words車牌字符數(shù)據(jù)if nargin 2 flag_display = 1;endm, n = size(bw);k1 = 1;k2 = 1;s = sum(bw); % 列積分投影j = 1; % 列游標(biāo)while j = n % 尋找車牌圖像左側(cè)邊界 while s(j) = 0 & j = n-1 j

12、 = j + 1; end k1 = j-1; % 車牌圖像左側(cè)邊界 % 尋找車牌圖像右側(cè)邊界 while s(j) = 0 & j Tol val, num = min(sum(bw(:, k1+5:k2-5); bw(:, k1+num+5)=0; % 抹去該字符 endend% 再切割bw = Segmation(bw);% 切割出 7 個字符m, n = size(bw);wideTol = round(n/20); % 區(qū)域?qū)挾茸钚〖s束rateTol = 0.25; % 中心區(qū)域比值約束flag = 0;word1 = ;while flag = 0 m, n = size(bw);

13、 left = 1; wide = 0; % 找到空隙位置 while sum(bw(:,wide+1) = 0 wide = wide+1; end if wide rateTol flag = 1; word1 = temp; %提取WORD 1 end bw(:, 1:wide) = 0; % 抹去已處理的區(qū)域 bw = Segmation(bw); endend% 分割出第二個字符word2, bw = Word_Segmation(bw);% 分割出第三個字符word3, bw = Word_Segmation(bw);% 分割出第四個字符word4, bw = Word_Segma

14、tion(bw);% 分割出第五個字符word5, bw = Word_Segmation(bw);% 分割出第六個字符word6, bw = Word_Segmation(bw);% 分割出第七個字符word7, bw = Word_Segmation(bw);wid = size(word1, 2) size(word2, 2) size(word3, 2) . size(word4, 2) size(word5, 2) size(word6, 2) size(word7, 2);maxwid, indmax = max(wid);maxwid = maxwid + 10;wordi =

15、word1;wordi = zeros(size(wordi, 1), round(maxwid-size(word1, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word1, 2)/2);word1 = wordi;wordi = word2;wordi = zeros(size(wordi, 1), round(maxwid-size(word2, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word2, 2)/2);word2 = wordi;wordi = word

16、3;wordi = zeros(size(wordi, 1), round(maxwid-size(word3, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word3, 2)/2);word3 = wordi;wordi = word4;wordi = zeros(size(wordi, 1), round(maxwid-size(word4, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word4, 2)/2);word4 = wordi;wordi = word5;wo

17、rdi = zeros(size(wordi, 1), round(maxwid-size(word5, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word5, 2)/2);word5 = wordi;wordi = word6;wordi = zeros(size(wordi, 1), round(maxwid-size(word6, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word6, 2)/2);word6 = wordi;wordi = word7;wordi

18、= zeros(size(wordi, 1), round(maxwid-size(word7, 2)/2) wordi zeros(size(wordi, 1), round(maxwid-size(word7, 2)/2);word7 = wordi;% figure(1);% subplot(2, 7, 1); imshow(word1); title(字符1);% subplot(2, 7, 2); imshow(word2); title(字符2);% subplot(2, 7, 3); imshow(word3); title(字符3);% subplot(2, 7, 4); im

19、show(word4); title(字符4);% subplot(2, 7, 5); imshow(word5); title(字符5);% subplot(2, 7, 6); imshow(word6); title(字符6);% subplot(2, 7, 7); imshow(word7); title(字符7);% 切割出的字符歸一化大小為 40*20,此處演示word11 = imresize(word1, 40 20);word21 = imresize(word2, 40 20);word31 = imresize(word3, 40 20);word41 = imresize

20、(word4, 40 20);word51 = imresize(word5, 40 20);word61 = imresize(word6, 40 20);word71 = imresize(word7, 40 20);% subplot(2, 7, 8); imshow(word11); title(字符1);% subplot(2, 7, 9); imshow(word21); title(字符2);% subplot(2, 7, 10); imshow(word31); title(字符3);% subplot(2, 7, 11); imshow(word41); title(字符4)

21、;% subplot(2, 7, 12); imshow(word51); title(字符5);% subplot(2, 7, 13); imshow(word61); title(字符6);% subplot(2, 7, 14); imshow(word71); title(字符7);% 賦值操作words.word1 = word11;words.word2 = word21;words.word3 = word31;words.word4 = word41;words.word5 = word51;words.word6 = word61;words.word7 = word71;if

22、 flag_display figure; subplot(2, 7, 1); imshow(word1); title(字符1, FontWeight, Bold); subplot(2, 7, 2); imshow(word2); title(字符2, FontWeight, Bold); subplot(2, 7, 3); imshow(word3); title(字符3, FontWeight, Bold); subplot(2, 7, 4); imshow(word4); title(字符4, FontWeight, Bold); subplot(2, 7, 5); imshow(w

23、ord5); title(字符5, FontWeight, Bold); subplot(2, 7, 6); imshow(word6); title(字符6, FontWeight, Bold); subplot(2, 7, 7); imshow(word7); title(字符7, FontWeight, Bold); subplot(2, 7, 8); imshow(word11); title(字符1, FontWeight, Bold); subplot(2, 7, 9); imshow(word21); title(字符2, FontWeight, Bold); subplot(2

24、, 7, 10); imshow(word31); title(字符3, FontWeight, Bold); subplot(2, 7, 11); imshow(word41); title(字符4, FontWeight, Bold); subplot(2, 7, 12); imshow(word51); title(字符5, FontWeight, Bold); subplot(2, 7, 13); imshow(word61); title(字符6, FontWeight, Bold); subplot(2, 7, 14); imshow(word71); title(字符7, Fon

25、tWeight, Bold);endfunction mask = Mask_Process(Img, id)% 圖像蒙版處理函數(shù)% 輸入?yún)?shù):% Img圖像矩陣% id圖像序號% 輸出參數(shù):% mask模板圖像% 如果已經(jīng)存在模板圖像則不再取模板filename = sprintf(maskmask%d.jpg, id);if exist(filename, file) mask = imread(filename); if isa(mask, logical) mask = im2bw(mask); end sz = size(Img); if isequal(sz(1:2), size(

26、mask) mask = imresize(mask, sz(1:2); mask = logical(mask); end return;endI = Img;Ptn = ;figure;subplot(1, 3, 1); imshow(I);title(取點_左鍵取點,右鍵退出, Color, r, . FontWeight, Bold);hold on;set(gcf, units, normalized, position, 0 0 1 1);x, y, button = ginput(1); % 點擊while button = 1 plot(x, y, r+, LineWidth,

27、 2); % 繪制節(jié)點 Ptn = Ptn; x y; % 存儲節(jié)點 x, y, button = ginput(1); % 點擊endif size(Ptn, 1) 2 return;endPtn = Ptn; Ptn(1, :);plot(Ptn(:, 1), Ptn(:, 2), ro-, LineWidth, 2, MarkerFaceColor, k);title(原圖像, Color, k, . FontWeight, Bold);mask = Get_PointSplineMask(I, Ptn); % 獲取蒙板圖像subplot(1, 3, 2); imshow(mask);t

28、itle(蒙板圖像, Color, k, . FontWeight, Bold);if ndims(I) = 3 I1 = I .* uint8(cat(3, mask, mask, mask);else if isequal(size(I), size(mask) mask = imresize(mask, size(I); mask = logical(mask); end I1 = I .* mask;endsubplot(1, 3, 3); imshow(I1);title(蒙板分割結(jié)果, Color, k, . FontWeight, Bold);imwrite(mask, file

29、name);function str = Pattern_Recognition(words)% 車牌字符識別% 輸入?yún)?shù):% words車牌字符圖像% 輸出參數(shù):% str識別結(jié)果pattern = ;dirpath = ./標(biāo)準(zhǔn)庫/;%讀取所有樣本文件files = ls(dirpath *.jpg);for t = 1 : length(files) imagedata = imread(dirpath, files(t,:); %讀取樣本文件 imagedata = im2bw(imagedata, 0.5); pattern(t).feature = imagedata; end %

30、 字符庫列表chepaiword=. 滬 C E N 2 6 7 浙 B T 2 0 0 8 . 皖 N 6 9 3 3 5 皖 A M 3 7 6 7 . 滬 B 9 1 2 1 3 滬 F S 2 6 9 5 . 吉 A E 3 1 2 7 滬 B B 3 3 9 3 . 滬 J B 7 7 7 8 京 H Z 9 1 3 9 . 晉 K C 3 8 4 1 蘇 D V 0 0 0 1 . 蘇 A V 8 8 8 8 浙 B 8 8 8 8 8 . 滬 F Q 1 2 7 7 蘇 E D 0 0 0 9 . 滬 E C 1 2 8 8 蘇 G B 0 5 7 8 . 滬 B B 2 0 2

31、 9 滬 D 5 4 2 2 6; for m = 1 : 7; for n = 1 : length(files); switch m case 1 distance(n)=sum(sum(abs(words.word1-pattern(n).feature); case 2 distance(n)=sum(sum(abs(words.word2-pattern(n).feature); case 3 distance(n)=sum(sum(abs(words.word3-pattern(n).feature); case 4 distance(n)=sum(sum(abs(words.wo

32、rd4-pattern(n).feature); case 5 distance(n)=sum(sum(abs(words.word5-pattern(n).feature); case 6 distance(n)=sum(sum(abs(words.word6-pattern(n).feature); case 7 distance(n)=sum(sum(abs(words.word7-pattern(n).feature); end end yvalue,xnumber=min(distance); filename = files(xnumber, :); pathstr, name,

33、ext = fileparts(filename); result(m) = chepaiword(str2num(name);endstr = 識別結(jié)果為: result;msgbox(str, 車牌識別, modal); str = result;function result = Plate_Process(plate, id, flag)% 車牌區(qū)域處理% 輸入?yún)?shù):% plate車牌區(qū)域% id車牌序號% flag顯示圖像標(biāo)記% 輸出參數(shù):% result結(jié)果圖像if nargin 3 flag = 1;endif ndims(plate) = 3 plate1 = rgb2gray

34、(plate);% figure(1)% imshow(plate1)else plate1 = plate;endIm = Image_Rotate(plate1, id);th = 0.85 0.50 0.85 0.70 0.30 . 0.40 0.65 0.45 0.45 0.65 . 0.51 0.80 0.40 0.75 0.60 . 0.55 0.40 0.40 0.76 0.72;bw = im2bw(Im, th(id); % 車牌區(qū)域二值化h = fspecial(average, 2); % 均值濾波模板bw1 = imfilter(bw, h, replicate); %

35、 均值濾波% figure(1)% imshow(bw1)mask = Mask_Process(bw1, id); % 模板處理bw2 = bw1 .* mask; % 模板濾波result = bw2;if flag figure; subplot(2, 2, 1); imshow(plate); title(車牌區(qū)域圖像, FontWeight, Bold); subplot(2, 2, 2); imshow(Im); title(車牌區(qū)域校正圖像, FontWeight, Bold); subplot(2, 2, 3); imshow(bw1); title(車牌區(qū)域二值圖像, Fon

36、tWeight, Bold); subplot(2, 2, 4); imshow(bw2); title(濾波二值圖像, FontWeight, Bold);endfunction Plate, bw, Loc = Pre_Process(Img, parm, flag)% 車牌圖像預(yù)處理,提取車牌區(qū)域% 輸入?yún)?shù):% Img圖像矩陣% parm參數(shù)向量% flag是否顯示處理結(jié)果% 輸出參數(shù):% Plate分割結(jié)果if nargin 3 flag = 1;endif nargin 900 parm = 0.35 0.9 90 0.35 0.7 90 2; end if size(Img, 2

37、) 700 & size(Img, 2) 500 & size(Img, 2) 700 parm = 0.5 0.54 50 0.6 0.7 50 3; end if size(Img, 2) 800 rate = 800/y; I = imresize(I, rate);endy, x, z = size(I); % y方向?qū)?yīng)行、x方向?qū)?yīng)列、z方向?qū)?yīng)深度myI = double(I); % 數(shù)據(jù)類型轉(zhuǎn)換bw1 = zeros(y, x);bw2 = zeros(y, x);Blue_y = zeros(y, 1);% 對每一個像素進行分析,統(tǒng)計滿足條件的像素所在的行對應(yīng)的個數(shù)for i

38、 = 1 : y for j = 1 : x rij = myI(i, j, 1)/(myI(i, j, 3)+eps); gij = myI(i, j, 2)/(myI(i, j, 3)+eps); bij = myI(i, j, 3); % 藍色RGB的灰度范圍 if (rij parm(1) & gij parm(3) . | (gij parm(1) & rij parm(3) Blue_y(i, 1) = Blue_y(i, 1) + 1; % 藍色象素點統(tǒng)計 bw1(i, j) = 1; end endend% Y方向車牌區(qū)域確定temp, MaxY = max(Blue_y);T

39、h = parm(7);% 向上追溯,直到車牌區(qū)域上邊界PY1 = MaxY;while (Blue_y(PY1,1)Th) & (PY11) PY1 = PY1 - 1;end% 向下追溯,直到車牌區(qū)域下邊界PY2 = MaxY;while (Blue_y(PY2,1)Th) & (PY2y) PY2 = PY2 + 1;end% 對車牌區(qū)域的修正PY1 = PY1 - 2;PY2 = PY2 + 2;if PY1 y PY2 = y;end% 得到車牌區(qū)域IY = I(PY1:PY2, :, :);% X 方向 % 進一步確定X方向的車牌區(qū)域Blue_x = zeros(1,x);for

40、j = 1:x for i = PY1:PY2 rij = myI(i, j, 1)/(myI(i, j, 3)+eps); gij = myI(i, j, 2)/(myI(i, j, 3)+eps); bij = myI(i, j, 3); % 藍色RGB的灰度范圍 if (rij parm(4) & gij parm(6) . | (gij parm(4) & rij parm(6) Blue_x(1,j) = Blue_x(1,j) + 1; % 藍色象素點統(tǒng)計 bw2(i, j) = 1; end endend% 想右追溯,直到找到車牌區(qū)域左邊界PX1 = 1;while (Blue_

41、x(1,PX1)Th) & (PX1x) PX1 = PX1 + 1;end% 想左追溯,直到找到車牌區(qū)域右邊界PX2 = x;while (Blue_x(1,PX2)PX1) PX2 = PX2 - 1;end% 對車牌區(qū)域的修正PX1 = PX1 - 2;PX2 = PX2 + 2;if PX1 x PX2 = x;end% 得到車牌區(qū)域IX = I(:, PX1:PX2, :);% 分割車牌區(qū)域Plate = I(PY1:PY2, PX1:PX2, :);Loc.row = PY1 PY2;Loc.col = PX1 PX2;bw = bw1 + bw2;bw = logical(bw)

42、;bw(1:PY1, :) = 0;bw(PY2:end, :) = 0;bw(:, 1:PX1) = 0;bw(:, PX2:end) = 0;if flag figure; subplot(2, 2, 3); imshow(IY); title(行過濾結(jié)果, FontWeight, Bold); subplot(2, 2, 1); imshow(IX); title(列過濾結(jié)果, FontWeight, Bold); subplot(2, 2, 2); imshow(I); title(原圖像, FontWeight, Bold); subplot(2, 2, 4); imshow(Pla

43、te); title(分割結(jié)果, FontWeight, Bold);endfunction e = Segmation(d)% 車牌圖像切割% 輸入?yún)?shù):% d車牌圖像% 輸出參數(shù):% e切割后的車牌圖像m, n = size(d);% 初始化參數(shù)top = 1; % 矩形框上參數(shù)bottom = m; % 矩形框下參數(shù)left = 1; % 矩形框左參數(shù)right = n; % 矩形框右參數(shù) % 尋找矩形框上參數(shù)while sum(d(top,:)=0 & top=1 bottom = bottom - 1;end% 尋找矩形框左參數(shù)while sum(d(:,left)=0 & left=1 right = right - 1;enddd = right - left; % 矩形框長hh = bottom - top; % 矩形

溫馨提示

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

最新文檔

評論

0/150

提交評論