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

如何使用dcm4che解压缩/解封装DICOM的Pixel Data片段?

解决dcm4che读取压缩DICOM像素数据的问题

你遇到的情况是DICOM图像采用了压缩存储(此处为西门子CT的封装压缩格式),直接读取PixelData得到的Fragments是压缩后的分片数据,需要借助dcm4che内置的解码工具解压,才能得到预期的512×512×16位像素数组。

具体解决方案

dcm4che提供的PixelDataReader类可自动识别并处理常见DICOM压缩格式(包括西门子私有封装格式),无需手动解析分片数据。

步骤1:引入必要依赖

如果使用Maven,需添加dcm4che-image模块:

<dependency>
    <groupId>org.dcm4che</groupId>
    <artifactId>dcm4che-image</artifactId>
    <version>你的dcm4che版本(如5.3.7)</version>
</dependency>

步骤2:修改代码读取解压后的像素数据

替换原有代码,通过PixelDataReader直接获取可访问的像素数组:

import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.imageio.codec.PixelDataReader;
import java.io.File;
import java.io.IOException;

public void readDicomPixelData(String filename) throws IOException {
    File file = new File(filename);
    short[] pixelData; // 16位像素对应short数组

    try (DicomInputStream dis = new DicomInputStream(file)) {
        Attributes attrs = dis.readDataset();
        
        // 创建PixelDataReader,自动处理压缩/封装逻辑
        PixelDataReader reader = PixelDataReader.newInstance(attrs);
        // 读取解压后的像素数据
        pixelData = (short[]) reader.readPixelData(0, 0, attrs.getInt(Tag.Rows, 0), attrs.getInt(Tag.Columns, 0));
    }

    // 访问指定位置的像素值示例
    int row = 100;
    int col = 100;
    short pixelValue = pixelData[row * 512 + col];
    
    // 若需转换为HU值,结合标签0028|1052和0028|1053计算
    double intercept = attrs.getDouble(Tag.RescaleIntercept, 0);
    double slope = attrs.getDouble(Tag.RescaleSlope, 1);
    double huValue = pixelValue * slope + intercept;
}

关键说明

  • PixelDataReader会自动从DICOM标签判断压缩格式,调用对应解码器完成解压,无需手动处理Fragments对象。
  • 16位像素数据以short数组存储,每个元素对应原始像素值,可结合Rescale参数转换为临床常用的HU值。

压缩与封装的差异

  • 压缩:像素数据通过编码算法(如RLE、JPEG)缩小体积,必须解码还原。
  • 封装:像素数据被分割为多个分片存储,可能是压缩或未压缩状态,需按封装规则拼接/解码。

内容的提问来源于stack exchange,提问作者Salu Ramos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 17:35:22