在matlab中用parfor保存高分辨率数字 [英] save high resolution figures with parfor in matlab

查看:185
本文介绍了在matlab中用parfor保存高分辨率数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这个数字手柄创建部分:

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

以下是保存部分代码:

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

p>

有什么想法?

谢谢

解决方案

这是记录的限制

在无头模式下打印


不带显示的打印和导出



在UNIX平台(包括Macintosh)上,你可以用
开始$ nodisplay 模式( matlab -nodisplay ),你可以使用
大多数驱动程序可以与显示器一起使用,并将大部分
导出为相同的文件格式。在UNIX平台上,PostScript和Ghostscript设备中的所有
函数都以 nodisplay 模式运行。图形设备
-djpeg -dpng -dtiff (压缩的TIFF位图)和 -tiff
(带TIFF预览的EPS)也可以工作,但 nodisplay 它们使用
Ghostscript来生成输出,而不是使用内置到
MATLAB中的驱动程序。但是,当生成
-djpeg - 时,Ghostscript会忽略 -r 选项。 dpng -dtiff -tiff 图像文件。这意味着

nodisplay 模式下运行时不能改变图像文件的分辨率。



对于 -noFigureWindows 启动选项也是如此,
禁止所有平台上的数字。在Windows平台上, -dwin
-dwinc -dsetup 选项在
-noFigureWindows 下照常运行。但是,在这种模式下, printpreview GUI不会运行
。当然,Windows只能在UNIX或Mac平台上使用 -dwin -dwinc 输出
格式有或没有
显示。







解析注意事项



使用 -rnumber 来指定生成输出的分辨率。在
一般情况下,使用更高的值将产生更高的质量输出,但是更大的输出文件的成本为
。它会影响分辨率,并输出所有MATLAB内置栅格格式的
大小(在图形格式文件的表格的
列四中标识)。

注意:内置图形格式直接从MATLAB生成,无需通过Ghostscript库进行转换。此外,在无头
nodisplay )模式下,写入某些图像格式不是由
内置驱动程序完成的,因为它是在显示正在使用。这些格式
-djpeg -dtiff -dpng 。此外,无头模式下无法生成 -dhdf
-dbmp 格式(但您可以使用
-dbmp16m 替换为 -dbmp )。请参阅不使用显示器打印和导出
,了解不使用显示器时的打印细节。



与内置的MATLAB格式不同,
Ghostscript不直接服从 -r 选项设置。但是,由$ MATLAB生成的
中间PostScript文件作为
Ghostscript处理器的输入受到 -r 设置的影响,因此可以
间接影响最终Ghostscript生成的
输出的质量。


$ b -r 选项的效果在使用OpenGL或ZBuffer渲染器时,输出质量可以微妙地为
普通放大倍数,并且
写入其中一种MATLAB内置栅格格式,或者
生成包含嵌入式光栅图像(例如
,PostScript或PDF)。由于较大的 -r 设置提供了$ b,因此指定较高
分辨率的效果在以较高的
放大率查看输出时或打印时更明显

生成完全向量化的输出时(与使用Painters
渲染器输出向量格式(如PostScript)或PDF),
分辨率设置会影响输出的详细程度;设定较高的
分辨率会产生较清晰的输出(但
分辨率的小变化可能没有可观察到的效果)。例如,不会使用实线(' - ')线型的线宽的
可能会受到影响。







parfor 产生无头MATLAB实例(包括Windows和Unix),所以根据以上所述,工作进程将回退到忽略 -r 选项的Ghostscript打印驱动程序。



将数据导出为栅格图形格式(PNG,JPEG,TIFF等)时,有两种情况:


  • if在正常会话中打印时,MATLAB将使用其内置的驱动程序直接生成图形文件,并且应该服从您指定的解析


  • 如果以无头模式打印,MATLAB将在内部将这些数字以Postscript矢量格式导出,然后使用 Ghostscript 使用以下Ghostsc将其转换为请求的栅格格式ript选项:

      -dNOPAUSE -q 
    -IC:\程序文件\MATLAB\R2014a \sys\extern\win64\ghostscript\ps_files
    -IC:\程序文件\MATLAB\R2014a\sys\extern\win64\ghostscript\fonts
    -sDEVICE = jpeg
    -g576x432
    -sOutputFile =file.jpeg

    正如你所看到的,由于某些原因,MATLAB在将PS文件转换为其他格式时,在无头模式下使用了一个固定的目标大小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);

%随机图
ax =轴('Parent',fig);
plot(ax,cumsum(rand(1000,1)-0.5))

%保存为每种指定格式(-r选项大多被忽略)
for f = 1 :numel(fmt)
print(fig,['-d'fmt {f}],'-r920',...
fullfile(outfolder,sprintf('plot%d。%s' ,I,FMT {F})));
drawnow
end

%还保存图文件
hgsave(图,sprintf('plot%d.fig',i))

关闭(图);
结束
删除(gcp)






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

另一种方法是在 parfor 内导出FIG文件。然后你可以在一个普通的MATLAB会话中用显示装载它们,并以期望的分辨率和格式连续打印:

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


I am using parfor loop to produce and save quite big number of figures. Due to the amount of data which will be presented in the figures, the resolution of the figures need to be high, something around 920 dpi. Using the normal for, the function works fine. But when we switch to parfor the resolution of the produced and saved pictures becomes totally low.

This is the figure handle creation part:

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

and here is the saving part code:

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

any idea?

Thanks

解决方案

This is a documented limitation of printing in headless mode:

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 spawns headless MATLAB instances (both Windows and Unix), so according to the above, the worker processes will fallback to Ghostscript printing driver which ignores the -r option.

When you export figures to raster graphics format (PNG, JPEG, TIFF, etc..) there are two cases:

  • if you printing in a normal session, MATLAB will use its built-in drivers to generate the graphics files directly, and should obey the resolution you specify

  • on the other hand, if you printing in headless mode, MATLAB will internally export the figure in Postscript vector format, and then use Ghostscript to convert it to the requested raster format using the following Ghostscript options:

    -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"
    

    as you can see, for some reason MATLAB uses a fixed target size 576x432 in headless mode when converting the PS file to other formats.


Here is some code for quick experimentation. I've tested it on a local parallel pool; All of the raster formats (PNG, JPEG, TIFF, PPM) had a fixed size of 576x432 (-r option ignored as previously explained). The PDF was also generated by converting the PS file to PDF (using -sDEVICE=pdfwrite Ghostscript output device).

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)


The way I see it, you ought to export as an EPS file, and manually convert it to whatever format you need. That way you get to specify the target image size in the Ghostscript command invoked (I wouldn't bother with the print -r resolution option, because it has little effect on vector formats)

The alternative would be to export FIG-files inside parfor. You would then load them in a normal MATLAB session with a display, and serially print with the desired resolution and format:

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

这篇关于在matlab中用parfor保存高分辨率数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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