当前位置:   article > 正文

python读取Dicom文件_python读取dcm数据

python读取dcm数据


下面提供几种用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/165523?site
推荐阅读
相关标签
  

闽ICP备14008679号