在Matlab中追踪包含背景图像的图形

时间:2018-07-11 16:52:49

标签: matlab user-interface plot event-handling matlab-figure

我正在尝试创建函数,以允许用户在matlab中跟踪图像并实时记录/绘制其在该图像顶部的坐标。

遵循本指南:https://www.mathworks.com/videos/capture-mouse-movement-97322.html,我能够创建以下功能性概念验证代码,该代码实时记录和绘制用户在图形中跟踪的图形,而背景中没有图像。

function traceVelocityCurve()

    fig = figure;

    plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1], 'ButtonDownFcn', @startDragFcn);
    hold all

    xValues = [];
    yValues = [];

    tracing = scatter(xValues, yValues);
    tracing.Marker = '.';
    tracing.MarkerFaceColor = 'red';
    tracing.MarkerEdgeColor = 'red';


    set(fig, 'WindowButtonUpFcn', @stopDragFcn);

        function startDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', @draggingFcn);

        end

        function draggingFcn(varargin)

            point = get(plotAxes, 'CurrentPoint');

            xValues = [xValues; point(1, 1)];
            yValues = [yValues; point(1, 2)];

            set(tracing, 'XData', xValues);
            set(tracing, 'YData', yValues);


        end


        function stopDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', '');

        end

end

也可以删除对scatter()的调用,并替换为对plot()的调用。我相信这将更符合在图像顶部进行绘图的要求:

function traceVelocityCurve()

    fig = figure;

    plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1], 'ButtonDownFcn', @startDragFcn);
    hold all

    xValues = [];
    yValues = [];


    set(fig, 'WindowButtonUpFcn', @stopDragFcn);

        function startDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', @draggingFcn);

        end

        function draggingFcn(varargin)

            point = get(plotAxes, 'CurrentPoint');

            xValues = [xValues; point(1, 1)];
            yValues = [yValues; point(1, 2)];

            plot(xValues, yValues, 'Color', 'red');


        end


        function stopDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', '');

        end

end

但是,如果我尝试首先导入图像并将其设置为图形背景,然后尝试在同一图形中的该图像的顶部进行跟踪/绘图,则无法执行此操作。例如,以下代码在我的图形的背景中正确显示了一个图像,但是我失去了跟踪功能(单击并在图形上拖动并没有绘制任何内容):

function traceVelocityCurve()

    fig = figure;

    plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1], 'ButtonDownFcn', @startDragFcn);
    loadedImage = imread('index.jpg');
    image(loadedImage, 'Parent', plotAxes);

    hold all

    xValues = [];
    yValues = [];


    set(fig, 'WindowButtonUpFcn', @stopDragFcn);

        function startDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', @draggingFcn);

        end

        function draggingFcn(varargin)

            point = get(plotAxes, 'CurrentPoint');

            xValues = [xValues; point(1, 1)];
            yValues = [yValues; point(1, 2)];

            plot(xValues, yValues, 'Color', 'red');


        end


        function stopDragFcn(varargin)

            set(fig, 'WindowButtonMotionFcn', '');

        end

end

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

通过在轴上添加图像,您已经完成了图像制作,因此您无法再单击轴。单击时,图像会发生事件,但轴不再发生。

但是,人物总是会得到事件。将图形的'WindowButtonDownFcn'设置为startDragFcn,它将起作用:

function traceVelocityCurve()
    fig = figure;
    plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1]);
    % ...
    set(fig, 'WindowButtonDownFcn', @startDragFcn);
    set(fig, 'WindowButtonUpFcn', @stopDragFcn);
    % ...