matlabzoomingmouseginput

MATLAB - How to zoom in to mouse position


have an image that I would like to zoom in/out when I left/Right-click a point on the image but I do not want to do it using the magnifying glass. Basically, I need a script for what I said above. I have come up with the following script but it only zooms in/out to the center not to the position that I click on.

And if you are asking why I am getting the clicked position using ginput the answer is, I am planning to use this script to edit a binary image! I have removed the lines corresponding to the binary image editing part to avoid any confusion.

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

I think the command set() does not do anything here but I saw someone suggested that in a forum. Sorry if that looks dumb!


Solution

  • Alright, after playing around with imshow and reading through the documentation on internet, I finally came up with what I had been looking for. Thanks to everyone who suggested some ideas, especially ASantosRibeiro who gave me the idea of changing the axis limits.

    Basically, I used a combination of commands zoom and xlim/ylim. Therefore, the final code would look like:

    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
    

    This code lets you zoom in/out when you use ginput and you need to use imshow to keep the aspect ratio of image fixed. This becomes very helpful when you need to read coordinates of image pixels and zoom in/out as well. It zooms in when the user left-clicks and zooms out when the user left-clicks. If the middle button is clicked the while loop ends. Other buttons can be added to perform desired operations. For example, I added a few lines of code to edit binary images.

    I would like to know your thoughts and if there is any way to make this code more efficient.