python读取Dicom文件

发布时间:2024年01月17日


下面提供几种用python方法读取Dicom文件

1. pydicom Library

import pydicom
# Read DICOM file
dataset = pydicom.dcmread("path_to_dicom_file.dcm")

# Access metadata and pixel data
patient_name = dataset.PatientName
pixel_array = dataset.pixel_array

2. SimpleITK Library

import SimpleITK as sitk

# Read DICOM file
image = sitk.ReadImage("path_to_dicom_file.dcm")

# Access metadata and pixel data
spacing = image.GetSpacing()
pixel_array = sitk.GetArrayFromImage(image)

3. ITK Library (Insight Toolkit)

import itk

# Read DICOM series
series_reader = itk.ImageSeriesReader.New()
series_reader.SetFileNames("path_to_dicom_series/*.dcm")
series_reader.Update()

# Access metadata and pixel data
image = series_reader.GetOutput()
spacing = image.GetSpacing()
pixel_array = itk.GetArrayViewFromImage(image)

4. GDCM Library (Grassroots DICOM)

import gdcm

# Read DICOM file
file_reader = gdcm.ImageReader()
file_reader.SetFileName("path_to_dicom_file.dcm")
file_reader.Read()

# Access metadata and pixel data
dataset = file_reader.GetImage()
pixel_array = dataset.GetBuffer()
import os
import SimpleITK as sitk
import numpy as np

# Specify the folder containing DICOM files
folder_path = "path_to_folder_containing_dicom_files"

# Get the list of DICOM files in the folder
dicom_files = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith('.dcm')]

# Read the DICOM series
reader = sitk.ImageSeriesReader()
reader.SetFileNames(dicom_files)
image = reader.Execute()

# Convert the 3D image to a NumPy array
volume = sitk.GetArrayFromImage(image)

# Access metadata (same for all DICOM files in the folder)
first_file = dicom_files[0]
first_dataset = sitk.ReadImage(first_file)
patient_name = first_dataset.GetMetaData("PatientName")
文章来源:https://blog.csdn.net/weixin_42046845/article/details/135645945
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。