python 将dicom图片转换成jpg图片的实例

作者:GoHowz 时间:2023-08-24 11:12:27 

主要原理:调整dicom的窗宽,使之各个像素点上的灰度值缩放至[0,255]范围内。

使用到的python库:SimpleITK

下面是一个将dicom(.dcm)图片转换成jpg图片的demo:


import SimpleITK as sitk
import numpy as np
import cv2

def convert_from_dicom_to_jpg(img,low_window,high_window,save_path):
 lungwin = np.array([low_window*1.,high_window*1.])
 newimg = (img-lungwin[0])/(lungwin[1]-lungwin[0])  #归一化
 newimg = (newimg*255).astype('uint8')        #将像素值扩展到[0,255]
 cv2.imwrite(save_path, newimg, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

if __name__ == '__main__':

# 下面是将对应的dicom格式的图片转成jpg
 dcm_image_path = '/DICOM_image/lung001.dcm'    #读取dicom文件
 output_jpg_path = 'JPG_image/lung001.jpg'
 ds_array = sitk.ReadImage(dcm_image_path)     #读取dicom文件的相关信息
 img_array = sitk.GetArrayFromImage(ds_array)   #获取array
 # SimpleITK读取的图像数据的坐标顺序为zyx,即从多少张切片到单张切片的宽和高,此处我们读取单张,因此img_array的shape
 #类似于 (1,height,width)的形式
 shape = img_array.shape
 img_array = np.reshape(img_array, (shape[1], shape[2])) #获取array中的height和width
 high = np.max(img_array)
 low = np.min(img_array)
 convert_from_dicom_to_jpg(img_array, low, high, output_jpg_path)  #调用函数,转换成jpg文件并保存到对应的路径
 print('FINISHED')

来源:https://blog.csdn.net/IT_forlearn/article/details/81046417

标签:python,dicom,转换,jpg
0
投稿

猜你喜欢

  • python定时执行指定函数的方法

    2021-04-09 03:20:14
  • 有关数据库SQL递归查询在不同数据库中的实现方法

    2024-01-28 00:40:15
  • python实现bucket排序算法实例分析

    2023-09-17 17:54:07
  • 关于Tensorflow 模型持久化详解

    2021-02-26 14:27:33
  • Django REST Framework之频率限制的使用

    2021-01-25 17:59:42
  • 实现 Python 脚本生成命令行

    2022-08-26 01:05:56
  • Python最基本的输入输出详解

    2023-11-27 16:49:24
  • JS简单模拟触发按钮点击功能的方法

    2024-04-22 13:02:52
  • DreamWeaver批处理提高篇

    2007-12-03 11:34:00
  • pandas 把数据写入txt文件每行固定写入一定数量的值方法

    2021-06-13 20:08:14
  • 如何通过python实现全排列

    2022-11-30 16:49:46
  • python中plt.imshow与cv2.imshow显示颜色问题

    2023-12-13 10:47:54
  • python使用正则表达式匹配字符串开头并打印示例

    2021-07-02 00:52:13
  • MySQL主从复制与读写分离原理及用法详解

    2024-01-14 21:16:25
  • JS创建对象的写法示例

    2024-04-16 08:54:48
  • xheditor所见即所得文本编辑器(代码高亮显示修改)

    2022-04-17 02:20:25
  • t-sql/mssql用命令行导入数据脚本的SQL语句示例

    2024-01-21 16:15:42
  • 关于Python网络爬虫框架scrapy

    2023-03-17 17:02:50
  • SpringBoot Logback日志记录到数据库的实现方法

    2024-01-16 11:58:11
  • 如何利用python实现列表嵌套字典取值

    2023-07-08 02:16:50
  • asp之家 网络编程 m.aspxhome.com