python中tile
㈠ 如何應用Python處理醫學影像學中的DICOM信息
下面Python代碼來演示如何編程處理心血管冠脈造影DICOM圖像信息。
1. 導入主要框架:SimpleITK、pydicom、PIL、cv2和numpy
import SimpleITK as sitk
from PIL import Image
import pydicom
import numpy as np
import cv2
2. 應用SimpleITK框架來讀取DICOM文件的矩陣信息。如果DICOM圖像是三維螺旋CT圖像,則幀參數則代表CT掃描層數;而如果是造影動態電影圖像,則幀參數就是15幀/秒的電影圖像幀數。
def loadFile(filename):
ds = sitk.ReadImage(filename)
img_array = sitk.GetArrayFromImage(ds)
frame_num, width, height = img_array.shape
return img_array, frame_num, width, height
3. 應用pydicom來提取患者信息。
def loadFileInformation(filename):
information = {}
ds = pydicom.read_file(filename)
information['PatientID'] = ds.PatientID
information['PatientName'] = ds.PatientName
information['PatientBirthDate'] = ds.PatientBirthDate
information['PatientSex'] = ds.PatientSex
information['StudyID'] = ds.StudyID
information['StudyDate'] = ds.StudyDate
information['StudyTime'] = ds.StudyTime
information['InstitutionName'] = ds.InstitutionName
information['Manufacturer'] = ds.Manufacturer
information['NumberOfFrames'] = ds.NumberOfFrames
return information
4. 應用PIL來檢查圖像是否被提取。
def showImage(img_array, frame_num = 0):
img_bitmap = Image.fromarray(img_array[frame_num])
return img_bitmap
5. 採用CLAHE (Contrast Limited Adaptive Histogram Equalization)技術來優化圖像。
def limitedEqualize(img_array, limit = 4.0):
img_array_list = []
for img in img_array:
clahe = cv2.createCLAHE(clipLimit = limit, tileGridSize = (8,8))
img_array_list.append(clahe.apply(img))
img_array_limited_equalized = np.array(img_array_list)
return img_array_limited_equalized