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