Xem trên TensorFlow.org | Chạy trong Google Colab | Xem nguồn trên GitHub | Tải xuống sổ ghi chép |
Tổng quat
Đây hướng dẫn chương trình làm thế nào để sử dụng tfio.image.decode_dicom_image trong TensorFlow IO để decode DICOM file với TensorFlow.
Thiết lập và sử dụng
Tải xuống hình ảnh DICOM
Những hình ảnh DICOM sử dụng trong hướng dẫn này là từ NIH Chụp X-ray bộ dữ liệu .
NIH Chụp X-ray bộ dữ liệu gồm 100.000 hình ảnh de-xác định của ngực x-quang ở định dạng PNG, được cung cấp bởi NIH lâm sàng Trung tâm và có thể được tải về thông qua liên kết này .
Google Cloud cũng cung cấp một phiên bản DICOM trong những hình ảnh, có sẵn trong Cloud Storage .
Trong hướng dẫn này, bạn sẽ tải về một tập tin mẫu của các bộ dữ liệu từ repo GitHub
- Xiaosong Wang, Yifan Peng, Le Lu, Zhiyong Lu, Mohammadhadi Bagheri, Ronald Summers, ChestX-ray8: Cơ sở dữ liệu chụp X-quang ngực quy mô bệnh viện và các điểm chuẩn về phân loại được giám sát yếu và bản địa hóa các bệnh thường gặp ở lồng ngực, IEEE CVPR, trang 3462 -3471, 2017
curl -OL https://github.com/tensorflow/io/raw/master/docs/tutorials/dicom/dicom_00000001_000.dcmls -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
Cài đặt các Gói cần thiết và khởi động lại thời gian chạy
try:
# Use the Colab's preinstalled TensorFlow 2.x
%tensorflow_version 2.x
except:
pass
pip install tensorflow-io
Giải mã hình ảnh 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

Giải mã siêu dữ liệu DICOM và làm việc với các thẻ
decode_dicom_data giải mã thông tin thẻ. dicom_tags chứa thông tin hữu ích như tuổi và giới tính của bệnh nhân, vì vậy bạn có thể sử dụng DICOM thẻ như dicom_tags.PatientsAge và dicom_tags.PatientsSex . tensorflow_io mượn ký hiệu thẻ tương tự từ gói dicom pydicom.
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
Xem trên TensorFlow.org
Chạy trong Google Colab
Xem nguồn trên GitHub
Tải xuống sổ ghi chép