1 基于模板匹配实现手写数字识别模型
模型参考这里。
2 部分代码
function varargout = NumDemo(varargin) % NUMDEMO MATLAB code for NumDemo.fig % NUMDEMO, by itself, creates a new NUMDEMO or raises the existing % singleton*. % % H = NUMDEMO returns the handle to a new NUMDEMO or the handle to % the existing singleton*. % % NUMDEMO('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in NUMDEMO.M with the given input arguments. % % NUMDEMO('Property','Value',...) creates a new NUMDEMO or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before NumDemo_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to NumDemo_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 NumDemo % Last Modified by GUIDE v2.5 28-Apr-2020 11:22:33 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @NumDemo_OpeningFcn, ... 'gui_OutputFcn', @NumDemo_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 NumDemo is made visible. function NumDemo_OpeningFcn(hObject, ~, 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 NumDemo (see VARARGIN) % Choose default command line output for NumDemo handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes NumDemo wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = NumDemo_OutputFcn(~, ~, 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; axis([0 250 0 250]); clc; % --- Executes on mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonDownFcn(~, ~, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global ButtonDown pos1 if strcmp(get(gcf, 'SelectionType'), 'normal') ButtonDown = 1; pos1 = get(handles.axes1, 'CurrentPoint'); end % --- Executes on mouse motion over figure - except title and menu. function figure1_WindowButtonMotionFcn(~, ~, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global ButtonDown pos1 if(ButtonDown == 1) pos = get(handles.axes1,'CurrentPoint'); line([pos1(1,1) pos(1,1)], [pos1(1,2) pos(1,2)], 'Color','Black','LineStyle','-', 'LineWidth',8,'Marker','.', 'MarkerSize',20); pos1 = pos; end % --- Executes on mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonUpFcn(~, ~, ~) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global ButtonDown ButtonDown = 0; % --- Executes on button press in pushbutton_clear. function pushbutton_clear_Callback(~, ~, handles) % hObject handle to pushbutton_clear (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %cla; cla(handles.axes1); % --- Executes on button press in pushbutton_save. function pushbutton_save_Callback(~, ~, handles) % hObject handle to pushbutton_save (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [f, p] = uiputfile({'*.bmp'},'Save Image'); str = strcat(p, f); px = getframe(handles.axes3); curImg = frame2im(px); imwrite(curImg, str,'bmp'); % --- Executes on button press in pushbutton_classify. function pushbutton_classify_Callback(~, ~, handles) % hObject handle to pushbutton_classify (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %strSample = 'Data.mat'; cla(handles.axes4,handles.axes5,handles.axes6); load 'Data.mat'; I1=handles.I1; curFea = GetFeature(I1); I2 = Normalize_Img(I1); bw1 = Bw_Img(I1); bw2 = Thin_Img(bw1); bw = bw2; sz = size(bw); [r, c] = find(bw==1); rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)]; vs = rect(1)+rect(3)*[5/12 1/2 7/12]; hs = rect(2)+rect(4)*[1/3 1/2 2/3]; pt1 = [rect(1:2); rect(1:2)+rect(3:4)]; pt2 = [rect(1)+rect(3) rect(2); rect(1) rect(2)+rect(4)]; k1 = (pt1(1,2)-pt1(2,2)) / (pt1(1,1)-pt1(2,1)); x1 = 1:sz(2); y1 = k1*(x1-pt1(1,1)) + pt1(1,2); k2 = (pt2(1,2)-pt2(2,2)) / (pt2(1,1)-pt2(2,1)); x2 = 1:sz(2); y2 = k2*(x2-pt2(1,1)) + pt2(1,2); axes(handles.axes4); imshow(I2,[]); handles.I2 = I2; hold on; h = rectangle('Position', [rect(1:2)-1 rect(3:4)+2], 'EdgeColor', 'r', 'LineWidth', 2); legend(h, '数字区域标记', 'Location', 'South'); axes(handles.axes5); imshow(bw1,[]); handles.bw1 = bw1; axes(handles.axes6); imshow(bw,[],'Border', 'Loose'); handles.bw = bw; hold on; h = []; for i = 1 : length(hs) h = [h plot([1 sz(2)], [hs(i) hs(i)], 'r-')]; end for i = 1 : length(vs) h = [h plot([vs(i) vs(i)], [1 sz(1)], 'g-')]; end h = [h plot(x1, y1, 'y-')]; h = [h plot(x2, y2, 'm-')]; legend([h(1) h(4) h(7) h(8)], {'水平线', '竖直线', '左对角线', '右对角线'}, 'Location', 'BestOutside'); hold off; %yLabel = euclid(curFea); yLabel = MaskRecon(Data, curFea); h = warndlg(num2str(yLabel),'识别结果'); % --- Executes on mouse press over axes background. function axes1_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to axes1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton_clear. function pushbutton_clear_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to pushbutton_clear (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [FileName,PathName,FilterIndex] = uigetfile({'*.jpg;*.tif;*.png;*.gif', ... '所有图像文件';... '*.*','所有文件' },'载入数字图像',... '.\\images\\手写数字\\t0.jpg'); if isequal(FileName, 0) || isequal(PathName, 0) return; end fileName = fullfile(PathName, FileName); I = imread(fileName); axes(handles.axes2); imshow(I); handles.I = I; guidata(hObject, handles); % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (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; axes(handles.axes2); I1 = imcrop(I); axes(handles.axes3); imshow(I1); handles.I1 = I1; guidata(hObject, handles); % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) I = getframe(handles.axes1); handles.I=I; I1 = frame2im(I); axes(handles.axes3); imshow(I1); handles.I1 = I1; guidata(hObject, handles);
3 运行结果
4 参考文献
[1]李天. 基于模版匹配的联机手写数字识别系统设计[J]. 计算机与网络, 2009.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。