clc
close all;
clear all;
I=imread('car.jpg'); %读取图像
figure(); subplot(3,2,1),imshow(I), title(' 原始图像 ');
I1=rgb2gray(I);% 转化为灰度图像
subplot(3,2,2),imshow(I1),title(' 灰度图像 ');
I2=edge(I1,'Sobel');% 采用robert算子进行边缘检测
subplot(3,2,3),imshow(I2),title(' 边缘检测后图像 ');
se=[1;1;1]; %线型结构元素
I3=imerode(I2,se); %腐蚀图像
subplot(3,2,4),imshow(I3),title(' 腐蚀后边缘图像 ');
se=strel('rectangle',[25,25]); %矩形结构元素
I4=imclose(I3,se);% 图像聚类、填充图像
subplot(3,2,5),imshow(I4),title(' 填充后图像 ');
I5=bwareaopen(I4,2000);%去除聚团灰度值小于 2000的部分
subplot(3,2,6),imshow(I5),title(' 形态滤波后图像 ');
[y,x,z]=size(I5);
I6=double(I5);
Y1=zeros(y,1);
for i=1:y
for j=1:x
if(I6(i,j,1)==1)
Y1(i,1)= Y1(i,1)+1;
end
end
end
[temp MaxY]=max(Y1);
figure();
subplot(3,2,1),plot(0:y-1,Y1),title(' 行方向像素点灰度值累计和'),xlabel('行值 '),ylabel('像素 ');
%求的车牌的行起始位置和终止位置
PY1=MaxY;
while ((Y1(PY1,1)>=50)&&(PY1>1))
PY1=PY1-1;
end
PY2=MaxY;
while ((Y1(PY2,1)>=50)&&(PY2
PY2=PY2+1;
end
IY=I(PY1:PY2,:,:);
X1=zeros(1,x);
for j=1:x
for i=PY1:PY2
if(I6(i,j,1)==1)
X1(1,j)= X1(1,j)+1;
end
end
end
subplot(3,2,2),plot(0:x-1,X1),title(' 列方向像素点灰度值累计和'),xlabel('列值 '),ylabel('像数 ');
PX1=1;
while ((X1(1,PX1)<3)&&(PX1
PX1=PX1+1;
end
PX2=x;
while ((X1(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-1;
end
PX1=PX1-1;
PX2=PX2+1;
%分割出车牌图像 %
dw=I(PY1:PY2,PX1:PX2,:);
subplot(3,2,3),imshow(dw),title(' 定位剪切后的彩色车牌图像 ')
%车牌字符分割,确定车牌位置后下一步的任务就是进行字符切分分离出车牌号码的全部字符图像 。
function y = isrgb(I)
if isrgb(I)
I1 = rgb2gray(I); %将RGB图像转化为灰度图像
else I1=I ;
end
g_max=double(max(max(I1)));
g_min=double(min(min(I1)));
T=round(g_max-(g_max-g_min)/3); % T 为二值化的阈值
[m,n]=size(I1);% d: 二值图像
%h=graythresh(I1);
I1=im2bw(I1,T/256);
subplot(3,2,4);
imshow(I1),title(' 二值化车牌图像 ');
end