解碼用於醫學影像的 DICOM 檔案

在 TensorFlow.org 上檢視 在 Google Colab 中執行 在 GitHub 上檢視原始碼 下載筆記本

總覽

本教學課程說明如何使用 TensorFlow I/O 中的 tfio.image.decode_dicom_image,以使用 TensorFlow 解碼 DICOM 檔案。

設定與使用方式

下載 DICOM 影像

本教學課程中使用的 DICOM 影像來自 NIH 胸腔 X 光資料集

NIH 胸腔 X 光資料集包含 NIH 臨床中心提供的 100,000 張去識別化 PNG 格式胸腔 X 光影像,可透過此連結下載。

Google Cloud 也提供 DICOM 版本的影像,可在 Cloud Storage 中取得。

在本教學課程中,您將從 GitHub 存放庫下載資料集的範例檔案

  • Xiaosong Wang、Yifan Peng、Le Lu、Zhiyong Lu、Mohammadhadi Bagheri、Ronald Summers,「ChestX-ray8:醫院規模的胸腔 X 光資料庫以及常見胸腔疾病的弱監督式分類和定位基準」,IEEE CVPR,第 3462-3471 頁,2017 年
curl -OL https://github.com/tensorflow/io/raw/master/docs/tutorials/dicom/dicom_00000001_000.dcm
ls -l dicom_00000001_000.dcm
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   164    0   164    0     0    600      0 --:--:-- --:--:-- --:--:--   598
100 1024k  100 1024k    0     0  1915k      0 --:--:-- --:--:-- --:--:-- 1915k
-rw-rw-r-- 1 kbuilder kokoro 1049332 Nov 22 03:47 dicom_00000001_000.dcm

安裝必要的套件,然後重新啟動執行階段

try:
  # Use the Colab's preinstalled TensorFlow 2.x
  %tensorflow_version 2.x 
except:
  pass
pip install tensorflow-io

解碼 DICOM 影像

import matplotlib.pyplot as plt
import numpy as np

import tensorflow as tf
import tensorflow_io as tfio

image_bytes = tf.io.read_file('dicom_00000001_000.dcm')

image = tfio.image.decode_dicom_image(image_bytes, dtype=tf.uint16)

skipped = tfio.image.decode_dicom_image(image_bytes, on_error='skip', dtype=tf.uint8)

lossy_image = tfio.image.decode_dicom_image(image_bytes, scale='auto', on_error='lossy', dtype=tf.uint8)


fig, axes = plt.subplots(1,2, figsize=(10,10))
axes[0].imshow(np.squeeze(image.numpy()), cmap='gray')
axes[0].set_title('image')
axes[1].imshow(np.squeeze(lossy_image.numpy()), cmap='gray')
axes[1].set_title('lossy image');
2021-11-22 03:47:53.016507: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected

png

解碼 DICOM Metadata 並使用標籤

decode_dicom_data 會解碼標籤資訊。dicom_tags 包含患者年齡和性別等實用資訊,因此您可以使用 DICOM 標籤,例如 dicom_tags.PatientsAgedicom_tags.PatientsSex。tensorflow_io 借用了 pydicom dicom 套件的相同標籤標記法。

tag_id = tfio.image.dicom_tags.PatientsAge
tag_value = tfio.image.decode_dicom_data(image_bytes,tag_id)
print(tag_value)
tf.Tensor(b'58', shape=(), dtype=string)
print(f"PatientsAge : {tag_value.numpy().decode('UTF-8')}")
PatientsAge : 58
tag_id = tfio.image.dicom_tags.PatientsSex
tag_value = tfio.image.decode_dicom_data(image_bytes,tag_id)
print(f"PatientsSex : {tag_value.numpy().decode('UTF-8')}")
PatientsSex : M