Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何用透明背景保存图形_Image_Matlab_Visualization - Fatal编程技术网

Image 如何用透明背景保存图形

Image 如何用透明背景保存图形,image,matlab,visualization,Image,Matlab,Visualization,我在Matlab中有一个绘图,正在通过以下方式将背景设置为透明: set(gcf, 'Color', 'None'); set(gca, 'Color', 'None'); 当我试图保存图像时(从查看器),我保存为“.png”,但它保存为白色背景。如何将其保存在透明背景中?这令人失望,但MATLAB的默认saveas和print命令无法很好地处理透明内容。您必须将其保存在一些背景中,然后通过imread/imwrite或其他工具进行转换 有一些工具可能会有所帮助: 出口无花果 svg导出

我在Matlab中有一个绘图,正在通过以下方式将背景设置为透明:

set(gcf, 'Color', 'None');
set(gca, 'Color', 'None');

当我试图保存图像时(从查看器),我保存为“.png”,但它保存为白色背景。如何将其保存在透明背景中?

这令人失望,但MATLAB的默认
saveas
print
命令无法很好地处理透明内容。您必须将其保存在一些背景中,然后通过
imread
/
imwrite
或其他工具进行转换

有一些工具可能会有所帮助:

  • 出口无花果
  • svg导出

我更喜欢矢量图形,所以在需要透明度时使用svg导出。如果确实有位图,请使用
imwrite(位图数据,'a.png','png','transparency',backgroundColor)

自Matlab2014b发布以来,情况发生了变化。新实现的图形系统(称为HG2,用于Handle graphics version 2)在透明度方面做得更好


现在它至少可以将透明度正确地保存到SVG

所以我仍然想要一些简单的东西,不需要我安装任何其他东西(不允许使用公司pc:/)。我无意中发现,他说:

你所要做的就是

1) 在matlab文件中,添加命令以使用透明背景格式化图形

 set(gcf, 'color', 'none');
 set(gca, 'color', 'none');
并保存或导出以eps格式生成的图形。(比如Bspline.eps)

2) 在记事本中打开Bspline.eps

3) 看第一行。例如
%!PS-Adobe-3.0 EPSF-3.0
。最后一个数字3.0表示Postscript级别。对于级别3,搜索字符串
rf
。您可以在这样一行中找到(四个数字后跟
rf

0 0 3025 2593 rf%使用%注释该行

(对于级别2,搜索字符串
pr
,而不是
rf

保存文件

现在,您可以使用eps文件,也可以将其转换为pdf,然后使用它。 无论如何,它将有透明的背景

 set(gcf, 'color', 'none');
 set(gca, 'color', 'none');
额外的


对我来说,这是两行带有
re
和两行,尽管我有
%!PS-Adobe-3.0 EPSF-3.0
紧随其后。但是结果是,
现在是透明的。

作为对作者答案的补充。Matlab 2020 with
exportgraphics
支持透明背景,但仅用于矢量化输出(with不适用于渲染内容)。对于导出具有透明度的渲染数据,仍然不能将其保存为具有透明度。但是,您可以通过使用两种不同的背景颜色(例如白色和黑色)保存渲染数据,然后加载两个临时图像,求解一个简单的方程系统,从而检索透明度和原始颜色数据,然后将所有这些数据保存到RGBA png文件中,从而获得透明度

关键是要实现Matlab保存的渲染输出是透明度乘以图像数据+(1-透明度)乘以背景色。从单个输出中,无法恢复图像数据和透明度,但可以从具有不同背景颜色的两个输出中恢复。如果Matlab在渲染输出中支持透明背景色,这会更容易,但这种方法也适用

示例脚本:

% create example data
g = -10:0.1:10;
[x, y, z] = ndgrid(g, g, g);
v = (x.^2 + y.^2 - 10).^2 + (z.^2 - 5).^2;

% render in 3D with transparency
fig = figure();

% one surface fully opaque
fv = isosurface(x, y, z, v, 20);
p = patch(fv, 'FaceColor', [1, 0, 0], 'FaceAlpha', 1, 'EdgeColor', 'none');

% another surface with transparency
fv = isosurface(x, y, z, v, 80);
p = patch(fv, 'FaceColor', [0, 1, 1], 'FaceAlpha', 0.5, 'EdgeColor', 'none');

fig.Children.Color = 'none'; % transparent background of figure axis
view([40 40]);
pbaspect([1,1,1]);
camlight;
lighting('gouraud');

% save figure in different ways

% save as vector format, doesn't produce nice output see for example https://stackoverflow.com/questions/61631063
exportgraphics(fig, 'test.pdf', 'ContentType', 'vector', 'BackGroundColor', 'none');
% prints warning: Warning: Vectorized content might take a long time to create,
% or it might contain unexpected results. Set 'ContentType' to 'image' for better performance.

% save as rendered output with transparent background not work
exportgraphics(fig, 'test.png', 'ContentType', 'image', 'BackGroundColor', 'none');
% prints warning: Warning: Background transparency is not supported; using white instead.

% now our solution, export with two background colors
exportgraphics(fig, 'test1.png', 'ContentType', 'image', 'BackgroundColor', 'k'); % black background
exportgraphics(fig, 'test2.png', 'ContentType', 'image', 'BackgroundColor', 'w'); % white background

% load exported images back in and scale to [0,1]
u = imread('test1.png');
u = double(u) / 255;
v = imread('test2.png');
v = double(v) / 255;

% recover transparency as a
a = 1 - v + u;
a = mean(a, 3);
a = max(0, min(1, a));
m = a > eps;

% recover rgb
c = zeros(size(u));
for i = 1 : 3
    ui = u(:, :, i);
    ci = c(:, :, i);
    ci(m) = ui(m) ./ a(m);
    c(:, :, i) = ci;
end
c = max(0, min(1, c));

% store again
imwrite(uint8(c*255), 'test.transparent.png', 'Alpha', a);

% temporary files test1.png and test2.png can now be deleted
这是两个带有白色和黑色背景的临时图像。

这是生成的透明图像。要查看,请保存图像并使用合适的查看器查看


最后一条注释:对于高质量的生产图形,您可能希望关闭(将Visible属性设置为off of)具有透明度的所有渲染零件,并将轴和标签保存为矢量化输出,然后反转可见性,也就是说,关闭所有轴和标签,仅显示具有透明度的渲染部分,并使用两种不同的背景色保存它们,然后重建透明度并覆盖它们、矢量化轴和标签以及具有透明度的渲染部分,并将其合并。从Matlab 2020b开始,Matlab无法独立完成这项工作。

我接受这一点,但作为查看器中的旁注,我进入了编辑->复制图形并粘贴到powerpoint中,它似乎保留了下来transparency@Aly我猜这是因为它是通过
emf
格式复制的。也许另存为emf将保持透明度。(我还没有测试过自己)PNG呢?你有没有为SVG保存透明度的示例代码?2018年3月,我只是手工编辑了背景,通过注释掉你所说的两行
re
。如果修复如此简单,而且确实如此,为什么TMW在生成eps图像时不提供透明背景的选项呢?在使用2014a生成的eps图中,
c0 1 j 1 sg 0 12252 11412 rf 6 w
具有必要的
rf
。在2017b中,看起来像这样的序列,当然有换行符:
1 GC N 0 576 756 re f GR GS[1 0 0 1 18 18]CT 1 GC N 0 0 576 756 re f GR