Zoom of image on UIAxes seems to have one axis bound
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hello, I have a highly rectangular image displayed on a uiaxes in AppDesigner

I want to Zoom into say the middle region, and so have performed:
zoom(app.UIAxes,32);
This gives

I was hoping it would expand in the ydirection too - but it seems constrained for some reason.
This is what I was hoping for, the red box is my uiaxes, so why are the bounds shown by yellow arrows not expanding when I perform the Zoom

As a second question, how can I get just part of the image that is in view?
Thanks
Jason
Accepted Answer
Try setting the DataAspectRatio to [1 1 1] using axis(uiax,'equal').
If that doesn't fix the issue, it would help to share a bit more about how you are plotting the image.
% uif = figure();
% uiax = uiaxes(uif);
%
% m = rand(10,1000);
% imagesc(uiax,m)
% axis(uiax,'equal')
%
%
% zoom(uiax, 32)

> how can I get just part of the image that is in view?
Method 1: capture the cropped axes
- Pros: quick and easy
- Cons: This copies the image portentially at a different resolution
Here's a function that receives an axes handle and creates of copy of the content in a new axes.
% exportClippedImage(uiax)
function exportClippedImage(ax)
% ax is a scalar axis handle.
% Temporarily turn off axes
originalState = ax.Visible;
revertVis = onCleanup(@()set(ax,'Visible',originalState));
ax.Visible = 'off';
% Copy axis content
F = getframe(ax);
% Plot axis content to a new figure
fig = figure();
newax = axes(fig);
imshow(F.cdata,'Parent',newax) % or use image()
end
Method 2: index the image
- Pros: uses the original image data
- Cons: Much more work and it's only applied to the image object, ignores anything else in the axes
- Use the xlim and ylim to crop the original image data. What makes this difficult is that the image pixels are centered on a coordinates and the coordinates are determined by the first and last XData and YData values. The xlim and ylim can split a pixel but your indexing cannot.
- Once you have the cropped CData, use it to generate a new image in a new axes.
- Ensure the new axes has an appropriate aspect ratio to match that of the original axes and adjust any other properties.
Demo
% Create initial image
uif = figure();
uiax = uiaxes(uif);
m = rand(40,500);
I = imagesc(uiax,m);
axis(uiax,'equal')
set(uiax,'xtick',[],'ytick',[]);
title('Original')
% Zoom in
zoom(uiax, 42)

%% Crop image and copy to to new figure
xl = xlim(uiax);
yl = ylim(uiax);
imgSize = size(I.CData);
% Compute the actual boundaries of the image.
xhalfPixWidth = diff(I.XData)/(size(I.CData,2)-1)/2;
yhalfPixWidth = diff(I.YData)/(size(I.CData,1)-1)/2;
imageXBounds = sort(I.XData([1,end]))+[-1 1]*xhalfPixWidth;
imageYBounds = sort(I.YData([1,end]))+[-1 1]*yhalfPixWidth;
% Convert the axis limits to image indices
imageXIdx = round((xl-imageXBounds(1))./diff(imageXBounds).*(imgSize(2)-1)+1);
imageYIdx = round((yl-imageYBounds(1))./diff(imageYBounds).*(imgSize(1)-1)+1);
% Clip indices to range of image size
imageXIdx = [max(imageXIdx(1),1), min(imageXIdx(2),imgSize(2))];
imageYIdx = [max(imageYIdx(1),1), min(imageYIdx(2),imgSize(1))];
% Crop the image
newimage = I.CData(imageYIdx(1):imageYIdx(2), imageXIdx(1):imageXIdx(2));
% Plot the cropped image in a new figure
newfig = figure();
newax = axes(newfig);
image(newax, uiax.XLim, uiax.YLim, newimage, 'CDataMapping',I.CDataMapping)
colormap(newax, uiax.Colormap)
clim(newax, uiax.CLim)
newax.PlotBoxAspectRatio = uiax.PlotBoxAspectRatio;
newax.DataAspectRatio = uiax.DataAspectRatio;
axis(newax,'equal','off')
title('Copped and copied')

Notice the the zoom in my demo captures portions of the rows and columns at the borders. My algorithm includes those full rows and column. You may need to set other properties to control the final copied image but this should get you started.
6 Comments
Jason
on 19 Dec 2024
Thanks Adam. So this is how I splat the image (IM) on the UIAxes
imagesc(ax1,IM); axis(ax1,'image');
colormap(ax1,'gray'); set(ax1,'xtick',[]); set(ax1,'ytick',[]);
I also have a little check box that I use to change from axis image to axis equal
val = app.AxisequalCheckBox.Value;
ax=app.UIAxes;
switch val
case 1
axis(ax,'equal');
case 0
axis(ax,'image');
end
But no matter what I use, it still doesn't expand in y
For info, my image is 16384 x 128 pixels in size
Adam Danz
on 19 Dec 2024
Ah, that makes sense. Using axis image disables the default “stretch-to-fill” behavior.
Once you execute axis image, executing axis equal does not revert all of the changes initially made by axis image.
Just use axis equal and if there's something about the image that isn't right, I can help you find the properties to set.
Jason
on 19 Dec 2024
Yes thats done it - I'd always been using image.....guess I wont anymore. I assume theres no disadvanatge in doing this?
Jason
on 19 Dec 2024
How about the 2nd part, to get hold of just the visible (zoomed) part of the image and display on another UIAxes?
Adam Danz
on 19 Dec 2024
axis image sets the XLimitMethod, YLimitMethod, and ZLimitMethod to tight. It's like if you called axis tight. This tightens the axis limit around your image. The same can be achieved by directly setting xlim, ylim if needed.
I didn't see the 2nd question until just now.
I can think of two ways to isolate the cropped image.
I'll update my answer to include them.
Jason
on 19 Dec 2024
Thanks Adam, I need to retain the resolution so will play with your Method 2
More Answers (0)
Categories
Find more on Printing and Saving in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)