MATLAB GUI设计手写输入板

最近要做一些模式识别的课程作业,设计一个手写输入板来实现测试样本的识别,好吧,废话不多说,干货来了。
要实现的目标:
1.实现手写
2.手写的图像能够保存
大概就是下面这个样子
![界面](https://img-blog.youkuaiyun.com/20151018102952336)
实现步骤:
1.添加控件,有点类似VB那种,先创建新的GUI界面,然后把控件拖里面去就行。
![创建界面](https://img-blog.youkuaiyun.com/20151018103234403)
2.创建一些回调函数
![回调函数](https://img-blog.youkuaiyun.com/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 hand
### MATLAB 中的手写输入设计MATLAB中创建手写输入主要涉及图形用户界面(GUI)的设计以及鼠标事件的捕捉。通过这些技术,能够实现对手写字迹的有效获取并用于后续处理如神经网络识别等操作[^1]。 #### 创建基本GUI布局 利用GUIDE工具或者App Designer来构建应用程序的基础结构,在界面上放置必要的组件比如Axes对象作为绘图区,按钮控件供清除画布等功能使用。 #### 实现鼠绘制功能的关键在于响应鼠标动作: - **按下**(ButtonDownFcn): 记录起始位置; - **移动**(WindowButtonMotionFcn): 当持续按住左键拖动时更新线条路径; - **释放**(WindowButtonUpFcn): 结束当前笔划记录; 对于上述三个阶段的操作均需设置对应的回调函数以便实时监听用户的交互行为,并据此调整显示内容。 下面给出一段简化版示例代码展示如何捕获鼠标轨迹并在指定区域内呈现出来: ```matlab function HandwritingPanel() % 初始化窗口参数 fig = figure('Name','Handwritten Digit Input Panel',... 'NumberTitle','off',... 'Position',[300 300 480 480]); ax = axes('Parent',fig,'Units','normalized',... 'Position',[0.1 0.1 0.8 0.8],... 'XLim',[0 1],'YLim',[0 1],... 'Visible','on'); hold(ax,'all'); axis off; whitebg(gcf,'w'); % 定义全局变量存储坐标数据 global points; set(fig,'WindowButtonDownFcn',{@startDraw},'WindowButtonUpFcn',{@endDraw}); uicontrol('Style','pushbutton',... 'String','Clear',... 'Callback',{@clearCanvas},'Position',[20 20 60 30]); function startDraw(~,~) cla reset;hold on; set(gcf,'WindowButtonMotionFcn',{@drawLine}); global points; points=[]; end function drawLine(~,eventdata) pos=get(eventdata.Source,'CurrentPoint'); x=pos(1); y=flipud(pos(:,2)); global points; points=[points;x]; plot(points(:,1),y,'k.','MarkerSize',5); end function endDraw(~,~) set(gcf,'WindowButtonMotionFcn',''); end function clearCanvas(~,~) cla reset; global points; points=[]; end end ``` 此段程序定义了一个简单的手写应用,其中包含了启动绘画(`startDraw`)、连续描边(`drawLine`) 和停止绘画 (`endDraw`) 的逻辑流程控制,同时还提供了一个清屏按键用于重置工作状态。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值