DICOM数据操作指南

DICOM数据

大家只要接触过医疗影像,那么对于DICOM数据就不会陌生。DICOM数据是常用的医疗影像存储格式,比如CT,X光。

DICOM数据操作的环境

语言:Python
库:pydicom

PyDICOM础数据格式介绍

  1. Dataset

dataset.Dataset is the main object you will work with directly. Dataset is derived from Python’s dict, so it inherits (and overrides some of) the methods of dict. In other words, it is a collection of key:value pairs, where the key is the DICOM (group,element) tag (as a Tag object, described below), and the value is a DataElement instance (also described below).

  1. DataElement

The dataelem.DataElement class is not usually used directly in user code, but is used extensively by dataset.Dataset. dataelem.DataElement is a simple object which stores the following things:

  • tag – a DICOM tag (as a Tag object)
  • VR – DICOM value representation – various number and string formats, etc
  • VM – value multiplicity. This is 1 for most DICOM tags, but can be multiple, e.g. for coordinates. You do not have to specify this, the DataElement class keeps track of it based on value.
  • value – the actual value. A regular value like a number or string (or list of them), or a Sequence.
  1. Tag

The Tag class is derived from Python’s int, so in effect, it is just a number with some extra behaviour:

  • Tag enforces that the DICOM tag fits in the expected 4-byte (group,element)
  • A Tag instance can be created from an int or from a tuple containing the (group,element) separately:
>>> from pydicom.tag import Tag
>>> t1=Tag(0x00100010) # all of these are equivalent
>>> t2=Tag(0x10,0x10)
>>> t3=Tag((0x10, 0x10))
>>> t1
(0010, 0010)
>>> t1==t2, t1==t3
(True, True)
  • Tag has properties group and element (or elem) to return the group and element portions
  • The is_private property checks whether the tag represents a private tag (i.e. if group number is odd).
  1. Sequence

Sequence is derived from Python’s list. The only added functionality is to make string representations prettier. Otherwise all the usual methods of list like item selection, append, etc. are available.

基础数据操作

读取dicom数据

import pydicom
meta_data = pydicom.dcmread("dicom文件路径")

由于pydicom库将读取的dicom数据转换为类对象。所以一些常用的dicom meat信息可以直接使用工厂方法读取其属性。下面介绍一部分很常用的dicom属性

#使用dir方法可以查看meta_data类中的相关属性
print(dir(meta_data))
########### 结果 ##########
['AccessionNumber', 'AcquisitionDate', 'AcquisitionNumber', 
'AcquisitionTime', 'BitsAllocated', 'BitsStored', 'Columns', 
'ContentDate', 'ContentTime', 'ContrastBolusAgent', 
'ContrastBolusRoute', 'ContributingEquipmentSequence', 
'ConvolutionKernel', 'DataCollectionDiameter', 'ExposureTime',
 'FrameOfReferenceUID', 'GantryDetectorTilt', 'HighBit', 
'ImageOrientationPatient', 'ImagePositionPatient', 'ImageType', 
'InstanceCreationDate', 'InstanceCreationTime', 'InstanceNumber', 
'InstitutionAddress', 'InstitutionName', 'KVP', 'Manufacturer', 
'ManufacturerModelName', 'Modality', 'OperatorsName', 'PatientAge', 
'PatientBirthDate', 'PatientID', 'PatientName', 'PatientPosition', 
'PatientSex', 'PatientWeight', 'PhotometricInterpretation', 'PixelData',
 'PixelRepresentation', 'PixelSpacing', 'PositionReferenceIndicator', 
'ReconstructionDiameter', 'ReferringPhysicianName', 
'RelatedSeriesSequence', 'RescaleIntercept', 'RescaleSlope', 'Rows', 
'SOPClassUID', 'SOPInstanceUID', 'SamplesPerPixel', 'ScanOptions', 
'SeriesDate', 'SeriesDescription', 'SeriesInstanceUID', 'SeriesNumber', 
'SeriesTime', 'SliceLocation', 'SliceThickness', 'SpecificCharacterSet', 
'StationName', 'StudyDate', 'StudyDescription', 'StudyID', 
'StudyInstanceUID', 'StudyTime', 'TemporalPositionIndex', 
'WindowCenter', 'WindowWidth', 'XRayTubeCurrent', '__contains__', 
'__delattr__', '__delitem__', '__dir__', '__enter__', '__eq__', '__exit__', 
'__format__', '__getattr__', '__getattribute__', '__getitem__', '__init__', 
'__iter__', '__len__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',
 '__str__', '__subclasshook__', '__weakref__', '_character_set', 
'_dataset_slice', '_pretty_str', '_slice_dataset', 'add', 'add_new', 'clear', 
'convert_pixel_data', 'data_element', 'decode', 'decompress', 'dir', 
'elements', 'ensure_file_meta', 'fix_meta_info', 'formatted_lines', 'get', 
'get_item', 'group_dataset', 'is_original_encoding', 'iterall', 'keys', 
'pixel_array', 'pop', 'popitem', 'remove_private_tags', 'save_as', 
'set_original_encoding', 'setdefault', 'top', 'trait_names', 'update', 
'values', 'walk']

常用dicom属性方法

  • PixelData - 存储了dicom中图像信息(原始二进制文件)
  • PixelSpacing - 每个像素点实际的长度与宽度,单位(mm)
  • SliceThickness - 每层切片的厚度,单位(mm)
  • SliceLocation - 读取的dicom文件所在的Z轴位置。
    PS:如果是一个case的文件夹,可以通过这个meta信息对该case的切片进行排序。
  • Rows - 该dicom数据的长度
  • Cols - 该dicom数据的宽度

得到dicom的图像

# 原始二进制文件
pixel_bytes = meta_data.PixelData
# pixel_bytes 通常没法直接查看
 
# CT值组成了一个矩阵
pix = meta_data.pixel_array

# 查看dicom的图像
import numpy as np
import matplotlib.pyplot as plt
# 如果使用jupyter notebook需要加上下面这句
%matplotlib inline
plt.figure()
plt.imshow(pix)
plt.show()

修改dicom CT值矩阵

# 对pix相关操作.PS:此时pix的类型是numpy.array只要使用相关numpy的操作来操作pix矩阵就行
# 接下来要将操作完的结果保存回去
# 此时需要修改的是dicom数据中的原始二进制数据
meta_data.PixelData = pix.tobytes()
meta_data.Rows, meta_data.Columns = pix.shape

修改meta数据中的其他属性

meta_data.add(data_element格式)
delattr(meta_data, '属性值')

del meta_data.属性值
meta_data.属性值 = 新的值

meta_data[字典key] = data_element格式的数据

pydicom文档官网

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355