matlab 保存圖片的幾種方式


最近在寫畢業論文, 需要保存一些高分辨率的圖片. 下面介紹幾種MATLAB保存圖片的 方式.

一. 直接使用MATLAB的保存按鍵來保存成各種格式的圖片

你可以選擇保存成各種格式的圖片,  實際上對於一般的圖片要求而言, 該方法已經足夠了.

 

二. 使用saveas函數

該函數實際上類似於 “另存為” 的選項, 並且忽略圖片的背景大小等等, 按照默認的屬性存儲.

一般格式為為

saveas(fig, filename, formattype)

clear
clc
x = 0:0.01:2*pi;
y = sin(x);
plot(x, y)
xlabel('x')
ylabel('y')
title('y = Sin(x)')
saveas(gcf, 'test', 'png')

  

這的可選項有png, jpg, bmp等等, 以及矢量圖格式, eps, svg, pdf等等.

 

三. 使用imwrite函數

imwrite 實際上是保存一個描述圖片的數組, 使用的一般格式為imwrite(A, filename)

clear
clc
x = 0:0.01:2*pi;
y = sin(x);
plot(x, y)
xlabel('x')
ylabel('y')
title('y = Sin(x)')
f = getframe(gcf);
imwrite(f.cdata, 'test.png');  

  

該函數可以用於保存為png, jpg, bmp等等格式, 但是不可以保存為eps, svg, pdf 等矢量圖格式. 

該函數還可以用於保存gif.

clear
clc
n = 1:10;
nImages = length(n);
x = 0:0.01:1;
im = cell{nImages, 1};
figure;
for idx = 1:nImages
    y = sin(2*pi*x*idx);
    plot(x,y,'LineWidth',3)
    title(['y = sin(2n\pix),  n = ' num2str(n(idx)) ])
    drawnow
    frame = getframe(gcf);
    im{idx} = frame.cdata;
end
close;
filename = 'test.gif'; 
for idx = 1:nImages
    [A,map] = rgb2ind(im{idx},256);
    if idx == 1
        imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);
    else
        imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
    end
end

 

四. 使用 printf 函數

clear
clc
x = 0:0.01:2*pi;
y = sin(x);
plot(x, y)
xlabel('x')
ylabel('y')
title('y = Sin(x)')
printf(gcf,'-dpng','test.png') 

  

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM