How can I make use of the full window size when zooming into very tall images?

11 views (last 30 days)
When zooming into an image with a hight aspect ratio (heigth much larger than its width), the displayed image is constrained to the area that was used with the original imshow command and the rest of the window remains unused. How can I make use of the full window size?
Example (normally imtall would be a linescan camera image of size 10000 by 512 pixels but this demonstrates my problem pretty well):
im=imread('cameraman.tif');
imtall=repmat(im,11,1); % size 2816 by 256
imshow(imtall)
zoom(10)
There would be enough space to the left and right side in the window to display the full cameraman:

Accepted Answer

Umar
Umar about 4 hours ago

@RalfW,

I tested your code with an image saved in my MATLAB Drive and can confirm exactly what you're describing. The zoomed image stays confined to that narrow vertical strip with lots of unused space on both sides. This is actually a known limitation with MATLAB's imshow function and I found a few good solutions for you.

The simplest fix that worked well for me is to use image instead of imshow along with colormap and axis equal. Here's the modified code:

im = imread('yourimage.png');
imtall = repmat(im,11,1);
image(imtall)
colormap(gray)
axis equal
zoom(10)

The reason this works better is that imshow sets very strict DataAspectRatio constraints to maintain exact pixel aspect ratio. When you zoom in, these constraints force the image to stay in that narrow column regardless of how much window space is available. The image function with axis equal still maintains proper aspect ratio but allows MATLAB to use the screen space more flexibly during zoom operations. I tested this in MATLAB Mobile and it filled the window width significantly better than before while keeping everything in proportion. Please see attached.

If you have the Image Processing Toolbox, the Image Viewer app actually has much better zoom behavior for this exact situation. You can use it like this:

im = imread('cameraman.tif');
imtall = repmat(im,11,1);
imtool(imtall);

According to the MathWorks documentation at https://www.mathworks.com/matlabcentral/answers/325282 , when zooming in imtool, the width of the tall image will automatically expand to fill the figure, which is exactly what you're looking for. The Image Viewer was specifically designed to handle viewport management better than imshow for interactive viewing.

For even more control over zoom behavior, Steve Eddins created a File Exchange submission specifically to address this limitation. He's the former MathWorks developer who was actually involved in the evolution of imshow since 1993, and he wrote about this exact problem on his blog at https://steveeddins.com/image-zoom-pan/ . He created the Image Zoom Level and Pan Utilities which you can get from https://www.mathworks.com/matlabcentral/fileexchange/167316-image-zoom-level-and-pan-utilities or from GitHub at https://github.com/eddins/imzm . Once you install it, you can use it like this:

im = imread('cameraman.tif');
imtall = repmat(im,11,1);
imshow(imtall)
setImageZoomLevel(1000)

The setImageZoomLevel function was specifically written so that the available plot region is used when the zoom level is changed, instead of staying constrained to the initial narrow viewport. Steve actually mentioned in his blog that he submitted an enhancement request to MathWorks about this issue because it happens whenever DataAspectRatio is set to a fixed value, not just with images but even with regular plots.

For your linescan camera images, I'd recommend starting with the image and axis equal approach since it's the most straightforward and doesn't require additional toolboxes or downloads. It significantly improves the use of available window space for tall images.

If you need more interactive control or plan to do this regularly, imtool or Steve's utilities would be worth looking into.

  1 Comment
RalfW
RalfW about 4 hours ago
Hello Omar,
thanks for you long answer with all the explanations and potential solutions.
Using image instead of imshow works fine for me. I just added
set(gca,"Position",[0.0 0.0 1 1])
set(gca,'Visible',"off")
to have more space available to display the image. I need to check, whether this solution works in combination with my image analysis tool, which is definitely not the case when using imtool.
Using
ax = gca;
ax.DataAspectRatioMode = 'manual';
ax.Clipping = 'off';
as described in https://ch.mathworks.com/matlabcentral/answers/325282-how-do-i-prevent-axes-clipping-when-changing-zoom-on-a-tall-image however led to strange zoom behaviour, when interactively zooming by dragging the rectangular zoom area several times.
Therefore I hope that Steve's enhancement request to MathWorks will be accepted, as he describes this clumsy zoom behaviour very clearly. When I have time I will check out his zoom and pan utility.
Thanks again, Ralf

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE about 3 hours ago
hello
you can try this :
% Example data
im=imread('cameraman.tif');
imtall=repmat(im,11,1); % size 2816 by 256
imshow(imtall)
zoom(10)
% Maximize width: fit X-axis to data range
[y,x,~] = size(imtall);
xlim([1 x]);
% % Optional: make figure fill screen
% set(gcf, 'Units', 'normalized', 'OuterPosition', [0 0 1 1]);
  1 Comment
RalfW
RalfW 5 minutes ago
Hello Mathieu,
thanks for your prompt answer. Setting xlim does the job - however only until I change the zoom level again, what I quite often do when exploring these large images. So I will consider keeping track of the zoom level and then update xlim and ylim accordingly.
Thanks, Ralf

Sign in to comment.

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!