function varargout = car_recognition(varargin)
% CAR_RECOGNITION MATLAB code for car_recognition.fig
% CAR_RECOGNITION, by itself, creates a new CAR_RECOGNITION or raises the existing
% singleton*.
%
% H = CAR_RECOGNITION returns the handle to a new CAR_RECOGNITION or the handle to
% the existing singleton*.
%
% CAR_RECOGNITION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CAR_RECOGNITION.M with the given input arguments.
%
% CAR_RECOGNITION('Property','Value',...) creates a new CAR_RECOGNITION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before car_recognition_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to car_recognition_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 car_recognition
% Last Modified by GUIDE v2.5 30-Dec-2017 21:01:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @car_recognition_OpeningFcn, ...
'gui_OutputFcn', @car_recognition_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 car_recognition is made visible.
function car_recognition_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 car_recognition (see VARARGIN)
% Choose default command line output for car_recognition
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes car_recognition wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = car_recognition_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
[filename,pathname]=uigetfile('*.jpg','选择一张图片');
I=imread([pathname,filename]);
axes(handles.axes1);
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
IGRAY=rgb2gray(I);
%灰度直方图
[num]=grey_level_histogram(IGRAY);
%均值化
[I_equal]=im_equal(num,IGRAY);
%灰度拉伸
f=double(IGRAY);
k1=min(min(f));
k2=max(max(f));
a=(k2-k1)/2;
b=k2-80;
c=k1-20;
[s_grayequal]=gray_stretch(a,b,c,I_equal);
[s_gray]=gray_stretch(a,b,c,IGRAY);
%二值化
[J,thresh,I,Hi]=OTSU(s_grayequal);
if sum(sum(J(:,1)))==size(J,1)*255
J=abs(255-J);
end
%边缘检测
I2=edge(J,'sobel',0.08,'both');
%腐蚀平滑图像
se=[1;1;1];
I3=imerode(I2,se);% 腐蚀Imerode(X,SE).其中X是待处理的图像,SE是结构元素对象
se = strel('line',11,90);
fes=imdilate(I3,se);
se=strel('rectangle',[8,8]);% 8X8的矩形
I4=imclose(fes,se);% 用8*8的矩形对图像进行闭运算(先膨胀后腐蚀)有平滑边界作用
I5=bwareaopen(I4,2000);% 从二进制图像中移除所有少于2000像素的连接对象,消失的是连续的白色像素数量少于2000的字符
[m,n]=size(I5);
a=[];
b=[];
for i=1:m
sum_x(i)=sum(I5(i,:));
if sum_x(i)~=0
a=[a i];
end
end
for i=1:n
sum_y(i)=sum(I5(:,i));
if sum_y(i)~=0
b=[b i];
end
end
[J_gray,thresh,I,Hi]=OTSU(s_gray);
%转换为黑底白字
if sum(sum(J_gray(:,3)))>=0.8*size(J_gray,1)*255
J_gray=abs(255-J_gray);
end
I6=J_gray(a(1,1):a(1,size(a,2)),b(1,1):b(1,size(b,2)));
I6(:,1:2)=0;
I6(:,size(I6,2))=0;
I6(1:2,:)=0;
I6(size(I6,1),:)=0;
I6=bwareaopen(I6,60);
imshow(I6);
se=strel('rectangle',[8,8]);
I7=imclose(I6,se);% 用8*8的矩形对图像进行闭运算(先膨胀后腐蚀)有平滑边界作用
%去除边框
JJ=I6;
J=I7;
[m,n]=size(JJ);
JJ=bwareaopen(JJ,60);
[JJ]=remove_border(J,JJ,m,n);
%字符分割
[charsum]=division(JJ);
% for i=0:6
% imwrite(charsum(:,1+16*i:16+16*i),[num2str(i+1),'.jpg']);
%
% end
axes(handles.axes2);
char1=charsum(:,1:16);
char1=imresize(char1,[20,20],'nearest');
imshow(char1);
axes(handles.axes3);
char2=charsum(:,17:32);
char2=imresize(char2,[20,20],'nearest');
imshow(char2);
axes(handles.axes4);
char3=charsum(:,33:48);
char3=imresize(char3,[20,20],'nearest');
imshow(char3);
axes(handles.axes5);
char4=charsum(:,49:64);
char4 = imresize(char4,[20,20],'nearest');
imshow(char4);
axes(handles.axes6);
char5=charsum(:,65:80);
char5=imresize(char5,[20,20],'nearest');
imshow(char5);
axes(handles.axes7);
char6=charsum(:,81:96);
char6=imresize(char6,[20,20],'nearest');
imshow(char6);
axes(handles.axes8);
char7=charsum(:,97:112);
char7=imresize(char7,[20,20],'nearest');
imshow(char7);
char1 = im2bw(char1);
array_char1 = reshape(char1, [400, 1]);
load('.\net_ch.mat');
test = sim(net_ch, array_char1);
test = compet(test);
[~, label] = max(test);
[s1]=wordrec(label);
char2 = im2bw(char2);
array_char2 = reshape(char2, [400, 1]);
load('.\net.mat');
test = sim(net, array_char2);
test = compet(test);
[~, label] = max(test);
[s2]=numrec(label);
char3 = im2bw(char3);
array_char3 = reshape(char3, [400, 1]);
load('.\net.mat');
test = sim(net, array_char3);
test = compet(test);
[~, label] = max(test);
[s3]=numrec(label);
char4 = im2bw(char4);
array_char4 = reshape(char4, [400, 1]);
load('.\net.mat');
test = sim(net, array_char4);
test = compet(test);
[~, label] = max(test);
[s4]=numrec(label);
char5 = im2bw(char5);
array_char5 = reshape(char5, [400, 1]);
load('.\net.mat');
test = sim(net, array_char5);
test = compet(test);
[~, label] = max(test);
[s5]=numrec(label);
char6 = im2bw(char6);
array_char6 = reshape(char6, [400, 1]);
load('.\net.mat');
test = sim(net, array_char6);
test = compet(test);
[~, label] = max(test);
[s6]=numrec(label);
char7 = im2bw(char7);
array_char7 = reshape(char7, [400, 1]);
load('.\net.mat');
test = sim(net, array_char7);
test = compet(test);
[~, label] = max(test);
[s7]=numrec(label);
s=char([s1,s2,s3,s4,s5,s6,s7]);
h=msgbox(s,'识别结果');
%修改字体
ah=get(h,'CurrentAxes');
ch=get(ah,'Children');
set(ch,'FontSize',20);
% 文本居中
th = findall(0, 'Tag','MessageBox' );
boxPosition = get(h,'position');
textPosition = get(th, 'position');
set(th, 'position', [boxPosition(3).*0.5 textPosition(2) textPosition(3)]);
set(th, 'HorizontalAlignment', 'center');
set(h,'Position',[250 250 200 100]);
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in Openin
MATLAB车牌识别-神经网络代码
4星 · 超过85%的资源 需积分: 33 80 浏览量
2018-01-22
11:20:28
上传
评论 33
收藏 3.93MB ZIP 举报
zhangxiang1209
- 粉丝: 2
- 资源: 2