gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:20 24 4
gpt4 key购买 nike

我正在尝试创建函数以允许用户在 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 - 在 matlab 中追踪包含背景图像的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51290703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com