[Python] DICOM파일 읽기

olxtar·2022년 4월 11일
0

0. DICOM이란?

의료용 디지털 영상 및 통신(Digital Imaging and Communications in Medicine, DICOM) 표준은 의료용 기기에서 디지털 영상표현과 통신에 사용되는 여러 가지 표준을 총칭하는 말





1. pydicom 모듈

참고 : pydicom github


...
pydicom is a pure Python package for working with DICOM files. It lets you read, modify and write DICIOM data in an easy "pythonic" way.

As a pure Python package, pydicom can run anywhere Python runs without any other requirements, although if you're working with Pixel Data then we recommend you also install NumPy.
...

# Using pip:
!pip install pydicom

# Using conda:
conda install -c conda-forge pydicom





2. pydicom 기초 사용


  • dicom파일 읽는 법
dcm = pydicom.dcmread(filename)
pirnt(dcm)

>>>
Dataset.file_meta -------------------------------
(0002, 0000) File Meta Information Group Length  UL: 138
(0002, 0001) File Meta Information Version       OB: b'\x00\x01'
(0002, 0002) Media Storage SOP Class UID         UI: Ultrasound Multi-frame Image Storage
(0002, 0003) Media Storage SOP Instance UID      UI: 999.999.133.1996.1.1800.1.6.29
(0002, 0010) Transfer Syntax UID                 UI: RLE Lossless
(0002, 0012) Implementation Class UID            UI: 999.999.332346
-------------------------------------------------
(0008, 0000) Group Length                        UL: 226
(0008, 0008) Image Type                          CS: ''
(0008, 0016) SOP Class UID                       UI: Ultrasound Multi-frame Image Storage
(0008, 0018) SOP Instance UID                    UI: 999.999.133.1996.1.1800.1.6.29
(0008, 0020) Study Date                          DA: '19940323'
(0008, 0030) Study Time                          TM: '115104.0'
(0008, 0050) Accession Number                    SH: ''
(0008, 0060) Modality                            CS: 'US'
(0008, 0070) Manufacturer                        LO: ''
(0008, 0090) Referring Physician's Name          PN: '------------------'
(0008, 1030) Study Description                   LO: 'Echocardiogram'
(0008, 103e) Series Description                  LO: 'IAS FEN2'
(0010, 0000) Group Length                        UL: 70
(0010, 0010) Patient's Name                      PN: 'Rubo DEMO'
(0010, 0020) Patient ID                          LO: '123-45-6789'
(0010, 0030) Patient's Birth Date                DA: '19231016'
(0010, 0040) Patient's Sex                       CS: 'F'
(0018, 0000) Group Length                        UL: 18
(0018, 1063) Frame Time                          DS: '62.727272'
(0020, 0000) Group Length                        UL: 120
(0020, 000d) Study Instance UID                  UI: 999.999.3859744
(0020, 000e) Series Instance UID                 UI: 999.999.94827453
(0020, 0010) Study ID                            SH: '027893462'
(0020, 0011) Series Number                       IS: '5829'
(0020, 0013) Instance Number                     IS: '28'
(0020, 0020) Patient Orientation                 CS: ''
(0020, 4000) Image Comments                      LT: 'IAS FEN2'
(0028, 0000) Group Length                        UL: 1754
(0028, 0002) Samples per Pixel                   US: 1
(0028, 0004) Photometric Interpretation          CS: 'PALETTE COLOR'
(0028, 0008) Number of Frames                    IS: '11'
(0028, 0009) Frame Increment Pointer             AT: (0018, 1063)
(0028, 0010) Rows                                US: 430
(0028, 0011) Columns                             US: 600
(0028, 0100) Bits Allocated                      US: 8
(0028, 0101) Bits Stored                         US: 8
(0028, 0102) High Bit                            US: 7
(0028, 0103) Pixel Representation                US: 0
(0028, 1101) Red Palette Color Lookup Table Desc US: [256, 0, 16]
(0028, 1102) Green Palette Color Lookup Table De US: [256, 0, 16]
(0028, 1103) Blue Palette Color Lookup Table Des US: [256, 0, 16]
(0028, 1199) Palette Color Lookup Table UID      UI: 999.999.389972238
(0028, 1201) Red Palette Color Lookup Table Data OW: Array of 512 elements
(0028, 1202) Green Palette Color Lookup Table Da OW: Array of 512 elements
(0028, 1203) Blue Palette Color Lookup Table Dat OW: Array of 512 elements
(7fe0, 0010) Pixel Data                          OB: Array of 424226 elements

  • 특정 attribute 접근하기
sex = dcm.PatientSex
#print(sex)

>>>
M

  • 이미지 데이터 값 얻기
img = dcm.pixel_array
print(img)
print(img.shape)

>>>
[[[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 ...

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]]
(96, 512, 512)

위의 DICOM 이미지(예시)의 shape은 (96, 512, 512) = (channel, width, height)인데, channel이 의미하는 바는 시간 축이라고 보면됨.

예를 들어, pixel_array[0] -> pixel_array[1] -> ... -> pixel_array[95] 시간 순서대로 (512, 512) gray scale 이미지가 변한다고 보면됨.

즉, pixel_array[n]이 하나의 사진 프레임이라고 생각하면 됨.



  • 이미지 보기
import matplotlib.pyplot as plt

# dcm = pydicom.dcmread('./0002.dcm')
# img = dcm.pixel_array[0]

plt.imshow(img, cmap=plt.cm.bone)

>>>
<matplotlib.image.AxesImage at 0x16836b53ee0>

profile
예술과 기술

0개의 댓글