matlab - 加速 impoint 对象的创建

标签 matlab image-processing matlab-figure

我必须在轴上创建一些可拖动的点。然而,这似乎是一个非常缓慢的过程,在我的机器上这样做需要一秒多一点:

x = rand(100,1);
y = rand(100,1);

tic;
for i = 1:100
    h(i) = impoint(gca, x(i), y(i));
end
toc;

任何关于加速的想法将不胜感激。

这个想法只是为用户提供纠正先前由 Matlab 计算的图形位置的可能性,这里以随机数为例。

最佳答案

您可以使用 ginput while 循环中的光标以标记要编辑的所有点。之后只需在轴外单击即可离开循环,移动点并使用任意键接受。

f = figure(1);
scatter(x,y);
ax = gca;
i = 1;
 while 1
        [u,v] = ginput(1);
        if ~inpolygon(u,v,ax.XLim,ax.YLim); break; end;
        [~, ind] = min(hypot(x-u,y-v));
        h(i).handle = impoint(gca, x(ind), y(ind));
        h(i).index  = ind;
        i = i + 1;
 end

enter image description here


根据您更新绘图的方式,您可以通过使用类似 clf 的函数获得总体加速。 (清晰的数字)和cla (清除轴)而不是总是打开一个新的图形窗口,如 this answer 中所述可能有用。


或者,以下是我在评论中的意思的一个非常粗略的想法。它会抛出各种错误,我现在没有时间调试它。但也许它有助于作为一个起点。

1) 数据的常规绘制和datacursormode的激活

x = rand(100,1);
y = rand(100,1);
xlim([0 1]); ylim([0 1])

f = figure(1)
scatter(x,y)

datacursormode on
dcm = datacursormode(f);
set(dcm,'DisplayStyle','datatip','Enable','on','UpdateFcn',@customUpdateFunction)

2) 自定义更新函数评估所选数据提示并创建一个impoint

function txt = customUpdateFunction(empt,event_obj)

pos = get(event_obj,'Position');
ax = get(event_obj.Target,'parent');
sc = get(ax,'children');

x = sc.XData;
y = sc.YData;
mask = x == pos(1) & y == pos(2);
x(mask) = NaN;
y(mask) = NaN;
set(sc, 'XData', x, 'YData', y);
set(datacursormode(gcf),'Enable','off')

impoint(ax, pos(1),pos(2));
delete(findall(ax,'Type','hggroup','HandleVisibility','off'));

txt = {};

它适用于,如果您只想移动一个点。重新激活数据光标模式并设置第二个点失败:

enter image description here

也许你能找到错误。

关于matlab - 加速 impoint 对象的创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288280/

相关文章:

matlab - 围绕零值自动构建颜色图

matlab - 为每个图像执行与新向量的卷积的最佳方法?

c++ - 来自图像的简单视频制作者

matlab - 在 MATLAB 中使用 surf 创建沿 z 轴的堆叠二维矩阵

matlab - 从现有绘图中提取 MATLAB 图例字符串

matlab - 使用隐马尔可夫模型的手势识别

matlab - 使用 mex 链接和加载静态 .lib

python - 在圆上区分矩形(python-openCV)

java - 如何检测二值图像中的圆圈

matlab - Matlab 中条形组的自定义颜色