5

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 size without any zooming out .. is that possible in imshow function are there any parameters that do this?

thank you all

Amro
  • 123,847
  • 25
  • 243
  • 454
Omar Osama
  • 1,401
  • 3
  • 19
  • 29
  • 1
    have you considered using [IMTOOL](http://www.mathworks.com/help/toolbox/images/ref/imtool.html)? – Amro Sep 13 '11 at 23:23
  • I tried it .. it works .. but I want `imshow` to do that because of saving issues using `print`.. `imtool` will not enable me to save the figure – Omar Osama Sep 14 '11 at 10:37
  • similar question: [MATLAB: showing an image in its original size](http://stackoverflow.com/questions/1427602/matlab-showing-an-image-in-its-original-size) – Amro Sep 30 '11 at 23:51

3 Answers3

3

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.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • 1
    very cool :-). You could add `s=warning('off','Images:initSize:adjustingMag'); figure,imshow(img); warning(s);` to avoid the warning message... – Jonas Heidelberg Sep 13 '11 at 22:32
  • (deleting old comments which no longer apply...) – Jonas Heidelberg Sep 13 '11 at 22:33
  • @Jonas Heidelberg: Good point! Unfortunately, I have only installed the pre-release, where there's yet another different set of warning IDs, so I can't make my answer R2011b compatible yet. – Jonas Sep 13 '11 at 23:36
  • 1
    @Jonas: Check the PDF file attached at the end of this page: http://www.mathworks.com/support/solutions/en/data/1-ERAFNC/?solution=1-ERAFNC – Amro Sep 14 '11 at 00:03
  • @Amro: Nice! I did in fact request such a document after the prerelease came out; I'm glad they made it. – Jonas Sep 14 '11 at 00:18
3

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

screenshot

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.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • thank you .. :) .. but it doesn't work .. I think it didn't work because of the huge size of the image .. it is `1914-by- 2294` – Omar Osama Sep 14 '11 at 11:04
  • @OmarOsama: what exactly has gone wrong? It is working just fine for me. BTW the tiled image size in the example above is 2060x3450.. – Amro Sep 14 '11 at 13:19
0

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.

Gnubie
  • 2,587
  • 4
  • 25
  • 38