【问题标题】:MATLAB - How to zoom in to mouse positionMATLAB - 如何放大到鼠标位置
【发布时间】:2014-09-20 04:07:32
【问题描述】:

当我左键/右键单击图像上的某个点时,我想放大/缩小图像,但我不想使用放大镜进行放大/缩小。基本上,我需要一个我上面所说的脚本。我想出了以下脚本,但它只放大/缩小到中心而不是我点击的位置。

如果你问我为什么使用ginput 获得点击位置,答案是,我打算使用这个脚本来编辑二进制图像!我已经删除了与二进制图像编辑部分相对应的行以避免任何混淆。

hFi= figure; imshow(e);

button=0;

while button~=2
    % Get the mouse position on the axes (needed for binary image editing) and button number
    [y,x,button]=ginput(1);

    % Get the mouse position on the figure
    position=get(hFi,'CurrentPoint')

    % Set 'CurrentPoint' to the mouse position that was just captured
    set(hFi,'CurrentPoint',position)

    % Determine if it is a zoom-in or zoom-out
    if button==1
        zoom(2);
    elseif button==3
        zoom(0.5);
    end

end

我认为命令set() 在这里没有做任何事情,但我看到有人在论坛中建议。对不起,如果这看起来很愚蠢!

【问题讨论】:

  • 您应该使用回调来执行此操作。我为文本回答了一个类似的问题here,但它可以适用于图像。
  • 谢谢!我会尝试并告诉你。
  • 重读您的问题后,您可能希望专门查看鼠标单击侦听器/回调。类似于this 的东西。 MATLAB 中的回调是在事件之后调用的函数(即,WindowButtonDownFcn 在鼠标单击后被调用)。这是处理此类事件的途径。

标签: matlab zooming mouse ginput


【解决方案1】:

我假设您在询问 y 和 x 值时有一个矩阵。 这样,一个简单的答案就是显示更小或更大范围的值。

让 A 成为你的矩阵。

    A=rand(1000,1000);
figure,
h=imagesc(A)
axis off
axis image
colormap('gray')
A_range=size(A);
A_zoom=1;
while true

    [y,x,button]=ginput(1)

    A_center=[x,y];

    if button==1
        A_zoom=A_zoom/2;
    elseif button==3
        A_zoom=A_zoom*2;
    end

    axis([max(x-A_range(1)*A_zoom,1), min(x+A_range(1)*A_zoom,A_range(1)), ...
        max(y-A_range(2)*A_zoom,1), min(y+A_range(2)*A_zoom,A_range(2))])
end

【讨论】:

  • 您好,感谢您的代码。但是当我尝试您的脚本时,它不会放大。对于缩小,它只会裁剪图像。
  • UPS。对面变焦。现在应该更正。是的,目标是作物。如果您想要不同,则需要进行插值,这将创建人工数据。
  • 这样你会看到它是matlab的正常缩放。
  • @Omid 你让我很好奇。应该是这样的。
  • 忘记添加了。你应该使用imagesc。 imshow 太受限制了,不应该是你想要的。我看不到使用 imshow 的意义。祝你好运
【解决方案2】:

好吧,在玩了imshow 并阅读了互联网上的文档之后,我终于找到了我一直在寻找的东西。感谢所有提出想法的人,特别是 ASantosRibeiro,他给了我改变轴限制的想法。

基本上,我使用了zoomxlim/ylim 命令的组合。因此,最终代码如下所示:

hFi= figure; h=imshow(e);

button=0;
zlvl=1;
xl = get(gca,'xlim');
xlen = size(e,2);
yl = get(gca,'ylim');
ylen = size(e,1);

while button~=2
    % Get the mouse position on the axes (needed for binary image editing) and button number
    [y,x,button]=ginput(1);

    % Determine if it is a zoom-in or zoom-out
    if button==1
        zlvl = zlvl*2;
        zoom(2);
    elseif button==3
        zlvl = zlvl/2;
        if zlvl<1, zlvl=1; end % No zoom level smaller than 1
        zoom(0.5);
    end

    % Change the axis limits to where the mouse click has occurred
    % and make sure that the display window is within the image dimensions
    xlimit = [x-xlen/zlvl/2+0.5 x+xlen/zlvl/2+0.5];
    if xlimit(1)<0.5, xlimit=[0.5 xlen/zlvl+0.5]; end
    if xlimit(2)>0.5+xlen, xlimit=[xlen-xlen/zlvl+0.5 xlen+0.5]; end
    xlim(xlimit);

    ylimit = [y-ylen/zlvl/2+0.5 y+ylen/zlvl/2+0.5];
    if ylimit(1)<=0.5, ylimit=[0.5 ylen/zlvl+0.5]; end
    if ylimit(2)>=0.5+ylen, ylimit=[ylen-ylen/zlvl+0.5 ylen+0.5]; end
    ylim(ylimit);
end

此代码可让您在使用ginput 时放大/缩小,并且您需要使用imshow 来保持图像的纵横比固定。当您需要读取图像像素的坐标并放大/缩小时,这将变得非常有用。当用户左键单击时它会放大,当用户左键单击时它会缩小。如果单击中间按钮,则 while 循环结束。可以添加其他按钮来执行所需的操作。例如,我添加了几行代码来编辑二进制图像。

我想知道您的想法,以及是否有任何方法可以使这段代码更高效。

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多