#dicom

dicom-pixeldata

A high-level API for decoding DICOM objects into images and ndarrays

4 releases

new 0.1.1 May 12, 2022
0.1.0 Apr 28, 2022
0.1.0-rc.2 Feb 3, 2022
0.1.0-rc.1 Nov 18, 2021

#126 in Images

Download history 3/week @ 2022-01-27 22/week @ 2022-02-03 9/week @ 2022-02-10 10/week @ 2022-02-17 8/week @ 2022-02-24 10/week @ 2022-03-03 15/week @ 2022-03-10 15/week @ 2022-03-17 9/week @ 2022-03-24 13/week @ 2022-03-31 14/week @ 2022-04-07 11/week @ 2022-04-14 23/week @ 2022-04-21 69/week @ 2022-04-28 74/week @ 2022-05-05 135/week @ 2022-05-12

302 downloads per month
Used in 2 crates

MIT/Apache

2MB
33K SLoC

DICOM-rs pixeldata

CratesIO Documentation

This sub-project is directed at users of the DICOM-rs ecosystem. It provides constructs for handling DICOM pixel data and is responsible for decoding pixel data elements into images or multi-dimensional arrays.

This crate is part of the DICOM-rs project.


lib.rs:

This crate contains the DICOM pixel data handlers and is responsible for decoding various forms of native and compressed pixel data, such as JPEG lossless, and convert it into more usable data structures.

dicom-pixeldata currently supports a small, but increasing number of DICOM image encodings in pure Rust. As a way to mitigate the current gap, this library has an integration with GDCM bindings for an extended range of encodings. This integration is behind the Cargo feature "gdcm", which requires CMake and a C++ compiler.

dicom-pixeldata = { version = "0.1", features = ["gdcm"] }

Once the pixel data is decoded, the decoded data can be converted to:

This conversion includes eventual Modality and value of interest (VOI) transformations.

WebAssembly support

This library works in WebAssembly by ensuring that the "gdcm" feature is disabled. This allows the crate to be compiled for WebAssembly albeit at the cost of supporting a lesser variety of compression algorithms.

Examples

To convert a DICOM object into a dynamic image:

# use std::error::Error;
use dicom_object::open_file;
use dicom_pixeldata::PixelDecoder;
# fn main() -> Result<(), Box<dyn Error>> {
let obj = open_file("dicom.dcm")?;
let image = obj.decode_pixel_data()?;
let dynamic_image = image.to_dynamic_image(0)?;
dynamic_image.save("out.png")?;
# Ok(())
# }

To convert a DICOM object into an ndarray:

# use std::error::Error;
use dicom_object::open_file;
use dicom_pixeldata::PixelDecoder;
use ndarray::s;
# fn main() -> Result<(), Box<dyn Error>> {
let obj = open_file("rgb_dicom.dcm")?;
let pixel_data = obj.decode_pixel_data()?;
let ndarray = pixel_data.to_ndarray::<u16>()?;
let red_values = ndarray.slice(s![.., .., .., 0]);
# Ok(())
# }

In order to parameterize the conversion, pass a conversion options valueto the _with_options variant methods.

# use std::error::Error;
use dicom_object::open_file;
use dicom_pixeldata::{ConvertOptions, PixelDecoder, VoiLutOption};
# fn main() -> Result<(), Box<dyn Error>> {
let obj = open_file("dicom.dcm")?;
let image = obj.decode_pixel_data()?;
let options = ConvertOptions::new()
    .with_voi_lut(VoiLutOption::Normalize)
    .force_8bit();
let dynamic_image = image.to_dynamic_image(0)?;
# Ok(())
# }

See [ConvertOptions] for the options available, including the default behavior for each method.

Dependencies

~12–16MB
~272K SLoC