开发者

image is too big to fit in the screen (MATLAB)

开发者 https://www.devze.com 2023-04-04 19:54 出处:网络
I know that it is just a warning and it will not affect the code .. but my problem is that I need to show the image in its real siz开发者_C百科e without any zooming out .. is that possible in imshow f

I know that it is just a warning and it will not affect the code .. but my problem is that I need to show the image in its real siz开发者_C百科e without any zooming out .. is that possible in imshow function are there any parameters that do this?

thank you all


The solution given by @Jonas, which I already upvoted, is really good. Let me suggest some minor improvements so that it handles the case when the figure is resized:

%# read an image and make it large
img = imread('autumn.tif');
img = repmat(img, [10 10]);

%# new figure
hFig = figure;

%# try show image at full size (suppress possible warning)
s = warning('off', 'Images:initSize:adjustingMag');
imshow(img, 'InitialMagnification',100, 'Border','tight')
warning(s);

%# handle figure resize events
hAx = gca;
set(hFig, 'ResizeFcn',{@onResize,hAx})

%# call it at least once
feval(@onResize,hFig,[],hAx);

%# enable panning tool
pan on

the following is the resize callback function:

function onResize(o,e,hAx)
    %# get axes limits in pixels
    oldUnits = get(hAx, 'Units');    %# backup normalized units
    set(hAx, 'Units','pixels')
    pos = get(hAx, 'Position');
    set(hAx, 'Units',oldUnits)       %# restore units (so it auto-resize)

    %# display the top left part of the image at magnification 100%
    xlim(hAx, [0 pos(3)]+0.5)
    ylim(hAx, [0 pos(4)]+0.5)
end

image is too big to fit in the screen (MATLAB)

You could probably improve this further so that when you resize the figure, you don't always go back to the top-left corner, but maintain the current position.


One solution that should work is to display the image and then change the axes limits so that there is one screen pixel for every image pixel:

%# read an image and make it large
img = imread('autumn.tif');
img = repmat(img,[10,10]);

%# turn off the warning temporarily, we're going to fix the problem below
%# Note that in R2011b, the warning ID is different!
warningState = warning('off','Images:initSize:adjustingMag');
figure
imshow(img)
warning(warningState);


%# get axes limits in pixels
set(gca,'units','pixels')
pos = get(gca,'position')

%# display the top left part of the image at magnification 100%
xlim([0.5 pos(3)-0.5]),ylim([0.5 pos(4)-0.5])

Now you can select the hand (pan tool) and move the image around as needed.


Note: To centre the image (instead of showing its top left), use

    xlim([(w_image - w_window) / 2, (w_image + w_window) / 2]);
    ylim([(h_image - h_window) / 2, (h_image + h_window) / 2]);

where w_image and h_image are the image's dimensions, and w_window and h_window are the above answers' pos(3) and pos(4), respectively.

0

精彩评论

暂无评论...
验证码 换一张
取 消