視覺化醫學影像CT

熱衷開源的Boy發表於2020-12-13

DICOM格式

Digital Imaging and Communications in Medicine (DICOM)是醫學標準格式的醫學影像

2載入第三方包

IS_LOCAL = False
import numpy as np
import pandas as pd
from skimage.io import imread
import seaborn as sns
import matplotlib.pyplot as plt
from glob import glob
if(IS_LOCAL):
    import pydicom as dicom
else:
    import dicom
import os

 

2.引數設定和檔案路徑

if(IS_LOCAL):
    PATH="../input/siim-medical-image/"
else:
    PATH="../input/"
print(os.listdir(PATH))

3.讀取資料

data_df = pd.read_csv(os.path.join(PATH,"overview.csv"))
Hide
In [4]:
print("CT Medical images -  rows:",data_df.shape[0]," columns:", data_df.shape[1])

4讀取TIFF格式資料

print("Number of TIFF images:", len(os.listdir(os.path.join(PATH,"tiff_images"))))
tiff_data = pd.DataFrame([{'path': filepath} for filepath in glob(PATH+'tiff_images/*.tif')])

5.處理TIFF資料

def process_data(path):
    data = pd.DataFrame([{'path': filepath} for filepath in glob(PATH+path)])
    data['file'] = data['path'].map(os.path.basename)
    data['ID'] = data['file'].map(lambda x: str(x.split('_')[1]))
    data['Age'] = data['file'].map(lambda x: int(x.split('_')[3]))
    data['Contrast'] = data['file'].map(lambda x: bool(int(x.split('_')[5])))
    data['Modality'] = data['file'].map(lambda x: str(x.split('_')[6].split('.')[-2]))
    return data
tiff_data = process_data('tiff_images/*.tif')

6.檢查資料集資訊

tiff_data.head(10)

7.讀取DICOM資料集

print("Number of DICOM files:", len(os.listdir(PATH+"dicom_dir")))

8.處理DICOM資料

dicom_data = process_data('dicom_dir/*.dcm')

9.檢查資訊

dicom_data.head(10)

10.檢查資料的一致性,

def countplot_comparison(feature):
    fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize = (16, 4))
    s1 = sns.countplot(data_df[feature], ax=ax1)
    s1.set_title("Overview data")
    s2 = sns.countplot(tiff_data[feature], ax=ax2)
    s2.set_title("Tiff files data")
    s3 = sns.countplot(dicom_data[feature], ax=ax3)
    s3.set_title("Dicom files data")
    plt.show()

11.顯示TIFF影像

def show_images(data, dim=16, imtype='TIFF'):
    img_data = list(data[:dim].T.to_dict().values())
    f, ax = plt.subplots(4,4, figsize=(16,20))
    for i,data_row in enumerate(img_data):
        if(imtype=='TIFF'): 
            data_row_img = imread(data_row['path'])
        elif(imtype=='DICOM'):
            data_row_img = dicom.read_file(data_row['path'])
        if(imtype=='TIFF'):
            ax[i//4, i%4].matshow(data_row_img,cmap='gray')
        elif(imtype=='DICOM'):
            ax[i//4, i%4].imshow(data_row_img.pixel_array, cmap=plt.cm.bone) 
        ax[i//4, i%4].axis('off')
        ax[i//4, i%4].set_title('Modality: {Modality} Age: {Age}\nSlice: {ID} Contrast: {Contrast}'.format(**data_row))
    plt.show()

應用函式,顯示影像

show_images(tiff_data,16,'TIFF')

12.顯示DICOM資料

# extract voxel data  
def extract_voxel_data(list_of_dicom_files):  
    datasets = [dicom.read_file(f) for f in list_of_dicom_files]  
     try:  
         voxel_ndarray, ijk_to_xyz = dicom_numpy.combine_slices(datasets)  
     except dicom_numpy.DicomImportException as e:  
     # invalid DICOM data  
         raise  
     return voxel_ndarray 

13.檢視DICOMM更多的資訊

dicom_file_path = list(dicom_data[:1].T.to_dict().values())[0]['path']
dicom_file_dataset = dicom.read_file(dicom_file_path)
dicom_file_dataset

更多的資訊:哪個醫院,病人年齡、性別、名字、病人的ID、診斷方式

14.我們可以修改視覺化函式,來顯示引數

def show_dicom_images(data):
    img_data = list(data[:16].T.to_dict().values())
    f, ax = plt.subplots(4,4, figsize=(16,20))
    for i,data_row in enumerate(img_data):

        data_row_img = dicom.read_file(data_row['path'])
        modality = data_row_img.Modality
        age = data_row_img.PatientAge
        
        ax[i//4, i%4].imshow(data_row_img.pixel_array, cmap=plt.cm.bone) 
        ax[i//4, i%4].axis('off')
        ax[i//4, i%4].set_title('Modality: {} Age: {}\nSlice: {} Contrast: {}'.format(
         modality, age, data_row['ID'], data_row['Contrast']))
    plt.show()

 

參考資料https://www.kaggle.com/gpreda/visualize-ct-dicom-data

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章