Skip to Content
go-dicom(3)
Packages

pixels

Native pixel bytes with the attributes that describe them, and reading single samples and simple statistics (minimum, maximum, mean, median) from them.

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

When to use it

When you hold native (uncompressed) samples as bytes and want one sample at a row and column, or the range of the values, without converting the whole frame.

For almost everything else, use dataset: PixelArrayInterpreted gives every sample as a typed array, decoding compressed data first; GetPixelDataInfo gives the attributes; RenderFrame gives a picture. Dataset.PixelArrayWithAccessor and GetPixelStatistics return this package's types for a data set.

Key types and functions

NameWhat it is for
PixelData, NewPixelDataThe bytes with rows, columns, frames, bits, sign, samples and byte order.
NewAccessor, Accessor.GetPixelValueOne sample, by row, column and frame.
Accessor.GetInterpretedValueThe same as a float64.
NewCalculator, Calculator.CalculateStatisticsMinimum, maximum, mean and median over every sample.
Calculator.CalculateStatisticsSampledThe same over a fraction of them.
NewValidatorCheck that the attributes and the byte count agree.

Example

// Read single samples and simple statistics from native pixel bytes.
package main

import (
	"fmt"
	"log"
	"os"

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

func main() {
	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
	if err != nil {
		log.Fatal(err)
	}
	ds := df.GetDataset()
	info, err := ds.GetPixelDataInfo()
	if err != nil {
		log.Fatal(err)
	}
	raw, err := ds.DecodedPixelData()
	if err != nil {
		log.Fatal(err)
	}

	pd := pixels.NewPixelData(raw, uint32(info.Rows), uint32(info.Columns))
	pd.BitsAllocated = uint16(info.BitsAllocated)
	pd.BitsStored = uint16(info.BitsStored)
	pd.HighBit = uint16(info.HighBit)
	pd.PixelRepresentation = uint16(info.PixelRepresentation)
	pd.LittleEndian = true // DecodedPixelData is little endian

	v, err := pixels.NewAccessor(pd).GetPixelValue(0, 0, 0) // row, column, frame
	if err != nil {
		log.Fatal(err)
	}
	stats, err := pixels.NewCalculator(pd).CalculateStatistics()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("first %v, min %.0f, max %.0f, mean %.1f\n", v, stats.MinValue, stats.MaxValue, stats.MeanValue)
}

On CT_small.dcm it prints first 175, min 128, max 2191, mean 904.9.

Notes

  • NewPixelData sets only the data, rows and columns. Set the bit depths, Pixel Representation and LittleEndian yourself, or the samples are read wrongly; DecodedPixelData is always little endian.
  • Values are stored values: no rescale, no window.
  • The types hold no lock. They are safe to share for reading while nothing changes the PixelData.

Note

This is a thin helper, and nothing in the reading, rendering or coding path depends on it. dataset.Dataset.PixelArrayInterpreted is the usual way to get samples.

See also

dataset · compress · Pixel data

On this page