Zoom of image on UIAxes seems to have one axis bound

43 vues (au cours des 30 derniers jours)
Jason
Jason le 19 Déc 2024 à 13:04
Modifié(e) : Adam Danz le 19 Déc 2024 à 20:51
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

Réponse acceptée

Adam Danz
Adam Danz il y a environ 21 heures
Modifié(e) : Adam Danz il y a environ 15 heures
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 commentaires
Adam Danz
Adam Danz le 19 Déc 2024 à 16:18
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
Jason le 19 Déc 2024 à 17:18
Thanks Adam, I need to retain the resolution so will play with your Method 2

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Exploration dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by