在MATLAB中使用ginput函数时放大/缩小

问题描述 投票:3回答:2

我正在使用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
2个回答
6
投票

我认为这样做的方法是利用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]

(1)中的ginput(1)很重要,因此一次只需要单击一下/按下一次即可。

Enter键是默认的ginput中断键,它返回空的b,并由第一个if语句处理。

如果按下键91或93,我们分别缩小(zoom(1/2))或放大(zoom(2))。使用axis的几行光标位于图形的中心,并且非常重要,因此可以缩放图像的特定部分。

否则,将光标坐标x,y添加到坐标集X,Y中。


3
投票

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

一个小例子(假设使用R2014b或更高版本:]:>

datacursormode

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

这里我使用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循环。如前所述,waitforbuttonpress返回while表示鼠标单击,waitforbuttonpress表示按键,但不是由Ctrl

ShiftAlt CapsNumScrLk键。单击时按住Alt可以选择多个点。

选择完点后,请按任何触发0的键,并通过调用1输出数据点,该数据将返回包含有关点信息的数据结构。该数据结构包含2或3个字段,具体取决于绘制的内容。该结构将始终包含[​​C0](是包含数据点的图形对象的句柄)和waitforbuttonpress(这是指定光标的x,y和(和z)坐标的数组)。如果已绘制getCursorInfoTarget对象,则还将具有Position,它是对应于最接近数据点的数据数组中的标量索引。

© www.soinside.com 2019 - 2024. All rights reserved.