LoginSignup
23
29

More than 3 years have passed since last update.

PythonでDICOM画像を扱う

Last updated at Posted at 2020-09-12

きっかけ

Kaggleコンペで胸部のCTスキャン画像、DICOM画像が出てきてどのように扱えばいいか分からなかったから。

何ができるようになるか

・DICOM画像とは何か
・DICOM画像の表示、データの扱い方

DICOM画像とは何か?

DICOM(ダイコム)とは、CTやMRI、CRなどで撮影した医用画像のフォーマットと、それらを扱う医用画像機器間の通信プロトコルを定義した標準規格である。
wiki

使用例

インストール

%pip install pydicom

datasetにはメタデータが入っている

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)

image.png

値の表示はキー名(.Rows)や番号(0x0010,0x0010)で表示,アクセスできる。

dataset[0x0010, 0x0010]
dataset.Rows

image.png

画像の表示まで

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()

image.png

参考

pydicom

23
29
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
29