matlabimage-processingcoordinateszoomingginput

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

Solution

  • 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.