close all clc [fn,pn,fi]=uigetfile('ChePaiKu\*.jpg','选择图片'); YuanShi=imread([pn fn]);%输入原始图像 figure(1);subplot(3,2,1),imshow(YuanShi),title('原始图像'); %%%%%%%%%%1、图像预处理%%%%%%%%%%% YuanShiHuiDu=rgb2gray(YuanShi);%转化为灰度图像 subplot(3,2,2),imshow(YuanShiHuiDu),title('灰度图像'); BianYuan=edge(YuanShiHuiDu,'robert',0.09,'both');%Robert 算子边缘检测 subplot(3,2,3),imshow(BianYuan),title('Robert 算子边缘检测后图像'); se1=[1;1;1]; %线型结构元素 FuShi=imerode(BianYuan,se1); %腐蚀图像 subplot(3,2,4),imshow(FuShi),title('腐蚀后边缘图像'); se2=strel('rectangle',[30,30]); %矩形结构元素 TianChong=imclose(FuShi,se2);%图像聚类、填充图像 subplot(3,2,5),imshow(TianChong),title('填充后图像'); YuanShiLvBo=bwareaopen(TianChong,2000);%从对象中移除面积小于 2000 的小对象 figure(2); subplot(2,2,1),imshow(YuanShiLvBo),title('形态滤波后图像'); %%%%%%%%%%2、车牌定位%%%%%%%%%%% [y,x]=size(YuanShiLvBo);%size 函数将数组的行数返回到第一个输出变量,将数组的列数返回到第二个输出变量 YuCuDingWei=double(YuanShiLvBo); %%%%%%%%%%2.1、车牌粗定位之一确定行的起始位置和终止位置%%%%%%%%%%% Y1=zeros(y,1);%产生 y 行 1 列全零数组 for i=1:y for j=1:x if(YuCuDingWei(i,j)==1) Y1(i,1)= Y1(i,1)+1;%白色像素点统计 end end end [temp,MaxY]=max(Y1);%Y 方向车牌区域确定。返回行向量 temp 和 MaxY,temp 向量记录 Y1的每列的最大值,MaxY 向量记录 Y1 每列最大值的行号 subplot(2,2,2),plot(0:y-1,Y1),title('原图行方向像素点值累计和'),xlabel('行值'),ylabel('像素'); PY1=MaxY; while ((Y1(PY1,1)>=80)&&(PY1>1)) PY1=PY1-1; end PY2=MaxY; while ((Y1(PY2,1)>=80)&&(PY2