%%
clear;
close all;
clc;
%% 1自动弹出提示框读取图像
[filename, filepath] = uigetfile('.jpg', '输入一个需要识别的图像');
file = strcat(filepath, filename); %imread函数读取图像文件
img = imread(file);
figure; %figure命令同时显示多幅图
imshow(img);
title('车牌原图像');
%% 2灰度处理:将原始彩色图像转换成灰度图像,并显示直方图
img2 = rgb2gray(img); % RGB图像转灰度图像
figure;
subplot(1, 2, 1);
imshow(img2);
title('灰度图像');
subplot(1, 2, 2);
imhist(img2); % 直方图
title('灰度处理后的灰度直方图');
%% 3边缘提取:使用roberts算子进行边缘检测(调用edge函数),标识数字图像中亮度变化明显的点
img3 = edge(img2, 'roberts', 0.15, 'both');
figure('name','边缘检测');
imshow(img3);
title('roberts算子边缘检测');
%% 4图像腐蚀:去除不需要的边缘信息,尽可能只保留车牌上字符的轮廓
se=[1;1;1];
img4 = imerode(img3, se); % 调用imerode图像腐蚀函数
figure('name','图像腐蚀');
imshow(img4);
title('图像腐蚀后的图像');
%% 5图像膨胀+平滑图像(图中未滤出车牌之外的所有边缘信息,如银白色车牌标志和进气格栅)
se = strel('rectangle', [15, 20]); % 调用strel创建一个长15,宽20的矩形结构,strel主要用于膨胀腐蚀及开闭运算
img5 = imclose(img4, se); % 用结构元素se对图像进行闭操作,使图像轮廓线更平滑
figure('name','平滑处理');
imshow(img5);
title('平滑处理后的图像轮廓');
%% 6从图像中删除所有大于10000像素,默认8邻接,5000~10000
% 此处主要为了去除银白色进气栅格类型车辆
img6 = removeLargeArea(img5, 10000);
figure('name', '移除大对象');
imshow(img6);
title('从图像中移除大对象');
%% 7从图像中删除所有少于1200像素8邻接
%主要是去除银白色车牌标志
img7 = bwareaopen(img6, 1200);
figure('name', '移除小对象');
imshow(img7);
title('切割出图像');
%% 8车牌定位后从原图中切割出车牌图像
[y, x, z] = size(img7); % 得到图像的尺寸
img8 = double(img7); % 转成双精度浮点型
tic % tic用来保存当前时间,而后使用toc来记录程序完成时间
% 车牌的蓝色区域
% Y方向
blue_Y = zeros(y, 1); % zeros功能是返回一个y行1列的double类零矩阵
for i = 1:y
for j = 1:x
if(img8(i, j) == 1) % 判断车牌位置区域
blue_Y(i, 1) = blue_Y(i, 1) + 1; % 每行蓝/白色像素点统计
end
end
end
% 找到Y坐标的最小值
MinY = 1; % 最小值从1开始
while (blue_Y(MinY, 1) < 10) && (MinY < y) % 10:每行像素点数量小于10的则
MinY = MinY + 1; % Ymin值+1,直到筛选到10像素以上及车牌区域时跳出循环
end
% 找到Y坐标的最大值
MaxY = y;
while (blue_Y(MaxY, 1) < 10) && (MaxY > MinY) % 同上
MaxY = MaxY - 1;
end
% X方向
blue_X = zeros(1, x);
for j = 1:x
for i = 1:y
if(img8(i, j) == 1) % 判断车牌位置区域
blue_X(1, j) = blue_X(1, j) + 1; % 每列蓝色像素点统计
end
end
end
% 找到X坐标的最小值
MinX = 1;
while (blue_X(1, MinX) < 5) && (MinX < x) % 同上
MinX = MinX + 1;
end
% 找到X坐标的最小值
MaxX = x;
while (blue_X(1, MaxX) < 5) && (MaxX > MinX) % 同上
MaxX = MaxX - 1;
end
% 对原图像进行裁剪
img9 = img(MinY:MaxY, MinX:MaxX, :); % 对原图像进行裁剪的范围
figure('name', '定位剪切图像');
imshow(img9);
title('定位剪切后的彩色车牌图像')
% 保存提取出来的车牌图像
imwrite(img9, '车牌图像.jpg');
%% 对车牌图像作图像预处理,直接进行字符识别会不准确
pre_img = imread('车牌图像.jpg');
% 9转换成灰度图像并绘制直方图
pre_img1 = rgb2gray(pre_img); % RGB图像转灰度图像
figure;
subplot(1, 2, 1);
imshow(pre_img1);
title('灰度图像');
subplot(1, 2, 2);
imhist(pre_img1);
title('灰度处理后的灰度直方图');
% 10直方图均衡化:增强对比度
pre_img2 = histeq(pre_img1);
figure('name', '直方图均衡化');
subplot(1,2,1);
imshow(pre_img2);
title('直方图均衡化的图像');
subplot(1,2,2);
imhist(pre_img2);
title('直方图');
% 11二值化处理:转为二值图
pre_img3 = im2bw(pre_img2, 0.75);
figure('name', '二值化处理');
imshow(pre_img3);
title('车牌二值图像');
% % 12中值滤波:滤除图像噪声的同时,能够保护信号的边缘
% pre_img4 = medfilt2(pre_img3);
% figure('name', '中值滤波');
% imshow(pre_img4);
% title('中值滤波后的图像');
%12均值滤波
h = fspecial('average',[3,3]); %3*3均值滤波
pre_img4 = im2bw (round(filter2(h,pre_img3)));
figure('name', '均值滤波');
imshow(pre_img4);
title('均值滤波后的图像');
%% 对预处理后的图像进行字符区块分割
% 13字符区域分割:利用自定义函数my_imsplit将字符区分割出来
pre_img5 = my_imsplit(pre_img4);
figure('name', '字符区域分割');
imshow(pre_img5);
title('字符区域分割图像');
% 单个字符分割
% 寻找连续文字的块,若长度大于某阈值,则需要分割
[m, n] = size(pre_img5); % 读取字符图像尺寸m,n
s = sum(pre_img5); % sum(x)是以矩阵x的每一列为对象,列求和,结果是行向量
j = 1; k1 = 1; k2 = 1;
while j ~= n %列数从1~n,直到某列不为0跳出循环
while s(j) == 0
j = j + 1;
end
k1 = j; %该列数赋值给k1
while s(j) ~= 0 && j <= n-1 %直到某列为0跳出循环
j = j + 1;
end
k2 = j + 1; %该列数赋值给k1
if k2 - k1 > round(n / 6.5) %判断区块是否大于总列数1/6.5;round函数是四舍五入取整
[value, num] = min(sum(pre_img5(:, [k1+5:k2-5]))); %把sum向量的最小值赋给val,最小值的位置(在哪列)赋给num
pre_img5(:, k1+num+5) = 0; % 分割
end
end
% 再切割
% 切割出7个字符,首先对车牌图像自左向右逐列扫描,找连续有文字的区间块,将区间块有效宽度和某一固定阈值
% (此处设定为10)比较,若小于设定阈值,则认为是左侧干扰,剪裁干扰区域;反之,分割出字符块
y1 = 10; y2 = 0.25; xh = 0;
word1 = []; %定义第一个字符
while xh == 0 %xh为自定义,作标记循环
[m, n] = size(pre_img5); %读取字符图像尺寸m,n
left = 1;
width = 0;
while sum(pre_img5(:, width+1)) ~= 0 %当图像某列不为0时,宽度+1
width = width + 1;
end
if width < y1 %宽度<10时,认为是左侧干扰
pre_img5(:, [1:width]) = 0; %左侧区域置0(黑色)
pre_img5 = my_imsplit(pre_img5); %图像切割去除黑色区域
else
temp = my_imsplit(imcrop(pre_img5, [1,1,width,m]));%图像切割出字符块
[m, n] = size(temp); %字符尺寸
all = sum(sum(temp)); %总像素值求和
two_thirds=sum(sum(temp([round(m/3):2*round(m/3)],:)));
if two_thirds/all > y2 %图像归一化处理
xh = 1;
word1 = temp; %字符块赋值给第一个字符
end
pre_img5(:, [1:width]) = 0; %该字符区域置0(黑色)
pre_img5 = my_imsplit(pre_img5);%图像切割去除黑色区域
end
end
%14字符分割绘图(剩下区域)
figure('name', '剩下区域');
subplot(2,4,1), imshow(pre_img5);
%由于车牌第一个是汉字,宽度较其他字符大,且需要排除左侧干扰,因此单独编写,其他字符调用函数getword即可
% 分割出第二个字符
[word2,pre_img5]=getword(pre_img5);
subplot(2,4,2), imshow(pre_img5);
% 分割出第三个字符
[word3,pre_img5]=getword(pre_img5);
subplot(2,4,3), imshow(pre_img5);
% 分割出第四个字符
[word4,pre_img5]=getword(pre_img5);
subplot(2,4,4), imshow(pre_img5);
% 分割出第五个字符
[word5,pre_img5]=getword(pre_img5);
subplot(2,3,4), imshow(pre_img5);
% 分割出第六个字符
[word6,pre_img5]=getword(pre_img5);
subplot(2,3,5), imshow(pre_img5);
% 分割出第七个字符
[word7,pre_img5]=getword(pre_img5);
subplot(2,3,6), imshow(pre_img5);
%15字符绘图(单个字符区域)
figure('name', '单个字符区域');
subplot(5,7,1),imshow(word1),title('1');
subplot(5,7,2),imshow(word2),title('2');
subplot(5,7,3),imshow(word3),title('3');
subplot(5,7,4),imshow(word4),title('4');
subplot(5,7,5),imshow(word5),title('5');
subplot(5,7,6),imshow(word6),title('6');
subplot(5,7,7),imshow(word7),title('7');
%imresize对图像做缩放处理,常用调用格式为:B=imresize(A,ntimes,method);
%其中method可选nearest,bilinear(双线性),bicubic,box,lanczors2,lanczors3等
word1=imresize(word1,[4