Skip to content

Read dicom overlay with pydicom

Miroslav Jiřík edited this page Sep 6, 2013 · 7 revisions
"""
pydicom is python module used for load dicom data. Dicom overlay
is saved on (60xx,3000) bit after bit. Data are decoded and 
each bit is stored as array element.
"""
import dicom
import numpy as np
import matplotlib.pyplot as plt

data = dicom.read_file('volumetry_slice.DCM')
# overlay index 
i_overlay = 1
n_bits = 8


# On (60xx,3000) are stored ovelays. 
# First is (6000,3000), second (6002,3000), third (6004,3000),  
# and so on.
dicom_tag1 = 0x6000 + 2*i_overlay

overlay_raw = data[dicom_tag1 ,0x3000].value

# On (60xx,0010) and (60xx,0011) is stored overlay size
rows = data[dicom_tag1,0x0010].value # rows = 512
cols = data[dicom_tag1,0x0011].value # cols = 512

decoded_linear = np.zeros(len(overlay_raw)*n_bits)

# Decoding data. Each bit is stored as array element
for i in range(1,len(overlay_raw)):
    for k in range (0,n_bits):
        byte_as_int = ord(overlay_raw[i]) 
        decoded_linear[i*n_bits + k] = (byte_as_int >> k) & 0b1

#overlay = np.array(pol)

overlay = np.reshape(decoded_linear,[rows,cols])

plt.imshow(overlay)
plt.show()
Clone this wiki locally