get_DICOM.py 913 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
from pydicom import read_file
from pydicom.tag import Tag
from sys import argv
import numpy as np
import matplotlib.pyplot as plt

def get_array_from_overlay(dcm):
    """ Return a 2D numpy array of the overlay of index 1 for the given DICOM file.
        Usefull to extract dose informaiton from `Siemens MedCom Object Graphics`.
    """
11
    size = (int(dcm[Tag(0x60000010)].value), int(dcm[Tag(0x60000011)].value))
12
    overlay_raw = dcm[Tag(0x60003000)].value
13 14 15 16 17
    area = size[0] * size[1]
    length = len(overlay_raw)
    n_bits = int(area / length)
    decoded_linear = np.zeros(area)
    for i in range(1, length):
18 19 20
        bits = tuple(int(j) for j in format(overlay_raw[i], '08b'))[::-1]
        for k in range (0, n_bits):
            decoded_linear[i * n_bits + k] = bits[k]
21
    return np.reshape(decoded_linear, size)
22

23
overlay = get_array_from_overlay(read_file(argv[-1]))
24 25
plt.imshow(overlay)
plt.show()