-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.m
More file actions
executable file
·372 lines (342 loc) · 11.5 KB
/
gui.m
File metadata and controls
executable file
·372 lines (342 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_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 gui
% Last Modified by GUIDE v2.5 01-Jan-2017 16:51:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_OpeningFcn, ...
'gui_OutputFcn', @gui_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 gui is made visible.
function gui_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 gui (see VARARGIN)
% Choose default command line output for gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_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;
% --- Executes on button press in pushbutton1.
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)
global I; %定义一个全局变量image
[filename,pathname]=uigetfile({'*.jpg';'*.png';'*.tif'},'载入图像'); %选择图像路径
str=[pathname filename]; %合成路径+文件名
I=imread(str); %读取图像
axes(handles.axes1); %使用第一个axes
imshow(I); %显示图像
% --- Executes on button press in pushbutton2.
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)
global I;
global dw;
I1=rgb2gray(I);%将彩图转换为灰度图
%figure(2),subplot(1,2,1),imshow(I1);title('灰度图');
%figure(2),subplot(1,2,2),imhist(I1);title('灰度直方图');%绘制灰度图的直方图
%ma=double(max(max(I2)));
%mi=double(min(min(I2)));
%I2=(255/(ma-mi))*I2-(255*mi)/(ma-mi);
%figure(3),imshow(I2);title('灰度拉伸');
I2=edge(I1,'roberts',0.18,'both');%选择阈值0.18,用roberts算子进行边缘检测
%figure(3),imshow(I2);title('robert算子边缘检测');
se=[1;1;1];
I3=imerode(I2,se);%对图像实施腐蚀操作,即膨胀的反操作
%figure(4),imshow(I3);title('腐蚀后图像');
%平滑图像
se=strel('rectangle',[25,25]);%构造结构元素以正方形构造一个se
I4=imclose(I3,se);% 图像聚类、填充图像
%figure(5),imshow(I4);title('平滑图像');
I5=bwareaopen(I4,2500);% 去除聚团灰度值小于2500的部分
%figure(6),imshow(I5);title('从对象中移除小对象'); %用imshow函数显示滤波后图像
[y,x,z]=size(I5);%返回I5各维的尺寸,存储在x,y,z中
myI=double(I5);%将I5转换成双精度
tic %tic表示计时的开始,toc表示计时的结束
Blue_y=zeros(y,1);%产生一个y*1的零阵
for i=1:y
for j=1:x
if(myI(i,j,1)==1)
%如果myI(i,j,1)即myI的图像中坐标为(i,j)的点值为1,即该点为车牌背景颜色蓝色
%则Blue_y(i,1)的值加1
Blue_y(i,1)= Blue_y(i,1)+1;%蓝色像素点统计
end
end
end
[temp MaxY]=max(Blue_y);%Y方向车牌区域确定
%temp为向量Blue_y的元素中的最大值,MaxY为该值的索引
Y1=MaxY;
while ((Blue_y(Y1,1)>=12)&&(Y1>1))
Y1=Y1-1;
end
Y2=MaxY;
while ((Blue_y(Y2,1)>=12)&&(Y2<y))
Y2=Y2+1;
end
IY=I(Y1:Y2,:,:);
%IY为原始图像I中截取的纵坐标在Y1:Y2之间的部分
%行方向车牌区域确定
%进一步确定x方向的车牌区域
Blue_x=zeros(1,x);
for j=1:x
for i=Y1:Y2
if(myI(i,j,1)==1)
Blue_x(1,j)= Blue_x(1,j)+1;
end
end
end
X1=1;
while ((Blue_x(1,X1)<3)&&(X1<x))
X1=X1+1;
end
X2=x;
while ((Blue_x(1,X2)<3)&&(X2>X1))
X2=X2-1;
end
X1=X1-1;%对车牌区域的校正
X2=X2+1;
dw=I(Y1:Y2,X1:X2,:);
%dw=I(Y1:Y2,:,:);
t=toc;
%figure(7),subplot(1,2,1),imshow(IY),title('行方向合理区域');%行方向车牌区域确定
%figure(7),subplot(1,2,2),imshow(dw),title('定位剪切后的彩色车牌图像');%定位后的车牌区域如下所示:
axes(handles.axes2); %使用第二个axes
imshow(dw); %显示图像
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global dw;
b=rgb2gray(dw);%将车牌图像转换为灰度图
%imwrite(b,'车牌灰度图像.jpg');%将灰度图像写入文件中
%figure(8);subplot(3,2,1),imshow(b),title('车牌灰度图像')
g_max=double(max(max(b)));
g_min=double(min(min(b)));
T=round(g_max-(g_max-g_min)/3); % T为二值化的阈值
d=(double(b)>=T); % d:二值图像
%imwrite(d,'车牌二值图像.jpg');
%subplot(3,2,2),imshow(d),title('均值滤波前')%均值滤波前
%{
% 旋转
rotate=0;
bw=edge(d);
[m,n]=size(d);
theta=1:179;
% bw 表示需要变换的图像,theta 表示变换的角度
% 返回值 r 表示的列中包含了对应于 theta中每一个角度的 Radon 变换结果
% 向量 xp 包含相应的沿 x轴的坐标
[r,xp]=radon(bw,theta);
i=find(r>0);
[foo,ind]=sort(-r(i));
k=i(ind(1:size(i)));
[y,x]=ind2sub(size(r),k);
[mm,nn]=size(x);
if mm~=0 && nn~=0
j=1;
while mm~=1 && j<180 && nn~=0
i=find(r>j);
[foo,ind]=sort(-r(i));
k=i(ind(1:size(i)));
[y,x]=ind2sub(size(r),k);
[mm,nn]=size(x);
j=j+1;
end
if nn~=0
if x % Enpty matrix: 0-by-1 when x is an enpty array.
x=x;
else % 可能 x 为空值
x=90; % 其实就是不旋转
end
d=imrotate(d,abs(90-x)); % 旋转图像
rotate=1;
end
end
imwrite(d,'4.Radon 变换旋转后的二值图像.jpg');
figure(8),subplot(3,2,2),imshow(d),title('4.Radon 变换旋转后的二值图像');
%}
%滤波
h=fspecial('average',3);
%建立预定义的滤波算子,average为均值滤波,模板的尺寸为3*3
d=im2bw(round(filter2(h,d)));%使用指定的滤波器h对h进行d即均值滤波
%imwrite(d,'均值滤波后.jpg');
%subplot(3,2,3),imshow(d),title('均值滤波后')
% 某些图像进行操作
% 膨胀或腐蚀
% se=strel('square',3); % 使用一个3X3的正方形结果元素对象对创建的图像进行膨胀
% 'line'/'diamond'/'ball'...
se=eye(2); % eye(n) returns the n-by-n identity matrix 单位矩阵
[m,n]=size(d);%返回矩阵b的尺寸信息, 并存储在m,n中
if bwarea(d)/m/n>=0.365 %计算二值图像中对象的总面积与整个面积的比是否大于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');
%subplot(3,2,4),imshow(d),title('膨胀或腐蚀处理后');
%寻找连续有文字的块,若长度大于某阈值,则认为该块有两个字符组成,需要分割
d=qiege(d);
[m,n]=size(d);
%subplot(3,2,5),imshow(d),title(n)
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 k2-k1>=round(n/6.5)
[val,num]=min(sum(d(:,[k1+5:k2-5])));
d(:,k1+num+5)=0; % 分割
end
end
% 再切割
d=qiege(d);
% 切割出 7 个字符
y1=10;y2=0.25;flag=0;word1=[];
while flag==0
[m,n]=size(d);
left=1;wide=0;
while sum(d(:,wide+1))~=0
wide=wide+1;
end
if wide<y1 % 认为是左侧干扰
d(:,[1:wide])=0;
d=qiege(d);
else
temp=qiege(imcrop(d,[1 1 wide 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
flag=1;word1=temp; % WORD 1
end
d(:,[1:wide])=0;d=qiege(d);
end
end
% 分割出第二至第七个字符
[word2,d]=getword(d);
[word3,d]=getword(d);
[word4,d]=getword(d);
[word5,d]=getword(d);
[word6,d]=getword(d);
[word7,d]=getword(d);
[m,n]=size(word1);
% 商用系统程序中归一化大小为 40*20,此处演示
word1=imresize(word1,[40 20]);
word2=imresize(word2,[40 20]);
word3=imresize(word3,[40 20]);
word4=imresize(word4,[40 20]);
word5=imresize(word5,[40 20]);
word6=imresize(word6,[40 20]);
word7=imresize(word7,[40 20]);
%subplot(2,7,8),imshow(word1),title('1');
%subplot(2,7,9),imshow(word2),title('2');
%subplot(2,7,10),imshow(word3),title('3');
%subplot(2,7,11),imshow(word4),title('4');
%subplot(2,7,12),imshow(word5),title('5');
%subplot(2,7,13),imshow(word6),title('6');
%subplot(2,7,14),imshow(word7),title('7');
imwrite(word1,'1.jpg');
imwrite(word2,'2.jpg');
imwrite(word3,'3.jpg');
imwrite(word4,'4.jpg');
imwrite(word5,'5.jpg');
imwrite(word6,'6.jpg');
imwrite(word7,'7.jpg');
liccode=char(['0':'9' 'A':'Z' '京辽鲁陕苏豫浙']); %建立自动识别字符代码表
SubBw2=zeros(40,20); %产生40*20的全0矩阵
l=1;
for I=1:7
ii=int2str(I); %转为串
t=imread([ii,'.jpg']); %读取图片文件中的数据
SegBw2=imresize(t,[32 16],'nearest'); %对图像做缩放处理
SegBw2=double(SegBw2)>16;
if l==1 %第一位汉字识别
kmin=37;
kmax=43;
elseif l==2 %第二位 A~Z 字母识别
kmin=11;
kmax=36;
else l>=3 %第三位以后是字母或数字识别
kmin=1;
kmax=36;
end
for k2=kmin:kmax
fname=strcat('sample/',liccode(k2),'.bmp'); %把行向量转化成字符串
SamBw2 = imread(fname);
SamBw2=double(SamBw2)>1;
%size(SamBw2)
for i=1:32
for j=1:16
SubBw2(i,j)=SegBw2(i,j)-SamBw2(i,j);
end
end
% 以上相当于两幅图相减得到第三幅图
Dmax=0;
for k1=1:32
for l1=1:16
if ( SubBw2(k1,l1) > 0 || SubBw2(k1,l1) <0 )
Dmax=Dmax+1;
end
end
end
Error(k2)=Dmax;
end
Error1=Error(kmin:kmax);
MinError=min(Error1);
findc=find(Error1==MinError);
Code(l*2-1)=liccode(findc(1)+kmin-1);
Code(l*2)=' '; %输出最大相关图像
l=l+1;
end
%figure(10),imshow(dw),title (['车牌号码:', Code],'Color','r');
set(handles.text1,'string',num2str(Code));