医学图像预处理 您所在的位置:网站首页 ct图像怎么处理 医学图像预处理

医学图像预处理

2024-07-12 15:36| 来源: 网络整理| 查看: 265

学习资料: UNet-肝脏肿瘤图像语义分割 医学图像预处理(三)——windowing(ct对比增强) Python CT图像预处理——nii格式读取、重采样、窗宽窗位设置 医学图像处理步骤为: 1、数据加载 2、CT图像增强 3、直方图均衡化增强 4、获取对应CT、掩膜灰度图并保存

DICOM数据读取:使用pydicom库中的dcmread函数进行读取 image_slices = [pydicom.dcmread(os.path.join(data_path, file_name)) for file_name in os.listdir(data_path)]

上述代码等同于

image_slices_test = [] for file_name_test in os.listdir(data_path): image_slices_test.append(pydicom.dcmread(os.path.join(data_path, file_name_test)))

作用是读取data_path中的文件,放入到image_slices中,得到一个129个大小的list,每一个list元素表示一个切片的全部信息。

排序:按照InstanceNumber(扫描的EPI时间序列顺序)顺序

img_slices.sort(key=lambda x:x.InstanceNumber)

提取像素值:

img_array = np.array([i.pixel_array for i in img_slices])

对于dicom、nii格式的图片,只要是ct图,就都可以选择将储存的原始数据转化为Hu值,Hu值代表了物体真正的密度。

对于nii格式的图片,在使用nibabel、simpleitk常用的api接口,都会自动的进行上述转化构成,即取出的值已是hu。若是想要得到原始数据:需要使用nib.load('xx').dataobj.get_unscaled()或者itk.ReadImage('xx').GetPixel(x,y,z) 对于dcm格式图片,simpleitk,pydicom常用的api接口都不会将原始数据转化为Hu!(itk snap软件读入dcm或nii都不会对数据进行scale操作)

HU值和像素值的转化,以dicm数据为例 HU = pixel_val * slope + intercept(如果Slope为1,Intercept为0,则不需要转化的问题,具体Slope和Intercept的值在头文件中都会有说明的) dicom转化为Hu的代码如下:

def get_pixels_hu(scans): # np.stack() # pixel_array 是一个正常的像素矩阵,512x512 image = np.stack([s.pixel_array for s in scans]) image = image.astype(np.int16) image[image == -2000] = 0 # 缩放斜率 RescaleIntercept intercept = scans[0].RescaleIntercept # 截距 RescaleSlope slope = scans[0].RescaleSlope if slope != 1: image = slope * image.astype(np.float64) image = image.astype(np.int16) image += np.int16(intercept) return np.array(image, dtype=np.int16)

根据Hu(CT)值来筛选我们想要的部位的图片,其他部位全部抹黑或者抹白,目的是为了增加图像对比度。使用windowing方法,观察的CT值范围:窗宽。观察的中心CT值即为窗位,然后对肿瘤部分进行二值化处理。

def windowing(img, window_width, window_center): # params:需要增强的图片, 窗口宽度, 窗中心 通过窗口最小值来线性移动窗口增强 min_windows = float(window_center)-0.5*float(window_width) new_img = (img-min_windows)/float(window_width) new_img[new_img1] = 1 # 抹黑 return (new_img * 255).astype('uint8') # 把数据整理成标准图像格式

直方图均衡化 将整个图像分成许多小块,对每个小块进行均衡化。Opencv中将这种方法为:cv2.createCLAHE()

def clahe_equalized(imgs): # 输入imgs的形状必须是3维 assert (len(imgs.shape) == 3) # 定义均衡化函数 clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # 新数组存放均衡化后的数据 img_res = np.zeros_like(imgs) for i in range(len(imgs)): img_res[i,:,:] = clahe.apply(np.array(imgs[i,:,:], dtype=np.uint8)) return img_res/255 nii格式CT数据读取

一、读取:使用nibabel包进行读写查看等操作 nibabel读取会将图像旋转九十度,也就是各轴的对应关系为[z, x, y]

import nibabel as nb img = nb.load(xxx.nii.gz) #读取nii格式文件 img_affine = img.affine data = img.get_data()

存储nii格式文件,保存时要联通affine矩阵一起保存

import nibabel as nb nb.Nifti1Image(data,img_affine).to_filename(xxx.nii.gz)

查看

from nibabel.viewers import OrthoSlicer3D OrthoSlicer3D(data.transpose(1,2,0)).show()

重采样 CT图像的重采样视为了使体数据中大小不同的体素变得大小相同。由于CT不同轴的体素尺寸、粗细粒度不同,直接使用不利于进行深度模型的训练和推理 重采样步骤: 1、获取先从nii文件的头文件中获取各轴单位体素对应的空间距离

header = img.header print(header)

打印出来可见如下信息:

print(header) object, endian='


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有