用Matlab记录运动时用鼠标画线

时间:2019-04-28 15:09:43

标签: matlab drawing mouseevent matlab-figure

我正在尝试制作签名记录程序。它应该能够使用鼠标/数位板画线,同时实时记录运动。

基于此文件:https://www.mathworks.com/matlabcentral/fileexchange/16539-mouseinput_timeout,我已经对其进行了修改,以便它可以记录多行。

%to call: out = mouseinput_timeout(timeoutval)
function selectedPts = mouseinput_timeout(timeoutval, axesHandle)

startTime = tic; %time start
a = 0;

if ~exist('axesHandle', 'var')
    axesHandle = gca;
end
if ~exist('timeoutval', 'var') 
    timeoutval = inf;
end
if ~(ishandle(axesHandle) && strcmp(get(axesHandle,'type'),'axes'))
    error('Axis handle is not valid');
end
if ~(isscalar(timeoutval) && (timeoutval > 0))
    error('Timeout should be a positive scalar');
end

figHandle = get(axesHandle, 'parent');
selectedPts = [];

oldProperties = get(figHandle, ...
    {'WindowButtonDownFcn','WindowButtonUpFcn',...
     'WindowButtonMotionFcn', 'units','pointer'});

set(figHandle, ...
    {'WindowButtonDownFcn','WindowButtonUpFcn',...
     'WindowButtonMotionFcn','units','pointer'}, ...
    { @buttonDownCallback, @buttonUpCallback, ...
      [], 'pixels', 'crosshair'}); 
figLocation = get(figHandle, 'Position'); 

if isinf(timeoutval)
    uiwait(figHandle);
else
    uiwait(figHandle, timeoutval);
end

set(figHandle, ...
    {'WindowButtonDownFcn','WindowButtonUpFcn',...
     'WindowButtonMotionFcn', 'units','pointer'}, ...
     oldProperties);

    function buttonMotionCallback(obj, eventdata) %#ok<INUSD>
        pt = mapCurrentPosition();
        selectedPts(end+1,:) = [pt(1,1:2) toc(startTime)];
    end

    function buttonStopCallback(obj, eventdata) %#ok<INUSD>
        selectedPts(end+1,:) = [0 0 toc(startTime)];
    end

    function buttonDownCallback(obj, eventdata) %#ok<INUSD>
        a = a+1;
        if a == 1
            selectedPts = [];
            set(obj, 'WindowButtonMotionFcn', @buttonMotionCallback);
        else
            set(obj, 'WindowButtonMotionFcn', @buttonMotionCallback);
        end
    end

    function buttonUpCallback(obj, eventdata) %#ok<INUSD>
        set(obj, 'WindowButtonMotionFcn', @buttonStopCallback);
    end

    function pt = mapCurrentPosition()
        scrn_pt = get(0, 'PointerLocation');              
        set(figHandle,'CurrentPoint',...
            [scrn_pt(1) - figLocation(1) + 1, ...
             scrn_pt(2) - figLocation(2) + 1]);
        pt = get(axesHandle,'CurrentPoint');       
    end
end

但是,该程序尚不能绘制可见线。我正在尝试将其与https://www.mathworks.com/matlabcentral/answers/75525-freehand-drawing-in-axeshttps://www.mathworks.com/matlabcentral/fileexchange/70372-matlab-paint结合使用,但尚未成功。

function buttonMotionCallback(obj, eventdata) %#ok<INUSD>
    pt = mapCurrentPosition();
    line(pt(1,1),pt(1,2),'Color','k');
    selectedPts(end+1,:) = [pt(1,1:2) toc(startTime)];
end

先谢谢您。任何帮助表示赞赏。

0 个答案:

没有答案