[英]How to lock image dimensions in MATLAB
So I have this matrix in MATLAB, 200 deep x 600 wide. 所以我在MATLAB中有这个矩阵,200深x 600宽。 It represents an image that is 2cm deep x 6cm wide.
它代表一个2厘米深x 6厘米宽的图像。 How can I plot this image so that it is locked into proper dimensions, ie 2cm x 6cm?
如何绘制此图像以使其锁定在适当的尺寸,即2厘米x 6厘米? If I use the image or imagesc commands it stretches it all out of shape and shows it the wrong size.
如果我使用image或imagesc命令,它会将它全部变形,并显示错误的大小。 Is there a way to lock it into showing an image where the x and y axes are proportional?
有没有办法将其锁定为显示x和y轴成比例的图像?
Second question, I need to then set this image into a 640x480 frame (20 pixel black margin on left and right, 280 pixel black margin on bottom). 第二个问题,我需要将此图像设置为640x480帧(左右20像素黑色边距,底部280像素黑色边距)。 Is there a way to do this?
有没有办法做到这一点?
To keep aspect ratio, you can use axis equal
or axis image
commands. 要保持纵横比,可以使用
axis equal
或axis image
命令。
Quoting the documentation: 引用文档:
axis equal
sets the aspect ratio so that the data units are the same in every direction. axis equal
设置纵横比,使得数据单元在每个方向上都相同。 The aspect ratio of the x-, y-, and z-axis is adjusted automatically according to the range of data units in the x, y, and z directions. 根据x,y和z方向上的数据单元的范围自动调整x轴,y轴和z轴的纵横比。
axis image
is the same as axis equal except that the plot box fits tightly around the data` axis image
与轴相同,只是绘图框紧紧围绕数据`
For second question: 第二个问题:
third_dimension_size=1; %# for b&w images, use 3 for rgb
framed_image=squeeze(zeros(640,480,third_dimension_size));
framed_image(20:20+600-1,140:140+200-1)= my_600_200_image;
imagesc(framed_image'); axis image;
set(gca,'DataAspectRatio',[1 1 1])
Second question: 第二个问题:
new_image = zeros(480,640);
new_image(20:(200+20-1),20:(600+20-1)) = old_image;
As an alternative to the other answers, you might want: 作为其他答案的替代方案,您可能需要:
set(gca, 'Units', 'centimeters', 'Position', [1 1 6 2])
Make sure you do this after plotting the image to get the other axis properties correct. 确保在绘制图像后执行此操作以使其他轴属性正确。
For the second question, take care with the number of colour channels: 对于第二个问题,请注意颜色通道的数量:
new_image = zeros(480,640, size(old_image));
new_image(20:(200+20-1),20:(600+20-1),:) = old_image;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.