r9 fan
r9 fan

Reputation: 177

Zooming in/out while using ginput function in MATLAB

I am using the ginput function in MATLAB to use the cursor to collect many x,y coordinates on images. I am following a certain path along the image and need to zoom-in to get a precise coordinates, but the zoom-in option is disabled while using ginput. Any ideas of how to get around this issue?

Here is the very simple code I am using.

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

Upvotes: 3

Views: 8418

Answers (3)

Riccardo Scorretti
Riccardo Scorretti

Reputation: 1

Still another method could be to use the function enableDefaultInteractivity() which enables using the mouse wheel to zoom in and out:

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

It works fine with my version of Matlab (9.9.0.1524771 (R2020b) Update 2, on Linux Fedora).

Upvotes: 0

Geoff
Geoff

Reputation: 1212

I think the way to do it is to take advantage of the "button" output of the ginput function, i.e.,

[x,y,b]=ginput;

b returns the mouse button or keyboard key that was pressed. Choose your two favorite keys (e.g. [ and ], characters 91 and 93) and write some zoom in/zoom out code to handle what happens when those keys are pressed:

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]

The (1) in ginput(1) is important so that you're only getting one click/keypress at a time.

The enter key is the default ginput break key, which returns an empty b, and is handled by the first if statement.

If key 91 or 93 was pressed, we zoom out (zoom(1/2)) or zoom in (zoom(2)), respectively. The couple lines using axis center the plot on your cursor, and are important so you can zoom on particular parts of the image.

Else, add the cursor coordinates x,y to your set of coordinates X,Y.

Upvotes: 6

sco1
sco1

Reputation: 12214

As an alternative method you can utilize MATLAB's datacursormode object, which does not block zooming/panning of your figure window.

A small example (assumes R2014b or newer):

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);

Note that you may need to click inside the figure window to trigger the data cursor.

Here I use waitforbuttonpress to control our while loop. As commented, waitforbuttonpress returns 0 for a mouse button click and 1 for a key press, but is not triggered by the Ctrl, Shift, Alt, Caps, Num, or ScrLk keys. You can select multiple points by holding Alt while you click.

Once you're done selecting points, hit any key that triggers waitforbuttonpress and your data points are output by calling getCursorInfo, which returns a data structure containing information on your points. This data structure contains 2 or 3 fields, depending on what is plotted. The structure will always contain Target, which is the handle of the graphics object containing the data point, and Position, which is an array specifying the x, y, (and z) coordinates of the cursor. If you have plotted a line or lineseries object you will also have DataIndex, which is a scalar index into the data arrays that correspond to the nearest data point.

Upvotes: 3

Related Questions