Enet4 / dicom-rs

Rust implementation of the DICOM standard
https://dicom-rs.github.io
Apache License 2.0
404 stars 76 forks source link

Error while converting 32-bit pixel data to ndarray or image #325

Open arturocs opened 1 year ago

arturocs commented 1 year ago

When I try to convert the pixel data from a RT Dose dicom to an image or an ndarray I get the following error: Error(InvalidBitsAllocated { backtrace: Backtrace(())) }) I have followed the examples described here and as a test file I have used this dicom: https://github.com/dicompyler/dicompyler-core/blob/master/tests/testdata/example_data/rtdose.dcm The error appears in version 0.1.5 of dicom_pixeldata whether or not I enable the gcmrs feature.

Enet4 commented 1 year ago

Thank you for reaching out!

The given RT dose image uses 32-bit pixel values, which is currently not supported by the DICOM-rs pixeldata crate. A known alternative to this is to fetch the pixel data manually from the object abstraction, but I would love to support this seamlessly though! I will take this issue as a tracker for the functionality.

arturocs commented 1 year ago

Thank you for your response. This function seems to work for my use case:

fn get_rtdose_pixel_data_as_ndarray(
    path: &str,
) -> Result<ndarray::Array3<u32>, Box<dyn std::error::Error>> {
    let obj = open_file(path)?;
    let pixel_data = obj.decode_pixel_data()?;
    let converted_data: Vec<_> = pixel_data
        .data()
        .chunks(4)
        .map(|i| u32::from_le_bytes([i[0], i[1], i[2], i[3]]))
        .collect();

    let ndarray = ndarray::Array3::from_shape_vec(
        [
            pixel_data.number_of_frames() as usize,
            pixel_data.rows() as usize,
            pixel_data.columns() as usize,
        ],
        converted_data,
    )?;
    Ok(ndarray)
}