Mtalab鼠标动态操作

function  MouseDraw(action)
% 本程序在鼠标移动轨迹记录
% global不能传矩阵
global InitialX InitialY FigHandle
imSize = 200;
if nargin == 0, action = 'start';   end

switch(action)
    %%开启图形视窗
    case 'start',
        FigHandle = figure('WindowButtonDownFcn','MouseDraw down');
        axis([1 imSize 1 imSize]);    % 设定图轴范围
%         axis off;
        grid on;
        box on;     % 将图轴加上图框
        title('手写体输入窗');
% %         fprintf('start');
        %%设定滑鼠按钮被按下时的反应指令为「MouseDraw down」
        % set(gcf, 'WindowButtonDownFcn', 'MouseDraw down'); 
dlmwrite('IXT.txt', -10, 'delimiter', '\t', 'precision', 6);
        dlmwrite('IYT.txt', -10, 'delimiter', '\t', 'precision', 6);
        %%滑鼠按钮被按下时的反应指令
    case 'down',
        if strcmp(get(FigHandle, 'SelectionType'), 'normal')    %如果是左键
            set(FigHandle,'pointer','hand');     
            CurPiont = get(gca, 'CurrentPoint');
            InitialX = CurPiont(1,1);
            InitialY = CurPiont(1,2);
            dlmwrite('IXT.txt', InitialX, '-append', 'delimiter', '\t', 'precision', 6);
            dlmwrite('IYT.txt', InitialY, '-append', 'delimiter', '\t', 'precision', 6);
            % 列印「MouseDraw down!」讯息
% %             fprintf('MouseDraw down!\n');
            % 设定滑鼠移动时的反应指令为「MouseDraw move」
            set(gcf, 'WindowButtonMotionFcn', 'MouseDraw move');
            set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
        elseif strcmp(get(FigHandle, 'SelectionType'), 'alt')   % 如果是右键
            set(FigHandle, 'Pointer', 'arrow');
            set( FigHandle, 'WindowButtonMotionFcn', '')
            set(FigHandle, 'WindowButtonUpFcn', '')
            fprintf('MouseDraw right button down!\n');
            ImageX = importdata('IXT.txt');
            ImageY = importdata('IYT.txt');
            InputImage = ones(imSize);
            roundX = round(ImageX);
            roundY = round(ImageY);
            for k = 1:size(ImageX,1)
                if 0<roundX(k) && roundX(k)<imSize && 0<roundY(k) && roundY(k)<imSize
                    InputImage(roundX(k)-1:roundX(k)+2, roundY(k)-1:roundY(k)+2) = 0;
                end
            end
            InputImage = imrotate(InputImage,90);       % 图像旋转90
            figure(2);
            imshow(InputImage);
        end
    %%滑鼠移动时的反应指令
    case 'move',
        CurPiont = get(gca, 'CurrentPoint');
        X = CurPiont(1,1);
        Y = CurPiont(1,2);
        % 当鼠标移动较快时,不会出现离散点。
        % 利用y=kx+b直线方程实现。
        x_gap = 0.1;    % 定义x方向增量
        y_gap = 0.1;    % 定义y方向增量
        if X > InitialX
            step_x = x_gap;
        else
            step_x = -x_gap;
        end
        if Y > InitialY
            step_y = y_gap;
        else
            step_y = -y_gap;
        end 
        % 定义x,y的变化范围和步长
        if abs(X-InitialX) < 0.01        % 线平行于y轴,即斜率不存在时
            iy = InitialY:step_y:Y;
            ix = X.*ones(1,size(iy,2));
        else
            ix = InitialX:step_x:X ;    % 定义x的变化范围和步长
            % 当斜率存在,即k = (Y-InitialY)/(X-InitialX) ~= 0
            iy = (Y-InitialY)/(X-InitialX).*(ix-InitialX)+InitialY;  
        end
        ImageX = [ix, X];
        ImageY = cat(2, iy, Y);
        line(ImageX,ImageY, 'marker', '.', 'markerSize',28, ...
            'LineStyle', '-', 'LineWidth', 4, 'Color', 'Red');
        dlmwrite('IXT.txt', ImageX, '-append', 'delimiter', '\t', 'precision', 6);
        dlmwrite('IYT.txt', ImageY, '-append', 'delimiter', '\t', 'precision', 6);
        InitialX = X;       %记住当前点坐标
        InitialY = Y;       %记住当前点坐标
        % 列印「MouseDraw is moving!」及滑鼠现在位置
        % fprintf('MouseDraw is moving! Current location = (%g, %g)\n', ...
          % CurPiont(1,1), CurPiont(1,2));
% %         fprintf('MouseDraw move!\n');
        % 设定滑鼠按钮被释放时的反应指令为「MouseDraw up」
        % set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
    %%滑鼠按钮被释放时的反应指令
    case 'up',
        % 清除滑鼠移动时的反应指令
        set(gcf, 'WindowButtonMotionFcn', '');
        % 清除滑鼠按钮被释放时的反应指令
        set(gcf, 'WindowButtonUpFcn', '');
        % 列印「MouseDraw up!」
% %         fprintf('MouseDraw up!\n');
end

end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值