SimpleITK
ITK是一个开源、跨平台的框架,提供给开发者增强功能的图像分析和处理套件(推荐使用)。
Note:注意SimpleITK不支持中文,即路径中不能有中文
X射线图像对应的读取
1 # @file : itk_p1.py 2 # @Time : 2021/8/28 16:27 3 # @Author : wmz 4 import SimpleITK as sitk 5 filename = '1.dcm' 6 itk_img = sitk.ReadImage(filename) 7 img_array = sitk.GetArrayFromImage(itk_img) 8 print(img_array.shape) 9 10 # 输出:(对应信息:frame_num, width, height)注意:img_array对应的是图像的矩阵信息 11 12 # (1, 512, 512)
推荐用法:
1 # -*- coding : UTF-8 -*- 2 # @file : itk_p2.py 3 # @Time : 2021/8/28 19:43 4 # @Author : wmz 5 6 import SimpleITK as sitk 7 # 推荐用法 8 filename = '1.dcm' 9 itk_img = sitk.ReadImage(filename) 10 img_array = sitk.GetArrayFromImage(itk_img)[0] 11 print(img_array.shape)
CT图像的读取
1 import SimpleITK as sitk 2 filename = '*.mhd' 3 itk_img = sitk.ReadImage(filename) 4 img_array = sitk.GetArrayFromImage(itk_img) 5 print(img_array.shape) 6 输出:(对应信息:frame_num, width, height)帧参数:frame_num,代表CT扫描层数注意:img_array对应的是图像的矩阵信息 7 8 (133, 512, 512)
将CT影像拆分成多个单幅图像
方法一:
1 import SimpleITK as sitk 2 import cv2 3 filename = '*.mhd' 4 itk_img = sitk.ReadImage(filename) 5 img_array = sitk.GetArrayFromImage(itk_img) 6 frame_num, width, height = img_array.shape 7 outpath = 'F:/data/LUNA16'#存放拆分得到的图像路径 8 index = -1 9 for img_item in img_array: 10 index = index + 1 11 cv2.imwrite("%s/%d.png" % (outpath, index), img_item) 12 print("done!")
方法二:
1 import SimpleITK as sitk 2 import os 3 import cv2 4 filename = '*.mhd' 5 path = 'F:/data/LUNA16' #存放拆分得到图像的路径 6 itk_img = sitk.ReadImage(filename) 7 img_array = sitk.GetArrayFromImage(itk_img) 8 for i, im in enumerate(img_array): 9 cv2.imwrite(os.path.join(path, '{}.png'.format(i)), im)
如下图所示是CT影像拆分出来的部分图像。拆分得到的图像大小均为512*512,总数是:frame_num张。
标签:sitk,itk,img,简单,filename,SimpleITK,使用,array From: https://www.cnblogs.com/ybqjymy/p/17549955.html