PythonでDICOM画像を読み込んで白黒反転まで

DICOM(ダイコム)は医療用画像の規格の1つである。

DICOM画像を扱う必要が出てきたので、Pythonで処理できないか調べてみると、Pydicomというパッケージが見つかったので使ってみる。

Pydicom |
Pydicom Dicom (Digital Imaging in Medicine) is the bread and butter of medical image datasets, storage and transfer. Thi...

インストールはpipでOK。

$ pip install pydicom

以下に参考プログラムが載っているので、jupyter notebookで試してみる。

Page not found · GitHub Pages

In [1]:

import matplotlib.pyplot as plt
import pydicom
from pydicom.data import get_testdata_files
filename = get_testdata_files("CT_small.dcm")[0]
ds = pydicom.dcmread(filename)

In [2]:

ds

Out [2]:

(0008, 0005) Specific Character Set              CS: 'ISO_IR 100'
(0008, 0008) Image Type                          CS: ['ORIGINAL', 'PRIMARY', 'AXIAL']
(0008, 0012) Instance Creation Date              DA: '20040119'
(0008, 0013) Instance Creation Time              TM: '072731'
(0008, 0014) Instance Creator UID                UI: 1.3.6.1.4.1.5962.3
(0008, 0016) SOP Class UID                       UI: CT Image Storage
(0008, 0018) SOP Instance UID                    UI: 1.3.6.1.4.1.5962.1.1.1.1.1.20040119072730.12322
(0008, 0020) Study Date                          DA: '20040119'
(0008, 0021) Series Date                         DA: '19970430'
(0008, 0022) Acquisition Date                    DA: '19970430'
(0008, 0023) Content Date                        DA: '19970430'
(0008, 0030) Study Time                          TM: '072730'
(0008, 0031) Series Time                         TM: '112749'
(0008, 0032) Acquisition Time                    TM: '112936'
(0008, 0033) Content Time                        TM: '113008'
(0008, 0050) Accession Number                    SH: ''
(0008, 0060) Modality                            CS: 'CT'
(0008, 0070) Manufacturer                        LO: 'GE MEDICAL SYSTEMS'
(0008, 0080) Institution Name                    LO: 'JFK IMAGING CENTER'
(0008, 0090) Referring Physician's Name          PN: ''
(0008, 0201) Timezone Offset From UTC            SH: '-0500'
(0008, 1010) Station Name                        SH: 'CT01_OC0'
(0008, 1030) Study Description                   LO: 'e+1'
(0008, 1090) Manufacturer's Model Name           LO: 'RHAPSODE'
(0009, 0010) Private Creator                     LO: 'GEMS_IDEN_01'
(0009, 1001) [Full fidelity]                     LO: 'GE_GENESIS_FF'
(0009, 1002) [Suite id]                          SH: 'CT01'
(0009, 1004) [Product id]                        SH: 'HiSpeed CT/i'
(0009, 1027) [Image actual date]                 SL: 862399669
(0009, 1030) [Service id]                        SH: ''
(0009, 1031) [Mobile location number]            SH: ''
(0009, 10e6) [Genesis Version - now]             SH: '05'
(0009, 10e7) [Exam Record checksum]              UL: 973283917
(0009, 10e9) [Actual series data time stamp]     SL: 862399669
(0010, 0010) Patient's Name                      PN: 'CompressedSamples^CT1'
(0010, 0020) Patient ID                          LO: '1CT1'
(0010, 0030) Patient's Birth Date                DA: ''
(0010, 0040) Patient's Sex                       CS: 'O'
(0010, 1002)  Other Patient IDs Sequence   2 item(s) ---- 
   (0010, 0020) Patient ID                          LO: 'ABCD1234'
   (0010, 0022) Type of Patient ID                  CS: 'TEXT'
   ---------
   (0010, 0020) Patient ID                          LO: '1234ABCD'
   (0010, 0022) Type of Patient ID                  CS: 'TEXT'
   ---------
(0010, 1010) Patient's Age                       AS: '000Y'
(0010, 1030) Patient's Weight                    DS: "0.0"
(0010, 21b0) Additional Patient History          LT: ''
(0011, 0010) Private Creator                     LO: 'GEMS_PATI_01'
(0011, 1010) [Patient Status]                    SS: 0
(0018, 0010) Contrast/Bolus Agent                LO: 'ISOVUE300/100'
(0018, 0022) Scan Options                        CS: 'HELICAL MODE'
(0018, 0050) Slice Thickness                     DS: "5.0"
(0018, 0060) KVP                                 DS: "120.0"
(0018, 0088) Spacing Between Slices              DS: "5.0"
(0018, 0090) Data Collection Diameter            DS: "480.0"
(0018, 1020) Software Versions                   LO: '05'
(0018, 1040) Contrast/Bolus Route                LO: 'IV'
(0018, 1100) Reconstruction Diameter             DS: "338.6716"
(0018, 1110) Distance Source to Detector         DS: "1099.3100585938"
(0018, 1111) Distance Source to Patient          DS: "630.0"
(0018, 1120) Gantry/Detector Tilt                DS: "0.0"
(0018, 1130) Table Height                        DS: "133.699997"
(0018, 1150) Exposure Time                       IS: "1601"
(0018, 1151) X-Ray Tube Current                  IS: "170"
(0018, 1152) Exposure                            IS: "170"
(0018, 1160) Filter Type                         SH: 'LARGE BOWTIE FIL'
(0018, 1190) Focal Spot(s)                       DS: "0.7"
(0018, 1210) Convolution Kernel                  SH: 'STANDARD'
(0018, 5100) Patient Position                    CS: 'FFS'
(0019, 0010) Private Creator                     LO: 'GEMS_ACQU_01'
(0019, 1002) [Detector Channel]                  SL: 912
(0019, 1003) [Cell number at Theta]              DS: "373.75"
(0019, 1004) [Cell spacing]                      DS: "1.0166"
(0019, 100f) [Horiz. Frame of ref.]              DS: "955.799988"
(0019, 1011) [Series contrast]                   SS: 2
(0019, 1013) [Start number for baseline]         SS: 0
(0019, 1014) [End number for baseline]           SS: 0
(0019, 1015) [Start number for enhanced scans]   SS: 0
(0019, 1016) [End number for enhanced scans]     SS: 0
(0019, 1017) [Series plane]                      SS: 2
(0019, 1018) [First scan ras]                    LO: 'S'
(0019, 1019) [First scan location]               DS: "7.79187"
(0019, 101a) [Last scan ras]                     LO: 'I'
(0019, 101b) [Last scan loc]                     DS: "-320.197968"
(0019, 101e) [Display field of view]             DS: "0.0"
(0019, 1023) [Table Speed [mm/rotation]]         DS: "5.0"
(0019, 1024) [Mid Scan Time [sec]]               DS: "17.784578"
(0019, 1025) [Mid scan flag]                     SS: 1
(0019, 1026) [Tube Azimuth [degree]]             SL: 0
(0019, 1027) [Rotation Speed [msec]]             DS: "1.0"
(0019, 102a) [x-ray On position]                 DS: "178.079926"
(0019, 102b) [x-ray Off position]                DS: "3994.299316"
(0019, 102c) [Number of triggers]                SL: 10431
(0019, 102e) [Angle of first view]               DS: "-718.079956"
(0019, 102f) [Trigger frequency]                 DS: "984.0"
(0019, 1039) [SFOV Type]                         SS: 16
(0019, 1040) [Stat recon flag]                   SS: 0
(0019, 1041) [Compute type]                      SS: 1
(0019, 1042) [Segment Number]                    SS: 0
(0019, 1043) [Total Segments Required]           SS: 0
(0019, 1044) [Interscan delay]                   DS: "1.0"
(0019, 1047) [View compression factor]           SS: 1
(0019, 104a) [Total no. of ref channels]         SS: 6
(0019, 104b) [Data size for scan data]           SL: 10431
(0019, 1052) [Recon post proc. Flag]             SS: 1
(0019, 1057) [CT water number]                   SS: -95
(0019, 1058) [CT bone number]                    SS: 0
(0019, 105e) [Number of channels (1...512)]      SL: 763
(0019, 105f) [Increment between channels]        SL: 1
(0019, 1060) [Starting view]                     SL: 1969
(0019, 1061) [Number of views]                   SL: 1576
(0019, 1062) [Increment between views]           SL: 1
(0019, 106a) [Dependent on #views processed]     SS: 4
(0019, 106b) [Field of view in detector cells]   SS: 852
(0019, 1070) [Value of back projection button]   SS: 1
(0019, 1071) [Set if fatq estimates were used]   SS: 0
(0019, 1072) [Z chan avg over views]             DS: "0.0"
(0019, 1073) [Avg of left ref chans over views]  DS: "0.0"
(0019, 1074) [Max left chan over views]          DS: "0.0"
(0019, 1075) [Avg of right ref chans over views] DS: "0.0"
(0019, 1076) [Max right chan over views]         DS: "0.0"
(0019, 10da) [Reference channel used]            SS: 0
(0019, 10db) [Back projector coefficient]        DS: "0.0"
(0019, 10dc) [Primary speed correction used]     SS: 1
(0019, 10dd) [Overrange correction used]         SS: 1
(0019, 10de) [Dynamic Z alpha value]             DS: "0.0"
(0020, 000d) Study Instance UID                  UI: 1.3.6.1.4.1.5962.1.2.1.20040119072730.12322
(0020, 000e) Series Instance UID                 UI: 1.3.6.1.4.1.5962.1.3.1.1.20040119072730.12322
(0020, 0010) Study ID                            SH: '1CT1'
(0020, 0011) Series Number                       IS: "1"
(0020, 0012) Acquisition Number                  IS: "2"
(0020, 0013) Instance Number                     IS: "1"
(0020, 0032) Image Position (Patient)            DS: [-158.135803, -179.035797, -75.699997]
(0020, 0037) Image Orientation (Patient)         DS: [1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000]
(0020, 0052) Frame of Reference UID              UI: 1.3.6.1.4.1.5962.1.4.1.1.20040119072730.12322
(0020, 0060) Laterality                          CS: ''
(0020, 1040) Position Reference Indicator        LO: 'SN'
(0020, 1041) Slice Location                      DS: "-77.2040634155"
(0020, 4000) Image Comments                      LT: 'Uncompressed'
(0021, 0010) Private Creator                     LO: 'GEMS_RELA_01'
(0021, 1003) [Series from which Prescribed]      SS: 2
(0021, 1005) [Genesis Version - now]             SH: '05'
(0021, 1007) [Series Record checksum]            UL: 1605775145
(0021, 1015) [Unknown]                           US: 24078
(0021, 1016) [Unknown]                           SS: 2
(0021, 1018) [Genesis version - Now]             SH: '05'
(0021, 1019) [Acq recon record checksum]         UL: 750675506
(0021, 1037) [Screen Format]                     SS: 16
(0021, 104a) [Anatomical reference for scout]    LO: ''
(0021, 1090) [Tube focal spot position]          SS: 7400
(0021, 1091) [Biopsy position]                   SS: 0
(0021, 1092) [Biopsy T location]                 FL: 0.0
(0021, 1093) [Biopsy ref location]               FL: 0.0
(0023, 0010) Private Creator                     LO: 'GEMS_STDY_01'
(0023, 1070) [Start time(secs) in first axial]   FD: 862399761.111079
(0023, 1074) [No. of updates to header]          SL: 1
(0023, 107d) [Indicates study has complete info  SS: 0
(0025, 0010) Private Creator                     LO: 'GEMS_SERS_01'
(0025, 1006) [Last pulse sequence used]          SS: 0
(0025, 1007) [Images in Series]                  SL: 44
(0025, 1010) [Landmark Counter]                  SL: 0
(0025, 1011) [Number of Acquisitions]            SS: 0
(0025, 1017) [Series Complete Flag]              SL: 0
(0025, 1018) [Number of images archived]         SL: 0
(0025, 1019) [Last image number used]            SL: 4
(0025, 101a) [Primary Receiver Suite and Host]   SH: ''
(0027, 0010) Private Creator                     LO: 'GEMS_IMAG_01'
(0027, 1006) [Image archive flag]                SL: 1
(0027, 1010) [Scout Type]                        SS: 0
(0027, 101c) [Vma mamp]                          SL: 150
(0027, 101d) [Vma phase]                         SS: 1
(0027, 101e) [Vma mod]                           SL: 24
(0027, 101f) [Vma clip]                          SL: 129
(0027, 1020) [Smart scan ON/OFF flag]            SS: 1
(0027, 1030) [Foreign Image Revision]            SH: ''
(0027, 1035) [Plane Type]                        SS: 2
(0027, 1040) [RAS letter of image location]      SH: 'I'
(0027, 1041) [Image location]                    FL: -77.20406341552734
(0027, 1042) [Center R coord of plane image]     FL: -11.199999809265137
(0027, 1043) [Center A coord of plane image]     FL: 9.699999809265137
(0027, 1044) [Center S coord of plane image]     FL: -75.69999694824219
(0027, 1045) [Normal R coord]                    FL: 0.0
(0027, 1046) [Normal A coord]                    FL: 0.0
(0027, 1047) [Normal S coord]                    FL: -1.0
(0027, 1048) [R Coord of Top Right Corner]       FL: -180.53579711914062
(0027, 1049) [A Coord of Top Right Corner]       FL: 179.03579711914062
(0027, 104a) [S Coord of Top Right Corner]       FL: -75.69999694824219
(0027, 104b) [R Coord of Bottom Right Corner]    FL: -180.53579711914062
(0027, 104c) [A Coord of Bottom Right Corner]    FL: -159.63580322265625
(0027, 104d) [S Coord of Bottom Right Corner]    FL: -75.69999694824219
(0027, 1050) [Scan Start Location]               FL: -63.19999694824219
(0027, 1051) [Scan End Location]                 FL: -116.20304870605469
(0027, 1052) [RAS letter for side of image]      SH: 'L'
(0027, 1053) [RAS letter for anterior/posterior] SH: 'A'
(0027, 1054) [RAS letter for scout start loc]    SH: 'I'
(0027, 1055) [RAS letter for scout end loc]      SH: 'I'
(0028, 0002) Samples per Pixel                   US: 1
(0028, 0004) Photometric Interpretation          CS: 'MONOCHROME2'
(0028, 0010) Rows                                US: 128
(0028, 0011) Columns                             US: 128
(0028, 0030) Pixel Spacing                       DS: [0.661468, 0.661468]
(0028, 0100) Bits Allocated                      US: 16
(0028, 0101) Bits Stored                         US: 16
(0028, 0102) High Bit                            US: 15
(0028, 0103) Pixel Representation                US: 1
(0028, 0120) Pixel Padding Value                 SS: -2000
(0028, 1052) Rescale Intercept                   DS: "-1024.0"
(0028, 1053) Rescale Slope                       DS: "1.0"
(0029, 0010) Private Creator                     LO: 'GEMS_IMPS_01'
(0029, 1004) [Lower range of Pixels1]            SL: 0
(0029, 1005) [Lower range of Pixels1]            DS: "0.0"
(0029, 1006) [Lower range of Pixels1]            DS: "0.0"
(0029, 1007) [Lower range of Pixels1]            SL: 87
(0029, 1008) [Lower range of Pixels1]            SH: ''
(0029, 1009) [Lower range of Pixels1]            SH: ''
(0029, 100a) [Lower range of Pixels1]            SS: 764
(0029, 1026) [Version of the hdr struct]         SS: 2
(0029, 1034) [Advantage comp. Overflow]          SL: 0
(0029, 1035) [Advantage comp. Underflow]         SL: 0
(0043, 0010) Private Creator                     LO: 'GEMS_PARM_01'
(0043, 1010) [Window value]                      US: 400
(0043, 1011) [Total input views]                 US: 10431
(0043, 1012) [X-ray chain]                       SS: [14, 2, 3]
(0043, 1013) [Decon kernel parameters]           SS: [107, 21, 4, 2, 20]
(0043, 1014) [Calibration parameters]            SS: [4, 4, 5]
(0043, 1015) [Total output views]                SS: 10431
(0043, 1016) [Number of overranges]              SS: 0
(0043, 1017) [IBH image scale factors]           DS: "0.095"
(0043, 1018) [BBH coefficients]                  DS: [0.085000, 1.102000, 0.095000]
(0043, 1019) [Number of BBH chains to blend]     SS: 350
(0043, 101a) [Starting channel number]           SL: 7
(0043, 101b) [Ppscan parameters]                 SS: [0, 0, 0, 0, 0]
(0043, 101c) [GE image integrity]                SS: 0
(0043, 101d) [Level value]                       SS: 40
(0043, 101e) [Delta Start Time [msec]]           DS: "2.0"
(0043, 101f) [Max overranges in a view]          SL: 0
(0043, 1020) [Avg overranges all views]          DS: "0.0"
(0043, 1021) [Corrected after glow terms]        SS: 0
(0043, 1025) [Reference channels]                SS: [1, 2, 3, 748, 749, 750]
(0043, 1026) [No views ref chans blocked]        US: [0, 1, 1, 0, 0, 0]
(0043, 1027) [Scan Pitch Ratio]                  SH: '/1.0:1'
(0043, 1028) [Unique image iden]                 OB: Array of 80 elements
(0043, 1029) [Histogram tables]                  OB: Array of 2068 elements
(0043, 102a) [User defined data]                 OB: Array of 40 elements
(0043, 102b) [Private Scan Options]              SS: [4, 4, 0, 0]
(0043, 1031) [Recon Center Coordinates]          DS: [-11.200000, 9.700000]
(0043, 1040) [Trigger on position]               FL: 178.07992553710938
(0043, 1041) [Degree of rotation]                FL: 3816.219482421875
(0043, 1042) [DAS trigger source]                SL: 0
(0043, 1043) [DAS fpa gain]                      SL: 0
(0043, 1044) [DAS output source]                 SL: 1
(0043, 1045) [DAS ad input]                      SL: 1
(0043, 1046) [DAS cal mode]                      SL: 3
(0043, 1047) [DAS cal frequency]                 SL: -1
(0043, 1048) [DAS reg xm]                        SL: 1
(0043, 1049) [DAS auto zero]                     SL: 0
(0043, 104a) [Starting channel of view]          SS: 1
(0043, 104b) [DAS xm pattern]                    SL: 0
(0043, 104c) [TGGC trigger mode]                 SS: 0
(0043, 104d) [Start scan to X-ray on delay]      FL: 0.0
(0043, 104e) [Duration of X-ray on]              FL: 10.60060977935791
(7fe0, 0010) Pixel Data                          OW: Array of 32768 elements
(fffc, fffc) Data Set Trailing Padding           OB: Array of 126 elements

たくさんの属性がDICOMに入っているのがわかる。

例えば

In [3]:

ds.PatientID

Out [3]:

'1CT1'

In [4]:

ds.PatientAge

Out [4]:

'000Y'

というように属性を指定すると、その値を得ることが出来る。

患者IDや検査結果、性別年齢などでif文を書いたりする際に役立つ。

次に、画像データを確認する。

In [5]:

ds.pixel_array

Out [5]:

array([[175, 180, 166, ..., 203, 207, 216],
       [186, 183, 157, ..., 181, 190, 239],
       [184, 180, 171, ..., 152, 164, 235],
       ...,
       [906, 910, 923, ..., 922, 929, 927],
       [914, 954, 938, ..., 942, 925, 905],
       [959, 955, 916, ..., 911, 904, 909]], dtype=int16)

In [6]:

type(ds.pixel_array)

Out [6]:

numpy.ndarray

16bitのndarrayで読み込まれている。

In [7]:

plt.imshow(ds.pixel_array, cmap='gray')

Out [7]:

<matplotlib.image.AxesImage at 0x114fbac18>

今回は16bitの画像なので、白黒反転(ネガポジ反転)するには2^16-1=65535から元の値を引けばいい。

In [8]:

plt.imshow(65535 - ds.pixel_array, cmap='gray')

Out [8]:

<matplotlib.image.AxesImage at 0x115059470>

もしくはpylabを使ってもいい。

In [9]:

import pylab
plt.imshow(ds.pixel_array, cmap=pylab.cm.binary)

Out [9]:

<matplotlib.image.AxesImage at 0x1150bbb38>

これで、DICOM画像の表示や白黒反転、属性情報の取得をPythonで出来るようになった。

参考

Pydicom |
Pydicom Dicom (Digital Imaging in Medicine) is the bread and butter of medical image datasets, storage and transfer. Thi...
Page not found · GitHub Pages
Python, NumPyで画像処理(読み込み、演算、保存) | note.nkmk.me
画像ファイルをNumPy配列ndarrayとして読み込むと、NumPyの機能を使って様々な画像処理を行うことができる。 要素(画素)の値の取得や書き換え、スライスでのトリミング、結合などndarrayの操作がそのまま使えるの ...

コメント

タイトルとURLをコピーしました