function[]=main(jpg) close all clc
tic %测定算法执行的时间
[fn,pn]=uigetfile('timg1,jpg','选择图片') %读入图片 I=imread([pn,fn]);
figure,imshow(I);title('原始图像'); %显示原始图像
Im1=rgb2gray(I); figure(2), subplot(1,2,1), imshow(Im1); title('灰度图'); figure(2), subplot(1,2,2), imhist(Im1);
title('灰度图的直方图'); %显示图像的直方图
Tiao=imadjust(Im1,[0.19,0.78],[0,1]); %调整图片 figure(3), subplot(1,2,1),
imshow(Tiao);title('增强灰度图'); figure(3), subplot(1,2,2), imhist(Tiao);
title('增强灰度图的直方图');
Im2=edge(Tiao,'Roberts','both'); %使用sobel算子进行边缘检测 figure(4), imshow(Im2);
title('sobel算子实现边缘检测')
se=[1;1;1];
Im3=imerode(Im2,se); figure(5), imshow(Im3); title('腐蚀效果图');
se=strel('square',40);%'rectangle',[25,25]/'diamond',25/ Im4=imclose(Im3,se); figure(6), imshow(Im4);
title('平滑图像的轮廓');
Im5=bwareaopen(Im4,1500); figure(7), imshow(Im5); title('移除小对象');
[y,x,z]=size(Im5); %返回Im5各维的尺寸,并存储在变量y、x、z中 Im6=double(Im5); %将Im5换成双精度数值 %开始横向扫描
tic %tic计时开始,toc结束,计算tic与toc之间程序的运行时间 Blue_y=zeros(y,1); %产生y*1的全0矩阵 for i=1:y %逐行扫描 for j=1:x
if(Im6(i,j,1)==1)%如果Im6图像中坐标为(i,j)的点值为1,即为移除小对象的白色区域, Blue_y(i,1)= Blue_y(i,1)+1;%则y*1列矩阵的相应像素点的元素值加1, end end end
[temp MaxY]=max(Blue_y);%temp为向量Blue_y的矩阵中的最大值,MaxY为该值的索引(最大值在向量中的位置)
%返回包含最大元素的列,即白色区域最宽的列 %Y方向车牌区域确定 figure(8),subplot(1,2,1),
plot(0:y-1,Blue_y),title('行方向白色像素点累计'),xlabel('行数'),ylabel('个数'); PY1=MaxY;
while ((Blue_y(PY1,1)>=120)&&(PY1>1)) PY1=PY1-1; end PY2=MaxY;
while ((Blue_y(PY2,1)>=40)&&(PY2
IY=I(PY1:PY2,:,:);
%IY为原始图像I中截取的纵坐标在PY1:PY2之间的部分 %横向扫描完成,开始纵向扫描
Blue_x=zeros(1,x);%进一步确定x方向的车牌区域 for j=1:x %逐列扫描 for i=PY1:PY2 if(Im6(i,j,1)==1)
Blue_x(1,j)= Blue_x(1,j)+1; %根据Im5的x值确定 end end end
figure(8),subplot(1,2,2),
plot(0:x-1,Blue_x),title('列方向白色像素点累计'),xlabel('列数'),ylabel('个数'); PX1=1;
while ((Blue_x(1,PX1)<3)&&(PX1
while ((Blue_x(1,PX2)<3)&&(PX2>PX1)) PX2=PX2-1; end
%end纵向扫描 PX1=PX1-2;%对车牌区域的校正 PX2=PX2+2;
dw=I(PY1:PY2,PX1:PX2,:); t=toc;
figure(9),subplot(1,2,1),imshow(IY),title('垂直方向合理区域');
figure(9),subplot(1,2,2),imshow(dw),title('定位剪切后的彩色车牌图像') imwrite(dw,'dw.jpg'); %将图像写入图形文件中 a=imread('dw.jpg'); b=rgb2gray(a);
imwrite(b,'车牌灰度图像.jpg'); figure(10); subplot(3,2,1), imshow(b),
title('1.车牌灰度图像')
基于Matlab的车牌识别实现源码



