在 matlab 中追踪包含背景图像的图形
Tracing over a figure containing a background image in matlab
我正在尝试创建函数以允许用户在 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
提前感谢您的帮助!
通过在坐标轴中添加图像,您就无法再点击坐标轴了。单击时,图像会发生事件,但轴不再发生。
然而,人物也总是得到事件。将图形的 'WindowButtonDownFcn'
设置为您的 startDragFcn
,它将起作用:
function traceVelocityCurve()
fig = figure;
plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1]);
% ...
set(fig, 'WindowButtonDownFcn', @startDragFcn);
set(fig, 'WindowButtonUpFcn', @stopDragFcn);
% ...
我正在尝试创建函数以允许用户在 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
提前感谢您的帮助!
通过在坐标轴中添加图像,您就无法再点击坐标轴了。单击时,图像会发生事件,但轴不再发生。
然而,人物也总是得到事件。将图形的 'WindowButtonDownFcn'
设置为您的 startDragFcn
,它将起作用:
function traceVelocityCurve()
fig = figure;
plotAxes = axes('Xlim', [0, 1], 'Ylim', [0 1]);
% ...
set(fig, 'WindowButtonDownFcn', @startDragFcn);
set(fig, 'WindowButtonUpFcn', @stopDragFcn);
% ...