适合使用MATLAB完全包含图像的轴

时间:2014-01-08 08:47:31

标签: matlab matlab-guide

我有一个名为“axes1”的轴和一个名为“im”的图像

如何使图像尺寸等于轴尺寸?

我的意思是我想让轴适合包含图像(不要进行图像拉伸或图像压缩)?

3 个答案:

答案 0 :(得分:0)

显示图像后使用命令轴图像。

答案 1 :(得分:0)

我将您的问题解释为您希望图像的1个像素为轴中的一个像素。

你需要为此做一些计算。

我会这样做:

imagesc(magic(64)) % example image
set(gca,'Units','pixel') % set units to pixel
pos_in_px=get(gca,'Position')
set(gca,'XLim',[1,pos_in_px(3)-pos_in_px(1)]) % set xaxis to the pixel range
set(gca,'YLim',[1,pos_in_px(4)-pos_in_px(2)]) % set yaxis to the pixel range

当然这是一个脆弱的解决方案 - 当您缩放或调整窗口大小时,事情可能会发生变化。

答案 2 :(得分:0)

我假设您要显示彩色图像。试试这个(在Matlab CLI中复制粘贴):

RGB = imread('ngc6543a.jpg');
[m,n,~] = size(RGB);
figure('Units', 'pixels', 'Position', [40 40 n m]);
image(RGB);
set(gca, 'Position', [0 0 1 1]);
axis equal;

'ngc6543a.jpg'是Matlab提供的标准图像。