[英]Saving a MATLAB matrix as an image to a specific size and resolution
After having read this question, I wanted to use this code to save my picture as to particular size. 阅读完此问题后,我想使用此代码将图片保存为特定大小。
I_c(:,:) = cropped_matrix(i,:,:);
set(I_c, 'PaperUnits', 'inches');
x_width = 7.25;
y_width = 9.125;
set(I_c, 'PaperPosition', [0 0 x_width y_width]);
imshow(I_c);
saveas(I_c,'fig1.pdf');
I_c
represents a 2D matrix (about 40x40) of uint8
's. I_c
代表uint8
的2D矩阵(大约40x40)。
However, I get the error: 但是,我得到了错误:
Error using set Invalid handle
使用设置无效句柄时出错
This makes me believe that I can only use this code with figures and not matrices which contain matrices. 这使我相信我只能将此代码与图形一起使用,而不能与包含矩阵的矩阵一起使用。 How would I go about this?
我将如何处理?
I have looked at the API for print
, as suggested as the first answer of the aforementioned linked question, but it also suggests using set
and 'PaperUnits'
. 正如前面提到的链接问题的第一个答案所建议的那样,我已经研究了用于
print
的API ,但是它也建议使用set
和'PaperUnits'
。
Note: This question also looks at this problem but suggests the same solution. 注意: 此问题也着眼于此问题,但提出了相同的解决方案。
Notes About Crowley's Answer 关于克劳利答案的注意事项
.jpg
being produced is the one shown below. .jpg
如下所示。 How would I get rid of all the extra white area through code? colourmap
of the figure being produced, to greyscale when I just use image(ImData)
? image(ImData)
时,如何将colourmap
的图形的colourmap
图更改为灰度? And here is how the actual figure appears: 这是实际数字的显示方式:
Here is the code that I have put in: 这是我输入的代码:
im = image(I_c);
set(gcf,'units','inches','position',[1 2 5 5]);
set(gca,'ydir','normal','units','centimeters','position',[0 0 0.5 0.5].*get(gcf,'position')) ;
filename = strcat('slice',int2str(i),'_','bead',int2str(j),'.jpg');
saveas(im,filename);
Suppose we have matrix I_c
containing values and x
and y
the coordinates so value I_c(ii,jj)
corresponds to x(ii)
and y(jj)
coordinates. 假设我们具有包含值的矩阵
I_c
以及x
和y
的坐标,因此值I_c(ii,jj)
对应于x(ii)
和y(jj)
坐标。
Then: 然后:
ImMin=min(min(I_c)); % Find the minimum value in I_c
ImData=I_c-ImMin; % Ensure non-zero values
ImMax=max(max(ImData)); % Find maximum value in ImData
ImData=ImData./ImMax; % Ensure ImData do NOT exceed 1
image(x,y,ImData*[1 1 1]) % plot the image in greyscale
set(gcf,'units','inches','position',[1 2 5 5]) % set active figure properties
set(gca,'ydir','normal','units','inches','position',[0 0 1 1].*get(gcf,'position')) % set active axes properties
export_fig(gcf,'figure-I_c','-png','-nocrop')
'ydir','normal'
parameter change default (1,1) point being in top-left corner to "normal" position in bottom-left corner. 'ydir','normal'
参数将默认(1,1)点从左上角更改为左下角的“ normal”位置。
[0 0 1 1].*get(gcf,'position)
will read active figure position (here [1 2 5 5]) and after element-by-element multiplication the [0 0 5 5]
is passed to the position
which causes that axes fit the image. [0 0 1 1].*get(gcf,'position)
将读取活动图形的位置(此处为[1 2 5 5]),在逐个元素相乘后, [0 0 5 5]
会传递到该position
使轴适合图像。
export_fig
function will create figure-I_c.png
image as it is shown in Matlab figure, if -nocrop
is omitted the possible white space in the edges is cropped out. export_fig
函数将创建Matlab图中所示的figure-I_c.png
图像,如果省略-nocrop
,则可能会裁剪掉边缘中的空白区域。 This function is available from MathWorks' File Exchange . 该功能可从MathWorks的File Exchange中获得 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.