pydicom数据的处理

p y d i c o m 数 据 的 处 理 pydicom数据的处理 pydicom

读取Dicom文件后,可以借助Numpy以及图像处理库(如PIL.Image)来进行简单的处理

读取图像

folder_path = r"D:\project"
file_name = "IM62.dcm"
file_path = os.path.join(folder_path,file_name)
dcm = pydicom.dcmread(file_path)

获取像素矩阵

dcm = pydicom.dcmread(file_path)
img_arr = dcm.pixel_array
print(type(img_arr))

在这里插入图片描述

获取像素矩阵的shape

print("shape",img_arr.shape)

在这里插入图片描述

获取像素矩阵的dtype(uint16)

print("dtype",img_arr.dtype)

在这里插入图片描述

查看所有值

flatten_data = img_arr.flatten()
print("flatten_data", flatten_data)
print(len(flatten_data))
list_data = flatten_data.tolist()
print("list_data", list_data)
set_data = set(list_data)
print("set_a",set_data)

在这里插入图片描述

借助PIL.Image可以直接进行简单的图像处理

from PIL import Image
data_img = Image.fromarray(ds.pixel_array)
print(type(data_img))
print(data_img)

在这里插入图片描述

Image转ndarray,查看dtype

img_convert_ndarray = np.array(data_img)
print(img_convert_ndarray.dtype)

在这里插入图片描述

查看所有值(没有变化)

flatten_data = img_convert_ndarray.flatten()
print("flatten_data", flatten_data)
print(len(flatten_data))
list_data = flatten_data.tolist()
print("list_data", list_data)
set_data = set(list_data)
print("set_a",set_data)

在这里插入图片描述

借助PIL.Image选择45°,data_img_rotated的像素值不会受到影响

data_img.show()
data_img_rotated = data_img.rotate(angle=45)
data_img_rotated.show()

经过PIL.IMAGE处理后的dicom图像转为ndarray后,可以重新保存为dicom文件

data_rotated = np.array(data_img_rotated)
dcm.PixelData = data_rotated.tobytes()
dcm.Rows,dcm.Columns = data_rotated.shape
new_name = "dicom_rotated.dcm"
dcm.save_as(os.path.join(".",new_name))

在这里插入图片描述

dcm_rotated = pydicom.dcmread(os.path.join(".",new_name))
dicom_rotated_img = Image.fromarray(dcm_rotated.pixel_array)
dicom_rotated_img.show()

在这里插入图片描述

可视化

方式一(pyplot)

from matplotlib import pyplot
pyplot.imshow(ds.pixel_array,cmap=pyplot.cm.bone)
pyplot.show()

在这里插入图片描述

方式二(PIL.Image)

from PIL import Image
data_img = Image.fromarray(dcm.pixel_array)
data_img.show()

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值