Skip to Content
go-dicom(3)
Packages

volume

Assembles the slices of a series into a regular stack: which slice comes next, how far apart they are, and where each lies in the patient. It reads geometry and decides order; it does not load pixels.

import "github.com/amrshadid/go-dicom/volume" · pkg.go.dev

When to use it

Before building a 3D array from a CT or MR series, or before measuring across slices. It accepts single-frame images (one file each) and the frames of Enhanced CT, Enhanced MR and Legacy Converted multi-frame images.

Key types and functions

NameWhat it is for
AssembleOrder a series' data sets into a stack, or refuse it.
VolumeRows, columns, pixel spacing, orientation, normal, slice spacing, frames, padding.
FrameOne slice: which data set, which frame in it, position, offset along the normal.
Volume.ReadFrameA slice's pixels in stack order, through a read function you give.
Options, DefaultSpacingToleranceSpacing and angle tolerances.
Error, ErrorKind, IsKindWhy a series was refused.
KindGap, KindTilt, KindMixedGeometrySome of the reasons: a missing slice, a tilt, slices of different size.
PixelPaddingThe padding value and range, when the series declares one.

Example

// Put the slices of a series in stack order and report its geometry.
package main

import (
	"errors"
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/dataset"
	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/volume"
)

func main() {
	var series []*dataset.Dataset
	for _, path := range os.Args[1:] { // the files of one series, in any order
		f, err := os.Open(path)
		if err != nil {
			log.Fatal(err)
		}
		df, err := filereader.ReadDICOMFileWithOptions(filebase.NewFileReader(f),
			filereader.ReadOptions{SkipBulkData: true}) // geometry needs no pixels
		f.Close()
		if err != nil {
			log.Fatal(err)
		}
		series = append(series, df.GetDataset())
	}

	v, err := volume.Assemble(series, volume.Options{})
	var refused *volume.Error
	if errors.As(err, &refused) {
		log.Fatalf("not a regular stack (%v): %s", refused.Kind, refused.Detail)
	} else if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%d slices of %dx%d, pixel spacing %v mm, slice spacing %g mm\n",
		len(v.Frames), v.Columns, v.Rows, v.PixelSpacing, v.Spacing)
	for i, fr := range v.Frames {
		fmt.Printf("slice %d: file %d frame %d at %.2f mm\n", i, fr.Source, fr.Index, fr.Offset)
	}
}

Given CT_small.dcm it prints one slice of 128x128 with pixel spacing [0.661468 0.661468]. Given CT_small.dcm and MR_small.dcm it refuses them with KindMixedGeometry.

Notes

  • A stack that assembles is one an array indexed by slice, row and column describes exactly. Anything else is refused rather than guessed: uneven spacing, a gap, a tilt, two slices at one position, missing geometry.
  • Read with filereader.ReadOptions{SkipBulkData: true}: geometry needs no pixels, and ReadFrame reads them later from the files.
  • Spacing is 0 for a single slice.

Tip

Test the reason with errors.As(err, &refused) and refused.Kind, or volume.IsKind(err, volume.KindGap), to decide whether to resample or ask for the missing slice.

See also

dataset · filereader · Pixel data

On this page