python读取Dicom文件

文章目录

    • 1. pydicom Library
    • 2. SimpleITK Library
    • 3. ITK Library (Insight Toolkit)
    • 4. GDCM Library (Grassroots DICOM)

下面提供几种用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")

你可能感兴趣的:(Dicom,图像处理,python,服务器,开发语言)