import os
import pydicom
import numpy as np
import matplotlib.pyplot as plt

# 打开DICOM文件
filename = 'C:\Users\lenovo\Desktop\数据结构与算法C++\20084125-张亭-数据结构算法实验1\dicom\Dicomread\11.dcm'
ds = pydicom.dcmread(filename)

# 判断文件是否为DICOM格式
if ds.file_meta.MediaStorageSOPClassUID != '1.2.840.10008.5.1.4.1.1.2':
    print('Not a DICOM file!')
    exit()

# 获取基本信息
file_name = os.path.basename(filename)
file_path = os.path.dirname(filename)
file_size = os.path.getsize(filename)  #文件大小

# 获取患者信息
patient_id = ds.PatientID
patient_name = ds.PatientName
patient_sex = ds.PatientSex
#patient_age = ds.PatientAge

# 获取检查信息
study_id = ds.StudyID  #检查ID
study_date = ds.StudyDate  #检查日期
study_time = ds.StudyTime   #检查时间
study_type = ds.Modality    #检查类型

# 获取图像信息
pixel_data = ds.pixel_array   #像素数据
pixel_offset = ds.data_element('PixelData').value[0]  #像素数据偏移量
rows = ds.Rows  #图像行数
columns = ds.Columns  #图像列数
pixel_size = (ds.PixelSpacing[0], ds.PixelSpacing[1])  #像素尺寸
pixel_spacing = (ds.SliceThickness, ds.PixelSpacing[0], ds.PixelSpacing[1])  #像素间距
image_position = ds.ImagePositionPatient    #图像位置
image_orientation = ds.ImageOrientationPatient  #图像方向

# 获取窗位窗宽
window_center = ds.WindowCenter
window_width = ds.WindowWidth

# 输出结果
print('File name: %s' % file_name)
print('File path: %s' % file_path)
print('File size: %s bytes' % file_size)
print('Patient ID: %s' % patient_id)
print('Patient name: %s' % patient_name)
print('Patient sex: %s' % patient_sex)
# print('Patient age: %s' % patient_age)
print('Study ID: %s' % study_id)
print('Study date: %s' % study_date)
print('Study time: %s' % study_time)
print('Study type: %s' % study_type)
print('Pixel data: %s' % pixel_data)
print('Pixel data offset: %s bytes' % pixel_offset)
print('Rows: %d' % rows)
print('Columns: %d' % columns)
print('Pixel size: %s' % str(pixel_size))
print('Pixel spacing: %s' % str(pixel_spacing))
print('Image position: %s' % str(image_position))
print('Image orientation: %s' % str(image_orientation))
print('Window center: %s' % str(window_center))
print('Window width: %s' % str(window_width))

# 获取序列信息
sequence_number = ds.InstanceNumber     #序列号
sequence_description = ds.SeriesDescription   #序列描述
sequence_type = ds.Modality      #序列类型
sequence_uid = ds.SeriesInstanceUID    #序列标识

# 输出结果
print('Sequence number: %s' % sequence_number)
print('Sequence description: %s' % sequence_description)
print('Sequence type: %s' % sequence_type)
print('Sequence UID: %s' % sequence_uid)


# 获取图像基本属性
pixel_type = ds.pixel_array.dtype
image_size = ds.pixel_array.shape
channels = 1 if len(image_size) == 2 else image_size[2]

# 输出结果
print('Pixel type: %s' % pixel_type)
print('Image size: %s' % str(image_size))
print('Channels: %d' % channels)

# 窗位窗宽线性变换
slope = ds.RescaleSlope
intercept = ds.RescaleIntercept
window_center = ds.WindowCenter
window_width = ds.WindowWidth

# 将像素值转换为灰度值
pixel_array = ds.pixel_array * slope + intercept

# 将灰度值转换为8位灰度图像或3通道彩色图像
if channels == 1:
    # 8位灰度图像
    pixel_array = np.uint8(pixel_array)
    plt.imshow(pixel_array, cmap='gray')
else:
    # 3通道彩色图像
    pixel_array = np.uint8(pixel_array / np.max(pixel_array) * 255)
    plt.imshow(pixel_array)

plt.show()
Python DICOM 文件信息读取和图像显示

原文地址: https://www.cveoy.top/t/topic/nctE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录