You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

保存带透明效果的DICOM图像时如何去除白色背景?

解决MATLAB保存带透明PNG时的白色背景问题

我在MATLAB中处理DICOM图像,给不需要显示的像素设置了100%透明度,但保存为PNG文件时出现了不想要的白色背景。保存后的效果图如下:

保存后的效果图

以下是我使用的MATLAB代码:

% Clear the workspace and command window
clc;
clear;

% Specify the file paths for the DICOM images
filePaths = {
    'C:\Users\Chekmenev Lab\Dropbox\Next Energy Image processing\Propane Lung MRI Images\HP propane Pig Lung Imaging\DCOM files\05262023 Dicom\27\S0000319\SE004093\I0000010.dcm',
    'C:\Users\Chekmenev Lab\Dropbox\Next Energy Image processing\Propane Lung MRI Images\HP propane Pig Lung Imaging\DCOM files\05262023 Dicom\27\S0000319\SE004093\I0000014.dcm',
};

% Read the DICOM images
image1 = dicomread(filePaths{1});
image2 = dicomread(filePaths{2});

% Compute the subtraction image
subtracted_image = double(image1) - double(image2);

% Resize the image to 256x256
subtracted_image = imresize(subtracted_image, [256, 256]);

% Set the threshold values
threshold1 = 250; % Adjust the threshold as needed
threshold2 = 760;

% Set the values outside the desired range to NaN
subtracted_image(subtracted_image < threshold1 | subtracted_image > threshold2) = NaN;

% Create a binary mask for the pixels below the threshold
mask = subtracted_image < threshold1;

% Create a binary mask for the pixels above the threshold
mask2 = subtracted_image > threshold2;

% Create a new figure
figure;

% Display the image with colormap and transparency
h = imshow(subtracted_image, []);
colormap(jet);

% Create a new colormap with white for 100% transparency
cmap = jet(255);

% Apply the new colormap
colormap(cmap);

% Set the transparency values for the pixels below and above the threshold
alpha_data = ones(size(subtracted_image));

% Set transparency to 0 for pixels below the threshold
alpha_data(mask) = 0;

% Set transparency to 0 for pixels above the threshold
alpha_data(mask2) = 0;

% Set transparency to 0 for NaN values
alpha_data(isnan(subtracted_image)) = 0;

% Set the transparency (AlphaData) of the image
set(h, 'AlphaData', alpha_data);

% Set the figure size to 256x256 pixels
set(gcf, 'Units', 'pixels', 'Position', [100 100 256 256]);

% Remove extra space around the image
set(gca, 'Position', [0 0 1 1]);

% Add colorbar
c = colorbar('Location', 'eastoutside');

% Adjust the position of the colorbar
c.Position = [0.95 0.1 0.03 0.8];

% Remove the title
title('');

% Save the figure as a PNG file with a 256x256 resolution
saveas(gcf, 'HP subtraction.png', 'png');

请问有没有方法可以去除这个白色背景?


解决方法

问题根源是saveas函数对透明通道的支持不足,且figure默认背景为白色,以下是两种可行方案:

方案1:用exportgraphics替代saveas(推荐MATLAB R2020a+)

exportgraphics是MATLAB后期版本推出的专业导出工具,能完美保留透明通道。把代码最后一行的saveas替换成:

exportgraphics(gcf, 'HP subtraction.png', 'BackgroundColor', 'none');
  • BackgroundColor设为'none'直接让背景透明
  • 如果需要固定分辨率,可以加上'Resolution', 300参数(数值按需调整)

方案2:手动生成RGBA图像保存(兼容旧版本)

如果你的MATLAB版本不支持exportgraphics,可以直接处理图像数据生成带Alpha通道的PNG:
在代码末尾添加/替换以下代码:

% 获取坐标轴内的RGB图像数据
rgb_img = frame2im(getframe(gca));
% 将alpha数据转换为0-255的uint8格式
alpha_channel = uint8(alpha_data * 255);
% 合并成RGBA四通道图像
rgba_img = cat(3, rgb_img, alpha_channel);
% 保存带透明通道的PNG
imwrite(rgba_img, 'HP subtraction.png', 'Alpha', alpha_channel);

额外优化:提前设置透明背景

在创建figure时就把背景设为透明,避免渲染时的白色残留:

% 替换原来的figure();
figure('Color', 'none');
set(gca, 'Color', 'none');

内容的提问来源于stack exchange,提问作者Redwan Shovon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 05:13:19