function [result, plate] = Plate_Process(plate, fname, flag)
% 分割步骤
if nargin < 3
flag = 1;
end
% n = ndims(A) returns the number of dimensions in the array A.
if ndims(plate) == 3
% I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale intensity image I.
% rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.
plate1 = rgb2gray(plate); % 将车牌原始图片信息转换成grayscale intensity image(灰度图像?为方便,下面都称为灰度图像)
else
plate1 = plate;
end
Im = plate1; % Im为灰度图像
plate = double(plate);
% B = mean2(A) computes the mean of the values in A.
% b = std2(A) computes the standard deviation of the values in A.
% 求出当前图片的[均值 标准差]矩阵,用于和数据库的[均值 标准差]矩阵进行计算,然后找出最适合用于处理当前图片的数据库参数信息
m = [mean2(plate(:,:,1)) mean2(plate(:,:,2)) mean2(plate(:,:,3)) std2(plate(:,:,1)) std2(plate(:,:,2)) std2(plate(:,:,3))];
% f = fullfile(filepart1,...,filepartN) builds a full file specification, f, from the folders and file names specified.
% f = fullfile('myfolder','mysubfolder','myfile.m') ===> f = myfolder\mysubfolder\myfile.m
load('model.mat');
ms = cat(1, M.m); % 数据库中的[均值 标准差]矩阵
% B = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A.
% The size of B is [size(A,1)*m, (size(A,2)*n]. The statement repmat(A,n) creates an n-by-n tiling.
m = repmat(m, size(ms, 1), 1); % 将当前图片的[均值 标准差]矩阵进行拓展,以便进行与数据库[均值 标准差]矩阵运算
% B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N,
% where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.
% 按行求和
dis = sum((m - ms).^2, 2); % 当前图片[均值 标准差]矩阵与数据库[均值 标准差]矩阵的方差
[~, id] = min(dis); % 找出方差最小的那个,也就是说,找出最适合用于处理当前图片的数据库参数
if fname(6)=='s'
ro = M(id).ro; % 图片旋转的角度参数,单位为度
else
ro=0;
end
th = M(id).th; % 将灰度图像转换为二进制图像的门限,灰度图像中的值大于该门限则转换为1,否则转换为0
pts = M(id).pts; % 定义图片的顶点
% B = imrotate(A,angle,method) rotates image A by angle degrees in a counterclockwise direction around its
% center point, using the interpolation method specified by method.
Im = imrotate(Im, ro, 'bilinear'); % 将灰度图片Im按照逆时针方向,旋转ro度,插值方法选择双线性插值
plate = imrotate(plate, ro, 'bilinear'); % 将车牌原始图像,也按照逆时针方向,旋转ro度,插值方法选择双线性插值
% BW = im2bw(I, level) converts the grayscale image I to a binary image.
% The output image BW replaces all pixels in the input image with luminance(亮度) greater than level with the value 1 (white)
% and replaces all other pixels with the value 0 (black). Specify level in the range [0,1]. This range is relative to
% the signal levels possible for the image's class. Therefore, a level value of 0.5 is midway between black and white, regardless of class.
bw = im2bw(Im, th); % 将灰度图像Im转换成二进制图像,转换门限为th
% h = fspecial('average', hsize) returns an averaging filter h of size hsize.
% The argument hsize can be a vector specifying the number of rows and columns in h, or it can be a scalar, in which case h is a square matrix.
h = fspecial('average', 2); % 定义一个多维均值滤波器h,维度为2 x 2
% B = imfilter(A,h) filters the multidimensional array A with the multidimensional filter h.
% The array A can be logical or a nonsparse numeric array of any class and dimension. The result B has the same size and class as A.
% ___= imfilter(___,options,...) performs multidimensional filtering according to the specified options.
% 'replicate' ===> Input array values outside the bounds of the array are assumed to equal the nearest array border value.
bw1 = imfilter(bw, h, 'replicate'); % 使用多维均值滤波器h,对二进制图像bw进行滤波,滤波选项为replicate
% mask = Mask_Process(bw1); % 此方法功能是去除bw1图像的上下杂线,即使用特定算法,剔除二进制图像bw1的上下边界处的信息(这些行的像素值全设为0)
% 此处的mask可理解为:如果图像像素点在杂线上边界与车牌上边界之间或杂线下边界与车牌下边界之间,则这个像素点的值为0,
% 否则,如果图像像素点在杂线上边界和杂线下边界之间,则这个像素点的值为1
% bw2 = bw1 .* mask; % 这个.*操作可理解为:与运算,这样bw2就是利用mask矩阵剔除二进制图像bw1中干扰后的结果
bw2 = bw1;
% 除了通过Mask_Process()函数剔除杂线外,如果有定义车牌边界信息,则可进一步使用车牌边界信息进行图像处理
% if ~isempty(pts) % 如果pts不空(即,有外部边界顶点定义),则执行
% % BW = roipoly(I, c, r) returns the region of interest(ROI) specified by the polygon(多边形) described by vectors c and r,
% % which specify the column and row indices of each vertex(顶点), respectively. c and r must be the same size.
% % 根据二进制图片bw2,以及多边形的顶点pts(:, 1), pts(:, 2),得到二进制图像mask,顶点围起来的区域的值全为1,
% % 其它区域的值全为0,这样做的目的是剔除车牌边界的干扰
% mask = roipoly(bw2, pts(:, 1), pts(:, 2)); % 得到一个由pts顶点(可组成多边形)指定的二进制边界图像
% bw1 = bw1 .* mask; % bw1是只使用多边形剔除干扰后的二进制图像
% bw2 = bw2 .* mask; % bw2是使用Mask_Process()算法和多边形两种方法剔除干扰后的二进制图像
% end
result = bw2; % 此时bw2即为原始车牌图像滤波后的图片,即剔除了杂线(和剔除多边形边界以外的无用信息)之后的二进制图像
if flag
figure;
subplot(2, 2, 1); imshow(mat2gray(plate)); title('车牌区域图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(Im); title('车牌区域校正图像', 'FontWeight', 'Bold');
% 二值图像,即二进制图像,指像素点的值,要么为1,要么为0
subplot(2, 2, 3); imshow(bw1); title('滤波二值图像(仅剔除上下边界)', 'FontWeight', 'Bold');
subplot(2, 2, 4); imshow(bw2); title('滤波二值图像', 'FontWeight', 'Bold');
end