Zoom of image on UIAxes seems to have one axis bound

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

 채택된 답변

Adam Danz
Adam Danz 2024년 12월 19일
편집: Adam Danz 2024년 12월 19일
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
  1. 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.
  2. Once you have the cropped CData, use it to generate a new image in a new axes.
  3. 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

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
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.
Yes thats done it - I'd always been using image.....guess I wont anymore. I assume theres no disadvanatge in doing this?
How about the 2nd part, to get hold of just the visible (zoomed) part of the image and display on another UIAxes?
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.
Thanks Adam, I need to retain the resolution so will play with your Method 2

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 12월 19일

편집:

2024년 12월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by