ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【车牌识别】基于matlab 国内车牌识别【含Matlab源码 417期】

2021-07-05 10:08:31  阅读:177  来源: 互联网

标签:plate bw1 源码 灰度 图像 识别 车牌 图片


一、简介

基于matlab 国内车牌识别步骤:原图像识别、列过滤、行过滤、分割结果、灰度、水平倾斜校正后、滤波二值化、字符分割。

二、源代码

% clear 
addpath(genpath(pwd)); %添加子文件夹下的所有文件
%%加载图像
start=6212;
% state=num+1;
% start=6540;
state=1;

% for num = state:294
    close all
num = 1  ;   %读取文件夹下的第num张图片 
% 20
% 

    fname=['样本库\IMG_',num2str(start+num),'.jpg'];
   % 有三个车牌文件夹:'PlateImages/%d.jpg' 或者'PlateImages/Image1/%d.jpg' 或'PlateImages/Image2/%d.jpg'
filename = fullfile(pwd, fname);
Img = imread(filename);
%{
figure(5);
subplot(2,2,1);imshow(Img);title('原图');I1=rgb2gray(Img);
subplot(2,2,2);imshow(I1);title('灰度图');
subplot(2,2,3);imhist(I1);title('灰度图直方图');I2=edge(I1,'roberts',0.15,'both');
subplot(2,2,4);imshow(I2);title('roberts算子边缘检测')
se=[1;1;1];
I3=imerode(I2,se);
figure(6);
subplot(2,2,1);imshow(I3);title('腐蚀后图像');se=strel('rectangle',[25,25]);I4=imclose(I3,se);
subplot(2,2,2);imshow(I4);title('平滑图像的轮廓');I5=bwareaopen(I4,2000);
subplot(2,2,3);imshow(I5);title('从对象中移除小对象');
%} 

GetDB;

%% 定位车牌
% 定位车牌:找出车牌在原始图片中的位置
% 输入参数Img:  读取的原始真彩色图像信息
% 输出参数plate:经过定位处理后,从原始真彩色图像(会进行压缩处理)截取到的车牌位置处的真彩色图像信息
plate = Pre_Process(Img);

%% 倾斜校正
% 倾斜校正:对待识别数字的倾斜校正,分别使用Radon变换和仿射函数处理水平倾斜校正和垂直倾斜校正
% 输入参数:plate为定位截取的真彩色车牌图像信息
% 输出参数:
plate = radon_repair(plate);

%% 车牌滤波
% 车牌滤波:剔除(像素值置0)车牌图片的边界图像信息
% 输入参数:plate为定位截取的真彩色车牌图像信息
% 输出参数:d为原始车牌图像滤波(即剔除上下边界外(和使用多边形区域剔除))后的图片,p为真彩色原始车牌图像plate逆时针旋转后的图片
[d, p] = Plate_Process(plate,fname);

%% 分割车牌
% 分割车牌:裁掉(删除)车牌图像信息的边界
% 输入参数:d为原始车牌图像滤波(即剔除上下边界外(和使用多边形区域剔除))后的图片,p为真彩色原始车牌图像plate逆时针旋转后的图片
% 输出参数:根据图像d的非0边界,裁剪出的输入图片:输入图片d裁剪后输出图片e,输入图片p裁剪后输出图片p
[e, p] = Segmation(d, p);
%% 去除噪声
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

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a

标签:plate,bw1,源码,灰度,图像,识别,车牌,图片
来源: https://blog.51cto.com/u_15287606/2976058

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有