0

当我左键/右键单击图像上的某个点时,我想放大/缩小图像,但我不想使用放大镜进行操作。基本上,我需要一个我上面所说的脚本。我想出了以下脚本,但它只放大/缩小到中心而不是我点击的位置。

如果您问我为什么使用ginput答案获得点击位置,我打算使用此脚本来编辑二进制图像!我已经删除了与二进制图像编辑部分相对应的行以避免任何混淆。

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

我认为该命令set()在这里没有做任何事情,但我看到有人在论坛上建议。对不起,如果这看起来很愚蠢!

4

2 回答 2

0

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.

于 2014-09-19T19:59:09.933 回答
0

我假设您在询问 y 和 x 值时有一个矩阵。以这种方式,一个简单的答案是显示更小或更大范围的值。

让 A 成为你的矩阵。

    A=rand(1000,1000);
figure,
h=imagesc(A)
axis off
axis image
colormap('gray')
A_range=size(A);
A_zoom=1;
while true

    [y,x,button]=ginput(1)

    A_center=[x,y];

    if button==1
        A_zoom=A_zoom/2;
    elseif button==3
        A_zoom=A_zoom*2;
    end

    axis([max(x-A_range(1)*A_zoom,1), min(x+A_range(1)*A_zoom,A_range(1)), ...
        max(y-A_range(2)*A_zoom,1), min(y+A_range(2)*A_zoom,A_range(2))])
end
于 2014-09-19T15:39:16.460 回答