function varargout = mygui(varargin)
% MYGUI MATLAB code for mygui.fig
% MYGUI, by itself, creates a new MYGUI or raises the existing
% singleton*.
%
% H = MYGUI returns the handle to a new MYGUI or the handle to
% the existing singleton*.
%
% MYGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYGUI.M with the given input arguments.
%
% MYGUI('Property','Value',...) creates a new MYGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before mygui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to mygui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help mygui
% Last Modified by GUIDE v2.5 27-Jun-2020 05:56:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mygui_OpeningFcn, ...
'gui_OutputFcn', @mygui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before mygui is made visible.
function mygui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to mygui (see VARARGIN)
% Choose default command line output for mygui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mygui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = mygui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% 按钮1为输入车牌读取按钮
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname]=uigetfile({'*.jpg';'*.bmp'}, 'File Selector');
I=imread([pathname '\' filename]);
handles.I=I;
guidata(hObject, handles);
axes(handles.axes1);
imshow(I);title('原图');
%矫正识别
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I=handles.I;
P=rgb2gray(I); %真彩色图像转化为灰度图像
%车牌矫正
%用Radon变换矫正倾斜的车牌
P1 = wiener2(P, [5, 5]); % 对灰度图像进行维纳滤波
P2 = edge(P1, 'canny'); % 边缘检测
theta = 0 : 179;
r = radon(P2, theta); %Radon变换
[m, n] = size(r); %compute the size of the image
c = 1;
for i = 1 : m
for j = 1 : n
if r(1,1) < r(i,j)
r(1,1) = r(i,j);
c = j;
end
end
end
rot_theta=0;
rot_theta = 90 - c;
P3 = imrotate(I, rot_theta, 'crop'); % 对图像进行旋转,校正图像
fprintf('图像倾斜角是:%d\n',rot_theta) %输出图像倾斜角
axes(handles.axes16);
imshow(P3);title('车牌矫正')
I1=rgb2gray(P3); %真彩色图像转化为灰度图像
I2=edge(I1,'roberts',0.18,'both'); %沿两个方向进行边缘检测,阈值为0.18(忽略小于阈值的边缘),返回图像大小相同
axes(handles.axes2); %选定句柄axes2在其上面作图
imshow(I1);title('彩色图像灰度化');
axes(handles.axes4);
imshow(I2);title('边缘检测处理');
axes(handles.axes3);imhist(I1);title('灰度化直方图');
%腐蚀操作
se=[1;1;1]; %构造一个三维单位列向量
I3=imerode(I2,se);%腐蚀图像 消除噪点,使目标缩小
axes(handles.axes5); %选定句柄axes2在其上面作图
imshow(I3);title('腐蚀处理');
se=strel('rectangle',[25,25]); %构造结构元素对象se,长方形(rectangle),大小25X25
%strel用于膨胀腐蚀及开闭运算等操作的结构元素对象
%闭运算
I4=imclose(I3,se);%图像聚类,填充图像,闭操作可使轮廓线更光滑,填充图像细小空间,平滑边界
I5=bwareaopen(I4,2000);%删除小面积对象(移除所有少于2000像素的连接的组件(对象))
axes(handles.axes14); %选定句柄axes2在其上面作图
imshow(I5);title('闭运算处理');
[y,x]=size(I5);%返回图像各维的尺寸,存储在x,y中(矩阵的行数y和列数x)
myI=double(I5); %数据类型转换
tic %tic计时开始,toc结束
%Y方向车牌区域确定
%横向扫描
Blue_y=zeros(y,1);%产生一个y维1列的零矩阵(y维零向量),水平方向
for i=1:y %for i=1:k:100 表示每次循环过后,i= i+k,如果不指定k,就默认i=i+1
for j=1:x
if(myI(i,j,1)==1) %扫描图像如果myI图像坐标为(i,j)点值为1,即背景颜色为蓝色,blue加一
Blue_y(i,1)=Blue_y(i,1)+1; %蓝色像素点统计(按行统计)
end
end
end
[~, MaxY]=max(Blue_y); %[Y,U]=max(A):返回行向量Y和U,Y向量记录A的每列的最大值,U向量记录每列最大值的行号。
%temp为向量Blue_y的元素中的最大值,MaxY为该值得索引(行号),即Blue_y的第MaxY行值最大最大值为temp
PY1=MaxY;
while((Blue_y(PY1,1)>=5)&&(PY1>1)) %从上向下扫描
PY1=PY1-1;
end
PY2=MaxY;
while((Blue_y(PY2,1)>=5)&&(PY2<y)) %从下向上扫描
PY2=PY2+1;
end
%X方向车牌区域确定
%纵向扫描
Blue_x=zeros(1,x);%进一步确认x方向的车牌区域竖直方向
for j=1:x
for i=PY1:PY2
if(myI(i,j,1)==1)
Blue_x(1,j)=Blue_x(1,j)+1;
end
end
end
PX1=1;
while((Blue_x(1,PX1)<3)&&(PX1<x)) %从左向右扫描
PX1=PX1+1;
end
PX2=x;
while((Blue_x(1,PX2)<3)&&(PX2>PX1)) %从右向左扫描
PX2=PX2-1;
end
PX1=PX1-1;%对车牌区域的矫正
PX2=PX2+1;
dw=P3(PY1:PY2-8,PX1:PX2,:); %定位矫正后的彩色车牌图像,存入dw中
toc; %计时
axes(handles.axes15);imshow(dw),title('定位车牌');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%车牌字符分割
imwrite(dw,'dw.jpg');%将彩色车牌写入dw文件中
a=imread('dw.jpg');%读取车牌
b=rgb2gray(a);%将车牌图像转换为灰度图
imwrite(b,'灰度车牌.jpg');%将灰度图写入文件
g_max=double(max(max(b))); %b的最大值并转化为双精度型
g_min=double(min(min(b))); %b的最小值并转化为双精度型
T=round(g_max-(g_max-g_min)/3);%T为二值化的阈值(round为取整函数,最近的方向取整)
%[m,n]=size(b);
d=(double(b)>=T);%d:二值图像(灰度图大于阈值的部分即为二值图像)imbw
imwrite(d,'二值化.jpg');
%均值滤波前
%均值滤波
h=fspecial('average',3);
%建立预定义的滤波算子,average为均值滤波,模板尺寸为3*3
d=im2bw(round(filter2(h,d)));%filter2使用指定的滤波器h对h进行d即均值滤波、
% im2bw使用阈值变换法把灰度图像转换成二值图像
imwrite(d,'均值滤波.jpg');
%膨胀或腐蚀
se=eye(2);%eye(n)即n维单位矩阵
[m,n]=size(d);%返回信息矩阵
if bwarea(d)/m/n>=0.365%bwarea计算二值图像中对象的总面积与整个面积的比是否大于0.365
d=imerode(d,se);%如果大于0.365则进行腐蚀、去除边缘检测后图像中的无关结构
elseif bwarea(d)/m/n<=0.235%计算二值图像中对象的总面积与整个面积的比值是否小于0.235
d=imdilate(d,se);%%如果小于则实现膨胀操作、放大图片以便于后期处理
end
imwrite(d,'膨胀.jpg');
%寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割
d=cut_char(d);%调用切割函数
[m,n]=size(d);
k1=1;
k2=1;
s=sum(d);%列求和返回一个行向量
j=1;
while j~=n
while s(j)==0
j=j+1;
end
k1=j;
while s(j)~=0 && j<=n-1
j=j+1;
end
k2=j-1;
if