如何使用saveas()将MATLAB图形另存为JPEG,而图像不会掉落严重? [英] How to save MATLAB figure as JPEG using saveas() without the image coming off badly?

查看:1102
本文介绍了如何使用saveas()将MATLAB图形另存为JPEG,而图像不会掉落严重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的MATLAB函数中,我正在生成一个图形.执行该功能后,将显示该图.我需要将图形另存为JPEG图像.为此,我可以在显示图形的图形窗口中执行File-> Save As.但我想使它自动化.我尝试使用saveas()函数来做到这一点.问题在于所得到的图像是不希望的.这是一个演示问题的图像,向您展示我的意思:

In a MATLAB function I am writing, I am generating a figure. The figure is displayed when the function is executed. I need to save the figure as a JPEG image. To do that, I could do File->Save As in the figure window that displays the figure. But I'd like to automate this. I've tried to do that using the saveas() function. The problem is that the resulting image is undesirable. Here are the images for a demo problem to show you what I mean:

使用文件"->另存为"在MATLAB图形窗口中手动保存的JPEG图像:

使用saveas()函数保存的JPEG图像(请注意,绘图不太好,标题重叠):

这是MATLAB函数,在其中生成图形并使用saveas()将图形另存为JPEG:

Here is the MATLAB function in which I generate the figure and save the figure as a JPEG using saveas():

function JpgSaveIssueDemo( )
    figure( 1 );
    t = 0:0.1:8;    

    subplot( 2, 2, 1 );    
    plot( t, sin(t) );
    title( 'Plot 1 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );

    subplot( 2, 2, 2 );
    plot( t, sin(t) );
    title( 'Plot 2 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );

    subplot( 2, 2, 3 );
    plot( t, sin(t) );
    title( 'Plot 3 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );

    subplot( 2, 2, 4 );
    plot( t, sin(t) );
    title( 'Plot 4 of Example to Demonstrate JPG Save Issue', 'FontSize', 18 );

    saveas( gcf, 'DemoPlot', 'jpg' );
end

执行JpgSaveIssueDemo()时显示的图形未最大化.因此,我认为,如果可以在执行saves()之前使用JpgSaveIssueDemo()中的函数call/s来最大化它,那么保存的JPEG图像就会很好地显示出来.

The figure that is displayed when JpgSaveIssueDemo() is executed isn't maximized. So, I thought that if I could maximize it using function call/s in JpgSaveIssueDemo() before saveas() is executed, then the JPEG image saved would come out well.

因此,我在JpgSaveIssueDemo()中的saveas()行之前使用了此代码以最大化图形:

So, I used this code before the saveas() line in JpgSaveIssueDemo() to maximize the figure:

drawnow;
jFrame = get(handle(gcf),'JavaFrame'); 
jFrame.setMaximized(true);

然后,显示的图形被最大化.但是,结果是一样的:JPEG图像仍然不理想地显示出来.

Then, the figure that is displayed is maximized. However, the result is the same: the JPEG image still comes out undesirably.

对此可以做什么?

推荐答案

Matlab图形导出对话框和saveas()函数缺少许多理想的功能.特别地,savas()无法创建自定义的重塑图像,这就是您的结果看起来很差的原因.对于创建位图图像,我强烈建议使用第三方功能 export_fig .通过将以下代码添加到函数中(包括最大化技巧)

The Matlab figure export dialog and the saveas() function lack a lot of desirable functionality. Especially, savas() cannot create a custom resoultion image which is why your results look poor. For creation of bitmap images I highly recommend using the third-party function export_fig. By adding the following code to your function (including the maximizing trick)

set(gcf, 'Color', 'white'); % white bckgr
export_fig( gcf, ...      % figure handle
    'Export_fig_demo',... % name of output file without extension
    '-painters', ...      % renderer
    '-jpg', ...           % file format
    '-r72' );             % resolution in dpi

我创建了此图片:(在您的浏览器中使用显示图片"或类似图片来获取原始大小)

I created this image: (use "show image" or similar in your browser to get the original size)

要获得更高的质量,请使用150甚至300 dpi的更高分辨率(用于打印).对于大多数应用而言,与其最大化图形窗口,不如定义轴大小以获得所需大小的图像:

For higher quality use higher resolutions of 150 or even 300 dpi (for print). Instead of maximizing the figure window, for most applications it's suitable to define the axis size to obtain an image of the desired size:

unitSave = get(figureHandle, 'Unit');                % store original unit
set(figureHandle, 'Unit', 'centimeters');            % set unit to cm
set(figureHandle,'position',[0 0 width height]);     % set size
set(figureHandle, 'Unit', unitSave);                 % restore original unit

这篇关于如何使用saveas()将MATLAB图形另存为JPEG,而图像不会掉落严重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆