At the Kaggle Competition, CT scan images and DICOM images of the chest came out and I didn't know how to handle them.
・ What is a DICOM image? ・ How to display DICOM images and handle data
DICOM is a standard that defines the formats of medical images taken by CT, MRI, CR, etc. and the communication protocol between the medical image devices that handle them. wiki
Installation
%pip install pydicom
dataset contains metadata
import pydicom
import matplotlib.pyplot as plt
from pydicom.data import get_testdata_files
filename = get_testdata_files('CT_small.dcm')[0]
dataset = pydicom.dcmread(filename)
print(dataset)
The value can be displayed and accessed by key name (.Rows) or number (0x0010,0x0010).
dataset[0x0010, 0x0010]
dataset.Rows
Until the image is displayed
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(dataset[0x0018, 0x0050])
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)"))
# plot the image using matplotlib
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()
Recommended Posts