如何在Matlab中得到透明背景的图片?

2005-11-10

MATLAB输出的矢量图往往不是透明背景的,这样插在ppt中就会很难看,就像贴了膏药似的。关于在MATLAB中如何得到透明背景的矢量图,我有个经验就是:用MATLAB导出pdf图形(而不是eps图形),然后用Acrobat另存成eps图形,往往就是透明的了。不过,如果一个图形包含多个子图,那么很可能只有第一个子图的背景是透明的,其余子图背景仍为白色。

附:
by alexxl 2006-02-11

在网上发现如下codes,可以生成透明图形。

% If you ever wondered how to get a matlab figure transparent (particularly usefull when you export it to formats that feature transparency, like eps, gif, png, etc.), here is how you do it:

% you created a figure and it is "current". % the following, you could have guessed
set(gcf,'color','none');
set(gca,'color','none');
% but this following little detail took me ages to figure out
set(gcf,'InvertHardCopy','off');

打开一个.fig的文件,在command中运行上面三行命令 Edit->Copy Figure->在powerpoint中粘贴,就得到背景透明的图片。

 
  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MATLAB ,要保存带有透明背景的图像,可以使用 `exportgraphics` 函数。首先,你需要确保你的图像有一个 alpha 通道,它指定了每个像素的不透明度。然后,你可以使用以下代码保存图像: ```matlab % 创建一张带有透明背景的图像 image = imread('input_image.png'); image_with_alpha = im2double(image); alpha_channel = create_alpha_channel(image_with_alpha); % 这里需要根据你的需求创建 alpha 通道 % 保存图像 output_image_filename = 'output_image.png'; exportgraphics(image_with_alpha, output_image_filename, 'Resolution', 300, 'BackgroundColor', 'none'); ``` 在上面的代码,`imread` 函数用于读取输入图像。接下来,我们使用 `im2double` 函数将图像转换为 double 类型,以便进行后续处理。然后,你需要使用 `create_alpha_channel` 函数创建一个与图像大小相同的 alpha 通道。这个函数的实现取决于你想要的透明度模式。最后,我们使用 `exportgraphics` 函数将带有透明背景的图像保存为 PNG 格式,并将背景颜色设置为 `'none'`。 请注意,`exportgraphics` 函数需要 MATLAB R2020b 或更高版本才能使用。如果你的 MATLAB 版本较旧,你可以尝试使用 `print` 函数来保存图像: ```matlab % 保存图像 output_image_filename = 'output_image.png'; print('-dpng', '-r300', '-opengl', output_image_filename); ``` 这里,`'-opengl'` 选项用于确保图像保存为具有透明背景的 PNG 格式。你可以调整分辨率(`'-r300'`)和输出文件名(`output_image.png`)以适应你的需求。 希望这能帮到你!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值