您的位置:首页 > 编程语言 > MATLAB

MATLAB GUI设计手写输入板

2015-10-18 10:43 435 查看
最近要做一些模式识别的课程作业,设计一个手写输入板来实现测试样本的识别,好吧,废话不多说,干货来了。
要实现的目标:
1.实现手写
2.手写的图像能够保存
大概就是下面这个样子
![界面](https://img-blog.csdn.net/20151018102952336)
实现步骤:
1.添加控件,有点类似VB那种,先创建新的GUI界面,然后把控件拖里面去就行。
![创建界面](https://img-blog.csdn.net/20151018103234403)
2.创建一些回调函数
![回调函数](https://img-blog.csdn.net/20151018103457787)
每个回调函数下面可以写函数,用到的回调函数也就那么几个,下面一一介绍
No.1 获取鼠标位置(鼠标按下)
function figure1_WindowButtonDownFcn(hObject, eventdata, 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 draw_enable;
global x;
global y;
draw_enable=1;
if draw_enable
position=get(gca,'currentpoint');
x(1)=position(1);
y(1)=position(3);
end
No.2更新鼠标位置并画线(鼠标在按下的情况下运动)
function figure1_WindowButtonMotionFcn(hObject, eventdata, 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 draw_enable;
global x;
global y;
if draw_enable
position=get(gca,'currentpoint');
x(2)=position(1);
y(2)=position(3);
line(x,y,'EraseMode','xor','LineWidth',5,'color','b');
x(1)=x(2);
y(1)=y(2);
end
No.3鼠标放开后停止画线
function figure1_WindowButtonUpFcn(hObject, eventdata, 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 draw_enable
draw_enable=0;
No.4清除图像(按下清除按键,这里要说明一下创建回调函数的时候要在清除按钮上右键,比较快)
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)
axes(handles.axes1);
cla;
NO.5保存图像(按下保存按键)
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)
h=getframe(handles.axes1);
imwrite(h.cdata,'output.bmp','bmp');
cla(handles.axes1);
将上述函数都一一调用后就可以实现手写输入了,这里要感谢MATLAB中文论坛的大神,上面的源代码基本上是他们原创的,我只是整理,稍微改写添加了一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息