【问题标题】:Zooming in/out while using ginput function in MATLAB在 MATLAB 中使用 ginput 函数时放大/缩小
【发布时间】:2015-12-13 18:04:10
【问题描述】:

我在 MATLAB 中使用ginput 函数来使用光标来收集图像上的许多 x,y 坐标。我沿着图像沿着特定路径移动,需要放大以获得精确的坐标,但是在使用 ginput 时,放大选项被禁用。关于如何解决这个问题的任何想法?

这是我正在使用的非常简单的代码。

A = imread('image1.tif');
B = imshow(A);
[x,y] = ginput; 
% at this point i scan the image and click many times, and
% ideally i would like to be able to zoom in and out of the image to better 
% aim the cursor and obtain precise xy coordinates

【问题讨论】:

    标签: matlab image-processing coordinates zooming ginput


    【解决方案1】:

    我认为这样做的方法是利用ginput函数的“按钮”输出,即,

    [x,y,b]=ginput;

    b 返回被按下的鼠标按钮或键盘键。选择您最喜欢的两个键(例如 [ 和 ],字符 91 和 93)并编写一些放大/缩小代码来处理按下这些键时发生的情况:

    A = imread('image1.png');
    B = imshow(A);
    X = []; Y = [];
    while 0<1
        [x,y,b] = ginput(1); 
        if isempty(b); 
            break;
        elseif b==91;
            ax = axis; width=ax(2)-ax(1); height=ax(4)-ax(3);
            axis([x-width/2 x+width/2 y-height/2 y+height/2]);
            zoom(1/2);
        elseif b==93;
            ax = axis; width=ax(2)-ax(1); height=ax(4)-ax(3);
            axis([x-width/2 x+width/2 y-height/2 y+height/2]);
            zoom(2);    
        else
            X=[X;x];
            Y=[Y;y];
        end;
    end
    [X Y]
    

    ginput(1) 中的 (1) 很重要,这样您一次只能获得一次点击/按键。

    enter键是默认的ginputbreak键,返回一个空的b,由第一个if语句处理。

    如果按下键 91 或 93,我们分别缩小 (zoom(1/2)) 或放大 (zoom(2))。使用axis 的几行将绘图置于光标的中心,这很重要,因此您可以放大图像的特定部分。

    否则,将光标坐标x,y 添加到您的坐标集X,Y

    【讨论】:

      【解决方案2】:

      作为替代方法,您可以使用 MATLAB 的 datacursormode 对象,它不会阻止您的图形窗口的缩放/平移。

      一个小例子(假设 R2014b 或更新版本):

      h.myfig = figure;
      h.myax = axes;
      
      plot(h.myax, 1:10);
      
      % Initialize data cursor object
      cursorobj = datacursormode(h.myfig);
      cursorobj.SnapToDataVertex = 'on'; % Snap to our plotted data, on by default
      
      while ~waitforbuttonpress 
          % waitforbuttonpress returns 0 with click, 1 with key press
          % Does not trigger on ctrl, shift, alt, caps lock, num lock, or scroll lock
          cursorobj.Enable = 'on'; % Turn on the data cursor, hold alt to select multiple points
      end
      cursorobj.Enable = 'off';
      
      mypoints = getCursorInfo(cursorobj);
      

      请注意,您可能需要在图形窗口内单击才能触发数据光标。

      这里我使用waitforbuttonpress 来控制我们的while 循环。正如所评论的,waitforbuttonpress 返回0 用于单击鼠标按钮,1 用于按键按下,但不是由 CtrlShiftAltCapsNumScrLk 键。您可以通过在单击时按住 Alt 来选择多个点。

      选择完点后,点击触发waitforbuttonpress 的任意键,您的数据点就会通过调用getCursorInfo 输出,这会返回一个包含点信息的数据结构。此数据结构包含 2 或 3 个字段,具体取决于绘制的内容。该结构将始终包含Target,它是包含数据点的图形对象的句柄,以及Position,它是一个指定光标的x、y(和z)坐标的数组。如果您绘制了linelineseries 对象,您还将拥有DataIndex,它是对应于最近数据点的数据数组的标量索引。

      【讨论】:

      • 这是更优雅的解决方案。我不知道该怎么做。 +1。
      • @excaza 我尝试使用它,但对我来说它实际上不起作用。当我单击缩放符号然后单击图像中的某个点时,它会自动切换到光标而不放大。我做错了什么吗?顺便说一句:如果这很重要,我会在 Mac 上使用 R2016a。
      【解决方案3】:

      还有一种方法是使用 enableDefaultInteractivity() 函数,它可以使用鼠标滚轮进行放大和缩小:

      enableDefaultInteractivity(gca);
      [x, y, b] = ginput();
      

      它适用于我的 Matlab 版本(9.9.0.1524771 (R2020b) Update 2,在 Linux Fedora 上)。

      【讨论】:

        猜你喜欢
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多