DICOM.jl icon indicating copy to clipboard operation
DICOM.jl copied to clipboard

Common Image/Array Interface

Open Tokazama opened this issue 3 years ago • 5 comments

I've been working on revamping a lot of the underlying components of JuliaImages for quite some time now (this rambling issue) and I'd like to see if I could assist with some things in DICOM.jl along the way.

I've put together some of this already in the docs for NeuroCore here, but there are basically four components:

  1. Raw array data: the image
  2. Dimension names: if it were an axial view the dimension names could be something like (:sagittal, :coronal).
  3. Axis keys: The actual pixels coordinates in millimeters along each axis
  4. Metadata: Some structure (typically a dictionary) that stores the non array specific data

I was hoping to get a minimal working example for this but I got a bit stuck on some of the details and I'm sure that depending on the modality the tags used to compose each part of this would be different, so here's an example where I fudged certain details with obnoxiously long method names.

function dcm_to_image(data::Dict{Tuple{UInt16,UInt16},Any}, wanted_names)
    raw_image = data[(0x7FE0, 0x0010)]
    dimension_names = get_dimension_names(data)
    xkeys = range(x_axis_start(data), step = data[(0x0028, 0x0030)][1], length= data[(0x0028, 0x0011)]) * Unitful.mm
    ykeys = range(y_axis_start(data), step = data[(0x0028, 0x0030)][2], length= data[(0x0028, 0x0011)]) * Unitful.mm

    # convert names to snake case symbols so they can be retreived like properties
    meta = Dict{Symbol,Any}()
    for x in wanted_names
        meta[Symbol(join(lowercase.(split(k)), "_"))] = DICOM.lookup(data, x)
    end

    return AxisIndices.NamedMetaAxisArray{dimension_names}(raw_image, (xkeys, ykeys), metadata=meta)
end

There are some other things we can do to make it easy to convert to color types also, but this is the basic idea. Also, if this ends up gaining some traction I have figured out ways to encode spatial information that should be compatible with JuliaImges, so rotation and linear transforms should be possible to include.

The ugliest part of this seems to be handling metadata. The metadata doesn't necessarily have to use Symbol keys, but 1) it allows doing image.kept_name and 2) it follows the norm in Julia of referring to dictionaries of labeled data using Symbol.

Tokazama avatar Aug 12 '20 22:08 Tokazama