matlab - 在 matlab 中使用 parfor 保存高分辨率图形

标签 matlab graphics printing figure parfor

我正在使用 parfor 循环来生成并保存相当多的数字。由于将在图中显示的数据量,图形的分辨率需要很高,约为 920 dpi。使用普通的 for,该函数工作正常。但是当我们切换到 parfor 时,生成和保存的图片的分辨率变得非常低。

这是图形句柄创建部分:

mainFig=figure('visible','off');
set(mainFig, 'Renderer', 'OpenGL');

这是保存部分代码:

print(mainFig,'-djpeg','-r920',strcat(MyDir,measure,sec_suffix,'.jpeg'))

有什么想法吗?

谢谢

最佳答案

这是一个 documented limitationprinting在 headless 模式下:

Printing and Exporting without a Display

On a UNIX platform (including Macintosh), where you can start in MATLAB nodisplay mode (matlab -nodisplay), you can print using most of the drivers you can use with a display and export to most of the same file formats. The PostScript and Ghostscript devices all function in nodisplay mode on UNIX platforms. The graphic devices -djpeg, -dpng, -dtiff (compressed TIFF bitmaps), and -tiff (EPS with TIFF preview) work as well, but under nodisplay they use Ghostscript to generate output instead of using the drivers built into MATLAB. However, Ghostscript ignores the -r option when generating -djpeg, -dpng, -dtiff, and -tiff image files. This means that you cannot vary the resolution of image files when running in nodisplay mode.

The same is true for the -noFigureWindows startup option which suppresses figures on all platforms. On Windows platforms the -dwin, -dwinc, and -dsetup options operate as usual under -noFigureWindows. However, the printpreview GUI does not function in this mode. Naturally, the Windows only -dwin and -dwinc output formats cannot be used on UNIX or Mac platforms with or without a display.

Resolution Considerations

Use -rnumber to specify the resolution of the generated output. In general, using a higher value will yield higher quality output but at the cost of larger output files. It affects the resolution and output size of all MATLAB built-in raster formats (which are identified in column four of the table in Graphics Format Files).

Note: Built-in graphics formats are generated directly from MATLAB without conversion through the Ghostscript library. Also, in headless (nodisplay) mode, writing to certain image formats is not done by built-in drivers, as it is when a display is being used. These formats are -djpeg, -dtiff, and -dpng. Furthermore, the -dhdf and -dbmp formats cannot be generated in headless mode (but you can substitute -dbmp16m for -dbmp). See "Printing and Exporting without a Display" for details on printing when not using a display.

Unlike the built-in MATLAB formats, graphic output generated via Ghostscript does not directly obey -r option settings. However, the intermediate PostScript file generated by MATLAB as input for the Ghostscript processor is affected by the -r setting and thus can indirectly influence the quality of the final Ghostscript generated output.

The effect of the -r option on output quality can be subtle at ordinary magnification when using the OpenGL or ZBuffer renderers and writing to one of the MATLAB built-in raster formats, or when generating vector output that contains an embedded raster image (for example, PostScript or PDF). The effect of specifying higher resolution is more apparent when viewing the output at higher magnification or when printed, since a larger -r setting provides more data to use when scaling the image.

When generating fully vectorized output (as when using the Painters renderer to output a vector format such as PostScript or PDF), the resolution setting affects the degree of detail of the output; setting resolution higher generates crisper output (but small changes in the resolution may have no observable effect). For example, the gap widths of lines that do not use a solid ('-') linestyle can be affected.


parfor 生成 headless MATLAB 实例(Windows 和 Unix),因此根据上述内容,工作进程将回退到忽略 -r 选项的 Ghostscript 打印驱动程序.

当您将图形导出为光栅图形格式(PNG、JPEG、TIFF 等)时,有两种情况:

  • 如果您在正常 session 中打印,MATLAB 将使用其内置驱动程序直接生成图形文件,并应遵循您指定的分辨率

  • 另一方面,如果您以 headless 模式打印,MATLAB 将在内部以 Postscript 矢量格式导出图形,然后使用 Ghostscript使用以下 Ghostscript 选项将其转换为请求的光栅格式:

    -dNOPAUSE -q 
    -I"C:\Program Files\MATLAB\R2014a\sys\extern\win64\ghostscript\ps_files"
    -I"C:\Program Files\MATLAB\R2014a\sys\extern\win64\ghostscript\fonts"
    -sDEVICE=jpeg
    -g576x432
    -sOutputFile="file.jpeg"
    

    如您所见,出于某种原因,当将 PS 文件转换为其他格式时,MATLAB 在 headless 模式下使用固定目标大小 576x432。


这是一些用于快速实验的代码。我已经在本地并行池上测试过了;所有光栅格式(PNG、JPEG、TIFF、PPM)的大小都是固定的 576x432(-r 选项被忽略,如前所述)。 PDF 也是通过将 PS 文件转换为 PDF 生成的(使用 -sDEVICE=pdfwrite Ghostscript 输出设备)。

fmt = {'ppm', 'tiff', 'png', 'jpeg', 'epsc2', 'pdf'};
outfolder = 'C:\Users\Amro\Desktop\print_test';

parpool(4)
parfor i=1:4
    fig = figure(i);

    % a random plot
    ax = axes('Parent',fig);
    plot(ax, cumsum(rand(1000,1)-0.5))

    % save in each specified format (-r option is mostly ignored)
    for f=1:numel(fmt)
        print(fig, ['-d' fmt{f}], '-r920', ...
            fullfile(outfolder,sprintf('plot%d.%s',i,fmt{f})));
        drawnow
    end

    % also save FIG-file
    hgsave(fig, sprintf('plot%d.fig',i))

    close(fig);
end
delete(gcp)

在我看来,您应该导出为 EPS 文件,然后手动将其转换为您需要的任何格式。这样你就可以在调用的 Ghostscript 命令中指定目标图像大小(我不会理会 print -r 分辨率选项,因为它对矢量格式几乎没有影响)

另一种方法是在 parfor 中导出 FIG 文件。然后,您可以在带有显示器的普通 MATLAB session 中加载它们,并以所需的分辨率和格式连续打印:

for i=1:4
    fig = hgload('plotXX.fig');
    movegui(fig, 'center')
    print(fig, '-djpeg', '-r920', 'outXX.jpeg')
    close(fig)
end

关于matlab - 在 matlab 中使用 parfor 保存高分辨率图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803383/

相关文章:

java - 在 paint 方法之外绘制图形

java - print 和 println 不是同时执行的

将特定 PDF 页面打印成图像的 Powershell 脚本

python - NumPy中MATLAB的repmat相当于什么

matlab - 使用每个 y 轴的两个向量格式化plotyy

css - 从网站下载图像

c++ - openGL:带着色器的线条

MATLAB GUIDE gui 列表框间歇性消失并出现看似过时的错误

matlab - 在 MATLAB 中模拟 C++ 模板

.net - 在 .NET 中编写虚拟打印机