车牌识别系统MATLAB源代码完整.docx

上传人:李医生 文档编号:11662074 上传时间:2021-08-28 格式:DOCX 页数:22 大小:25.29KB
返回 下载 相关 举报
车牌识别系统MATLAB源代码完整.docx_第1页
第1页 / 共22页
车牌识别系统MATLAB源代码完整.docx_第2页
第2页 / 共22页
车牌识别系统MATLAB源代码完整.docx_第3页
第3页 / 共22页
车牌识别系统MATLAB源代码完整.docx_第4页
第4页 / 共22页
车牌识别系统MATLAB源代码完整.docx_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《车牌识别系统MATLAB源代码完整.docx》由会员分享,可在线阅读,更多相关《车牌识别系统MATLAB源代码完整.docx(22页珍藏版)》请在三一文库上搜索。

1、cic; clear all; close all;filename, pathname, filterindex = uigetfile(,*.jpg;*.tif;*.png;*.gif/AII Image Files;TAII Files),1选择待处理图像: images01.jpg);file = fullfile(pathname, filename);%文件路径和文件名创建合成完整文件名id = Get_ld(fUe);%得到file中的所有对象Img = imread(file);%根据路径和文件名读取图片到ImgPlate, bw, Loc = Pre_Process(lmg)

2、; % 车牌区域预处理 result = Plate_Process(Plate, id); % 车牌区域二值化处理%寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组 成,需要分割 bw = Segmation(result);words = Main_Process(bw); % 主流程处理 Write_Mask(words, id); % 写出到模板 库 str = Pattern_Recognition(words); % 识别function id = Get_ld(file)%获取图像id信息%输入参数:% file图像路径%输出参数:% id图像id信息info = i

3、mfinfo(file);FS = 422227 354169 293184 235413 214202 . 130938 490061 120297 98686137193 . 80558 46208 69947 58110 62115 . 59072 52168 60457 53979 50223;id = find(FS = info.FileSize);if isempty(id) warndlgC未建立该图像模板库,可能运行出错! 警告,);id = 1;end function R = Cubic_Spline(P) %三次样条插值%输入参数:% P节点矩阵%输出参数:%R样条节点

4、矩阵% 计算相邻插值点之间的弦长 chordlen = sqrt(sum(diff(P, , 1 )/2,2);% 将弦长参数归一化至lj 0,1上 chordlen = chordlen/sum(chordlen);%计算每个插值节点处的累加弦长,作为给点处的参数cumarc = 0; cumsum(chordlen);x = cumarc;N = size(P, 1);R = ;%以下部分为一元三次样条插值的程序,对于空间三维数据,以同样的累 加%弦长作为参数,对X, y, z分量做三次一元样条插值得到的结果便是对 三维数据做三次样条插值for k = 1 : size(P, 2)y =

5、P(:, k); m = zeros(1, N);M = zeros(1, N);n = m;d = m;A = eye(N);A = 2*A; m=1; n(N) = 1; m(N) = 1;n(1) = 1;fori = 2:N-1m(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-1A(j,j+1) = m(j);A(j+1,j) =

6、n(j+1);endp = A(2:N-1,2:N-1);q = 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;%采样点个数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)A3)/(6*h)+M(i+1 )*(z0)-x(i)A3)/(6*h)+

7、(y(i)-M(i)*(hA2)/6)*(x(i+1 )- z(j)/h)+( y(i+1)-M(i+1)*hA2/6)*(z(j)-x(i)/h);endtemp = temp S;endR(:, k) = temp;end function mask = Get_PointSplineMask(lmg, Ptn)%获取封闭有序节点的蒙板图像% Img图像矩阵% Ptn封闭有序节点%输出参数:% mask蒙板图像if ndims(lmg) = 3I = rgb2gray(lmg);elseI = Img;endmask = zeros(size(l);Ptn = Cubic_Spline(P

8、tn); % 样条插值for i = 1 : size(Ptn, 1)-1pt1 =Ptn(i, :);% 线段起点pt2 = Ptn(i+1,:);% 线段终点 x1 =pt1(1);y1 =pt1(2);X2 = pt2(1);y2 = pt2(2);%直线段参数A = (y1 - y2)/(x1 *y2 - x2*y1);B = (-x1 + x2)/(x1*y2 - x2*y1);%直线段取点xk = Iinspace(min(x1, x2), max(x1, x2);if B = 0yk = Iinspace(min(y1, y2), max(y1, y2); elseyk = (-

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;endendendmask = logical(mask); % 类型转换mask = bwmorph(mask, bridge); % 桥接操作mask = imfill(mask, hole); % 补

10、洞操作function Im = lmage_Rotate(lmg3 num, flag)%旋转校正函数%输入函数:% Img图像矩阵% num图像序号% flag显示图像窗口%输出函数:% Irn 结果图像if nargin 3flag = 0;endrole = 60 0 -135 100 100 -52-1 220-5-2 062;Im = imrotate(lmg, role(num), bilinear);if flagfigure(2);subplot(1,2, 1); imshow(lmg); title。原图像);subplot(1,2, 2); imshow(lm); tit

11、le(, 旋转图像,);end function words = Main_Process(bw, flag_display)%主流程处理,分割字符并获取%输入参数:%bw车牌区域图像% flag_display显示图像标记%输出参数:% words车牌字符数据if nargin 2 flag_display = 1;endm, n = size(bw);k1 = 1;k2 = 1;s = sum(bw); %列积分投影j = 1;%列游标while j = n%寻找车牌图像左侧边界while s(j) = 0 & j = n-1j=j + 1; endId =w;%车牌图像左侧边界%寻找车牌

12、图像右侧边界while s(j) = 0 & j Tolval, 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); %区域宽度最小约束rateTol = 0.25; %中心区域比值约束flag = 0; wordl =;while flag = 0m, n = size(bw);left = 1;wide = 0;%找到空隙位置while sum(bw(:,wide+1) = 0

13、wide = wide+1;endif wide rateTolflag = 1;wordl = temp; %提取 WORD 1endbw(:, 1 :wide) = 0; %抹去已处理的区域bw = Segmation(bw);endend%分割出第二个字符word2, bw = Word_Segmation(bw);%分割出第三个字符word3, bw = Word_Segmation(bw);%分割出第四个字符word4, bw = Word_Segmation(bw);%分割出第五个字符word5, bw = Word_Segmation(bw);%分割出第六个字符word6, bw

14、 = 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 = wordl;wordi = zeros(size(wordi, 1), round(maxwid-size(word1,2)/2) word

15、i zeros(size(wordi, 1),round(maxwid-size(word1,2)/2);wordl = 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 = word3;wordi = zeros(size(wordi, 1), round(maxwid-size(word3, 2)/2) wordi zero

16、s(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(word45 2)/2);word4 = wordi;wordi= word5;wordi = zeros(size(wordi, 1), round(maxwid-size(word5, 2)/2) wordi zeros(siz

17、e(wordi, 1) round(maxwid-size(word5, 2)/2);wo rd 5 = 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);wo rd 6r=wordi;wordi= word7;wordi = zeros(size(wordi, 1), round(maxwid-size(word7, 2)/2) wordi zeros(size(wor

18、di, 1) round(maxwid-size(word75 2)/2);word? = wordi;% figure(1);% subplot(2, 7, 1); imshow(wordl); title(1 字符 1);% subplot(2, 7, 2); imshow(word2); titlef 字符2);% subplot(2, 7, 3); imshow(word3); title(字符3); % subplot(2,7, 4);imshow(word4); title(字符4);% subplot(2, 7, 5); imshow(word5); title(字符5); %

19、subplot(2,7, 6);imshow(word6); title。字符6);% subplot(2, 7, 7); imshow(word7); titlef 字符7); % 切割出的字符归一化大小为 40*20,此处演示wordi 1 = imresize(word1, 40 20);word21 = imresize(word2, 40 20); word31 = imresize(word3, 40 20); word41 = imresize(word4, 40 20); word51 = imresize(word5, 40 20); word61 = imresize(wo

20、rd6, 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();% subplot(2, 7, 12); imshow(word51); title(字符5); % subp

21、lot(2, 7, 13); imshow(word61); title(字符61); % subplot(2, 7, 14); imshow(word71); title(字符71); % 赋值操作 words.word 1 = word 11;words.word2 = word21;words.word3 = word31;words.word4 = word41;words.word5 = word51;words.word6 = word61;words.word7 = word71;if flag_displayfigure;subplot(2, 7, 1); imshow(wor

22、dl); title(1 字符 TontWeight1,subplot(2, 7, 2); imshow(word2); title( Bold);字符 2,subplot(2, 7, 3); imshow(word3);也世孽即犯秘酣而,Bold);subplot(2, 7, 4); imshow(word4); title(f 字符FontWeight, Bold);subplot(2, 7, 5); imshow(word5); title(f 字符FontWeight, Bold);subplot(2, 7, 6); imshow(word6); title。字符-JFontWeigh

23、t, Bold);subplot(2, 7, 7); imshow(word7); title(字符 T FontWeight, Bold);subplot(2, 7, 8); imshow(word11); title(字符 1, TontWeight, Bold);subplot(2, 7, 9); imshow(word21); subpl家符州火曲旅1);title(* subplot(2, 7, 11); imshow(word41);字符;3?福限俺陕彼牖);14); imshow(word71); title( endimshow(word51); title(* subplot

24、(2, 7, 13); imshovord6,1)F&H静胡|双);5 ,FontWeight, Bold*);6 FontWeight, Bold);7 FontWeight, Bold1);function mask = Mask_Process(lmg, id)%图像蒙版处理函数%输入参数:% Img图像矩阵% id图像序号%输出参数:% mask模板图像 %如果已经存在模板图像则不再取 模板 filename = sprintf(maskmask%d.jpg, id); if exist(filename, file1)mask = imread(filename);if -isa(m

25、ask, logical1)mask = im2bw(mask);endsz = size(lmg);if isequal(sz(1:2), size(mask)mask = imresize(mask, sz(1:2);mask = logical(mask);end return;end I = Img; Ptn =;figure;subplot(1,3, 1); imshow(l);讹le(,取点 J左键取点,右键退出, Color, r . FontWeight; Bold);hold on;set(gcf, units, normalized*, position, 0 0 1 1)

26、; x, y, button = ginput(1); % 点击while button = 1 plot(x, y, 7+*, LineWidth*, 2); % 绘制节点 Ptn = Ptn; x y; % 存储节点x, y, button = ginput(1); % 点击 end if size(Ptn, 1) 2 return;endPtn = Ptn; Ptn(1, :);plot(Ptn(:, 1), Ptn(:, 2), ro- LineWidth*, 2, MarkerFaceColor1, *k); title(, 原图像Color, *k, . FontWeight, B

27、old1);mask = Get_PointSplineMask(l, Ptn); % 获取蒙板图像 subplot(1,3, 2); imshow(mask);也做蒙板图像Color, k . FontWeight, Bold);if ndims(l) = 311 = I .* uint8(cat(3, mask, mask, mask);elseif isequal(size(l), size(mask) mask = imresize(mask, size(l); mask = logical(mask);end11=1.* mask;endsubplot(1,3, 3); imshow

28、(H); title,蒙板分割结果Color, k, .FontWeight, Bold1);imwrite(mask, filename);function str = Pattern_Recognition(words)%车牌字符识别%输入参数:% words车牌字符图像%输出参数:% str识另胞吉果 pattern =;dirpath = 1./ 标准库 /1; %读取所有样本文件 files = ls(dirpath *Jpg);for t = 1 : length(files)imagedata = imread(dirpath, files(t,:); % 读取样本文件 imag

29、edata = im2bw(imagedata, 0.5);pattern(t).feature = imagedata;end% 字符库列表 chepaiword=.沪,C E N 2 6 7浙,B T 2 O O 8 皖 N 6 9 3 3 5,皖AM 3 7 6 7 沪,B 9 T 2 T 3,沪一F S 2 6 9 5 吉,A 3 T 2 7沪,B B 3 3 9 3 沪 j B,7 7 7 8,京,H Z 9 T 3 9 晋,K C 3 8 4 T,苏一V O O O T 苏 TV V 8 8 8 浙,B 8 8 8 8 8 沪 F Q ” 2 7 71 苏,E D O O。9 沪,

30、E c ” 2 8 ,8 * 苏,G B O 5 7 8 沪,B B 2 O 2 9,沪D,5 4 2 2 6;for m = 1 : 7;for n = 1 : length(files); switch mcase 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

31、 distance(n)=sum(sum(abs(words.word4-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);endend yvalue,xnumber=min(distance); filename = files(

32、xnumber, :); pathstr, name, ext=fileparts(filename); result(m) = chepaiword(str2num(name);endstr =识别结果为:result; msgbox(strj车牌识别丁modal);str = result;function result = Plate_Process(plate, id, flag) % 车牌区域处理%输入参数:% plate车牌区域% id车牌序号% flag显示图像标记%输出参数:% result结果图像if nargin 3flag = 1;endif ndims(plate) =

33、 3platel = rgb2gray(plate);%figure(1)%imshow(platel)elseplatel = plate;endIm = lmage_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(lm, th(id); % 车牌区域二值化h = fspecial(average1, 2); % 均值滤波模板bw1 = imfilter(bw, h,

34、 replicate); % 均值滤波% figure(1)% imshow(bwl)mask = Mask_Process(bw1, id); % 模板处理bw2 = bw1 .* mask; % 模板滤波 result = bw2;if flagfigure;subplot(2, 2, 1); imshow(plate); title(车牌区域图像FontWeight, Bold*); subplot(2, 2, 2);imshow(lm); title(1 车牌区域校正图像 1, TontWeight, Bold);subplot 2, 3); imshow(bwl); titlef 车

35、牌区域二值图像 , FontWeight, Bold);subplot(2, 2, 4); imshow(bw2); titlef 滤波二值图像FontWeight, Bold); endfunction Plate, bw, Loc = Pre_Process(lmg, parm, flag)%车牌图像预处理,提取车牌区域%输入参数:% Img图像矩阵% parm参数向量% flag是否显示处理结果%输出参数:% Plate分害U结果if nargin 3flag = 1;endif nargin 900parm = 0.35 0.9 90 0.35 0.7 90 2;endif size(

36、lmg, 2) 700 & size(lmg, 2) 500 & size(lmg, 2) 700parm = 0.5 0.54 50 0.6 0.7 50 3;endif size(lmg, 2) 800rate = 800/y;I = imresize(l, rate);endy, x, z = size(l); % y方向对应行、x方向对应列、z方向对应深度myl = double(l); %数据类型转换bw1 = zeros(y, x);bw2 = zeros(y, x);Blue_y = zeros(y, 1);%对每一个像素进行分析,统计满足条件的像素所在的行对应的个数fori =

37、 1 :yfor j = 1 :xrij = myl(i,j, 1)/(myl(i, j, 3)+eps);gij = myl(i, j, 2)/(myl(i, j, 3)+eps);bij = myl(i, j, 3);%蓝色RGB的灰度范围if (rij parm(1) & gij parm(3).| (gij parm & rij parm(3) Blue_y(i, 1) = Blue_y(i, 1) + 1; %蓝色象素点统计bw1(i, j) = 1;endendend%丫方向车牌区域确定temp, MaxY = max(Blue_y);Th = parm(7);%向上追溯,直到车牌

38、区域上边界PY1 = MaxY;while (Blue_y(PY1,1)Th) & (PY11)PY1 =PY1 -1;end%向下追溯,直到车牌区域下边界PY2 = MaxY;while (Blue_y(PY2,1)Th) & (PY2y)PY2 = PY2 + 1;end%对车牌区域的修正PY1 = PY1 - 2;PY2 = PY2 + 2;if PY1 yPY2 = y;end%得到车牌区域IY = l(PY1:PY2,:);%X 方向 %进一步确定x方向的车牌区域Blue_x = zeros(1 ,x);for j = 1 :xfori = PY1 :PY2rij = myl(i,

39、j, 1)/(myl(i, j, 3)+eps);gij = myl(i, j, 2)/(myl(i, j, 3)+eps);bij = myl(i, j, 3);%蓝色RGB的灰度范围if (rij parm(4) & gij parm(6).| (gij parm & rij parm(6) Blue_x(1 J) = Blue_x(1 ,j) + 1; %蓝色象素点统计bw2(i, j) = 1;endendend%想右追溯,直到找到车牌区域左边界PX1 = 1;while (Blue_x(1,PX1)Th) & (PX1x)PX1 =PX1 +1;end%想左追溯,直到找到车牌区域右边

40、界PX2 = X;while (Blue_x(1,PX2)PX1)PX2 = PX2- 1;end%对车牌区域的修正PX1 = PX1 2;PX2 = PX2 + 2;if PX1 xPX2 = x;end%得到车牌区域IX = l(:, PX1:PX2,:);%分割车牌区域Plate = l(PY1:PY2, PX1:PX2,:);Loc.row = PY1 PY2;Loc.col = PX1 PX2; bw = bw1 + bw2; bw = logical(bw); bw(1:PY1, :) = 0; bw(PY2:end, :) = 0;bw(:, 1:PX1) = 0;bw(:, P

41、X2:end) = 0;if flagfigure;subplot 2, 3); imshow(IY); title(, 行过滤结果 TontWeight, Bold); subplot(2, 2, 1); imshow(IX); title。列过滤结果FontWeight, Bold); subplot(2, 2, 2); imshow(l); title(原图 像FontWeight丁Bold);subplot(2, 2, 4); imshow(Plate); title(, 分割结果FontWeight, Bold); end function e = Segmation(d) %车牌图像切割%输入参数:%d车牌图像%输出参数:%e切割后的车牌图像m, n = size(d);%初始化参数top = 1; %矩形框上参数bottom = m; %矩形框下参数left = 1; %矩形框左参数right = n; % 矩形框右参数%寻找矩形框上参数while sum(d(top,:)=0 & top=1 bottom = bottom - 1;end%寻找矩形框左参数while sum(d(:,left)=O & left=1 right = right -1;enddd =

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1