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
채택된 답변
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
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
