Try the using the 'InitialMagnification'
parameter with the 'fit'
option value with imshow
:
hImshow = imshow(img,'InitialMagnification','fit')
From this MathWorks tutorial:
You can also specify the text string 'fit' as the initial magnification value. In this case, imshow scales the image to fit the current size of the figure window
See also this section of the imshow
docs regarding 'InitialMagnification'
. So, that should keep your figure window the same size.
That would solve the issue of losing the window maximization.
To get an image displayed scaled at 1 pixel to 1 point on the screen, you can create an axis of the correct size for the image, and display into that:
fpos = get(hFig,'Position')
axOffset = (fpos(3:4)-[size(img,2) size(img,1)])/2;
ha = axes('Parent',hFig,'Units','pixels',...
'Position',[axOffset size(img,2) size(img,1)]);
hImshow = imshow(img,'Parent',ha);
Note that it is unnecessary to specify magnification since "If you specify the axes position (using subplot or axes), imshow
ignores any initial magnification you might have specified and defaults to the 'fit'
behavior", thus fitting to the axes specified by 'Parent'
.