Skip to content

Instantly share code, notes, and snippets.

@matheustguimaraes
Created October 10, 2018 19:56
Show Gist options
  • Save matheustguimaraes/99c326f440f905aa83737647216f524d to your computer and use it in GitHub Desktop.
Save matheustguimaraes/99c326f440f905aa83737647216f524d to your computer and use it in GitHub Desktop.
Read dicom image information
import numpy as np
import pydicom
import matplotlib.pyplot as plt
from pydicom.data import get_testdata_files
print(__doc__)
def get_database(filename, indent=0):
"""Go through all items in the dataset and print them with custom format
Modelled after Dataset._pretty_str()
"""
dont_print = ['Pixel Data', 'File Meta Information Version']
indent_string = " " * indent
next_indent_string = " " * (indent + 1)
for data_element in filename:
if data_element.VR == "SQ": # a sequence
print(indent_string, data_element.name)
for sequence_item in data_element.value:
get_database(sequence_item, indent + 1)
print(next_indent_string + "---------")
else:
if data_element.name in dont_print:
print("""<item not printed -- in the "don't print" list>""")
else:
repr_value = repr(data_element.value)
if len(repr_value) > 50:
repr_value = repr_value[:50] + "..."
print("{0:s} {1:s} = {2:s}".format(indent_string,
data_element.name,
repr_value))
def load_dcm(img_path):
dcm = pydicom.read_file(img_path)
RescaleIntercept = int(dcm.data_element('RescaleIntercept').value)
img = np.array(dcm.pixel_array, dtype=np.int16) + RescaleIntercept
return img
# 2 ^ 11 bytes = 2048
filename = "ImagensTC_Pulmao/8.dcm"
dataset = pydicom.dcmread(filename, force=True)
dataset.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
# dataset.pixel_array
# filename = get_testdata_files('MR_small.dcm')[0]
# ds = pydicom.dcmread(filename)
# myprint(ds)
# Normal mode:
print()
print("Filename.........:", filename)
print("Storage type.....:", dataset.SOPClassUID)
print()
pat_name = dataset.PatientName
display_name = pat_name.family_name + ", " + pat_name.given_name
print("Patient's name...:", display_name)
print("Patient id.......:", dataset.PatientID)
print("Modality.........:", dataset.Modality)
print("Study Date.......:", dataset.StudyDate)
# print("Bit Depth........:", dataset.pixel_array)
if 'PixelData' in dataset:
rows = int(dataset.Rows)
cols = int(dataset.Columns)
print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
rows=rows, cols=cols, size=len(dataset.PixelData)))
if 'PixelSpacing' in dataset:
print("Pixel spacing....:", dataset.PixelSpacing)
# use .get() if not sure the item exists, and want a default value if missing
print("Slice location...:", dataset.get('SliceLocation', "(missing)"))
get_database(dataset)
# plot the image using matplotlib
plt.imshow(dataset.pixel_array, cmap=plt.bone())
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment