Downsize MRI image using pydicomΒΆ

This example shows how to downsize an MR image from

System Message: WARNING/2 (512 imes 512)

latex exited with error [stderr] kpathsea: Running mktexfmt latex.fmt mktexfmt: mktexfmt is using the following fmtutil.cnf files (in precedence order): mktexfmt: /usr/share/texlive/texmf-dist/web2c/fmtutil.cnf mktexfmt: mktexfmt is using the following fmtutil.cnf file for writing changes: mktexfmt: /builddir/.texlive2020/texmf-config/web2c/fmtutil.cnf mktexfmt [INFO]: writing formats under /builddir/.texlive2020/texmf-var/web2c mktexfmt [INFO]: --- remaking latex with pdftex mktexfmt: running `pdftex -ini -jobname=latex -progname=latex -translate-file=cp227.tcx *latex.ini' ... This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (INITEX) restricted \write18 enabled. (/usr/share/texlive/texmf-dist/web2c/cp227.tcx) entering extended mode (/usr/share/texlive/texmf-dist/tex/latex/latexconfig/latex.ini (/usr/share/texlive/texmf-dist/tex/generic/tex-ini-files/pdftexconfig.tex) (/usr/share/texlive/texmf-dist/tex/latex/base/latex.ltx (/usr/share/texlive/texmf-dist/tex/latex/base/texsys.cfg) ./texsys.aux found \@currdir set to: ./. Assuming \openin and \input have the same search path. Defining UNIX/DOS style filename parser. catcodes, registers, parameters, LaTeX2e <2020-02-02> patch level 5 hacks, control, par, spacing, files, font encodings, lengths, ==================================== Local config file fonttext.cfg used ==================================== (/usr/share/texlive/texmf-dist/tex/latex/base/fonttext.cfg (/usr/share/texlive/texmf-dist/tex/latex/base/fonttext.ltx === Don't modify this file, use a .cfg file instead === (/usr/share/texlive/texmf-dist/tex/latex/base/omlenc.def) (/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.def) (/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.def) (/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.def) (/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def) (/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/t1cmr.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/ot1cmr.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/ot1cmss.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/ot1cmtt.fd))) ==================================== Local config file fontmath.cfg used ==================================== (/usr/share/texlive/texmf-dist/tex/latex/base/fontmath.cfg (/usr/share/texlive/texmf-dist/tex/latex/base/fontmath.ltx === Don't modify this file, use a .cfg file instead === (/usr/share/texlive/texmf-dist/tex/latex/base/omlcmm.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/omscmsy.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/omxcmex.fd) (/usr/share/texlive/texmf-dist/tex/latex/base/ucmr.fd))) ==================================== Local config file preload.cfg used ===================================== (/usr/share/texlive/texmf-dist/tex/latex/base/preload.cfg (/usr/share/texlive/texmf-dist/tex/latex/base/preload.ltx)) page nos., x-ref, environments, center, verbatim, math definitions, boxes, title, sectioning, contents, floats, footnotes, index, bibliography, output, =========================================== Local configuration file hyphen.cfg used =========================================== (/usr/share/texlive/texmf-dist/tex/generic/babel/hyphen.cfg (/usr/share/texlive/texmf-dist/tex/generic/babel/switch.def) (/usr/share/texlive/texmf-dist/tex/generic/hyphen/hyphen.tex) (/usr/share/texlive/texmf-dist/tex/generic/hyphen/dumyhyph.tex) (/usr/share/texlive/texmf-dist/tex/generic/hyphen/zerohyph.tex)) (/usr/share/texlive/texmf-dist/tex/latex/base/utf8.def (/usr/share/texlive/texmf-dist/tex/latex/base/omsenc.dfu) (/usr/share/texlive/texmf-dist/tex/latex/base/ot1enc.dfu) (/usr/share/texlive/texmf-dist/tex/latex/base/t1enc.dfu) (/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.dfu)) (/usr/share/texlive/texmf-dist/tex/latex/base/ltexpl.ltx (/usr/share/texlive/texmf-dist/tex/latex/l3kernel/expl3.ltx (/usr/share/texlive/texmf-dist/tex/latex/l3kernel/expl3-code.tex (/usr/share/texlive/texmf-dist/tex/latex/l3kernel/l3deprecation.def)))) ) ) Beginning to dump on file latex.fmt (preloaded format=latex 2020.6.3) 17444 strings of total length 297828 233795 memory locations dumped; current usage is 165&226828 15747 multiletter control sequences \font\nullfont=nullfont \font\OMX/cmex/m/n/10=cmex10 \font\tenln=line10 \font\tenlnw=linew10 \font\tencirc=lcircle10 \font\tencircw=lcirclew10 \font\OT1/cmr/m/n/5=cmr5 \font\OT1/cmr/m/n/7=cmr7 \font\OT1/cmr/m/n/10=cmr10 \font\OML/cmm/m/it/5=cmmi5 \font\OML/cmm/m/it/7=cmmi7 \font\OML/cmm/m/it/10=cmmi10 \font\OMS/cmsy/m/n/5=cmsy5 \font\OMS/cmsy/m/n/7=cmsy7 \font\OMS/cmsy/m/n/10=cmsy10 \font\c__fp_exp_intarray=cmr10 at 0.00002pt \font\c__fp_trig_intarray=cmr10 at 0.00003pt \font\g__regex_charcode_intarray=cmr10 at 0.00005pt \font\g__regex_catcode_intarray=cmr10 at 0.00006pt \font\g__regex_balance_intarray=cmr10 at 0.00008pt \font\g__regex_state_active_intarray=cmr10 at 0.00009pt \font\g__regex_thread_state_intarray=cmr10 at 0.0001pt \font\g__regex_submatch_prev_intarray=cmr10 at 0.00012pt \font\g__regex_submatch_begin_intarray=cmr10 at 0.00014pt \font\g__regex_submatch_end_intarray=cmr10 at 0.00015pt 532331 words of font info for 24 preloaded fonts 14 hyphenation exceptions Hyphenation trie of length 6081 has 183 ops out of 35111 2 for language 1 181 for language 0 0 words of pdfTeX memory 0 indirect objects No pages of output. Transcript written on latex.log. mktexfmt [INFO]: log file copied to: /builddir/.texlive2020/texmf-var/web2c/pdftex/latex.log mktexfmt [INFO]: /builddir/.texlive2020/texmf-var/web2c/pdftex/latex.fmt installed. mktexfmt [INFO]: successfully rebuilt formats: 1 mktexfmt [INFO]: not selected formats: 23 mktexfmt [INFO]: total formats: 24 mktexfmt [INFO]: exiting with status 0 [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2020-02-02> patch level 5 L3 programming layer <2020-04-06> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2019/12/20 v1.4l Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) ! LaTeX Error: File `amsthm.sty' not found. Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty) Enter file name: ! Emergency stop. <read *> l.5 \usepackage {amssymb}^^M No pages of output. Transcript written on math.log.

to

System Message: WARNING/2 (64 imes 64)

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2020-02-02> patch level 5 L3 programming layer <2020-04-06> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2019/12/20 v1.4l Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) ! LaTeX Error: File `amsthm.sty' not found. Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty) Enter file name: ! Emergency stop. <read *> l.5 \usepackage {amssymb}^^M No pages of output. Transcript written on math.log.

. The downsizing is performed by taking the central section instead of averagin the pixels. Finally, the image is store as a dicom image.

Note

This example requires the Numpy library to manipulate the pixel data.

Out:

The image has 64 x 64 voxels
The downsampled image has 8 x 8 voxels
The information of the data set after downsampling:

Dataset.file_meta -------------------------------
(0002, 0000) File Meta Information Group Length  UL: 190
(0002, 0001) File Meta Information Version       OB: b'\x00\x01'
(0002, 0002) Media Storage SOP Class UID         UI: MR Image Storage
(0002, 0003) Media Storage SOP Instance UID      UI: 1.3.6.1.4.1.5962.1.1.4.1.1.20040826185059.5457
(0002, 0010) Transfer Syntax UID                 UI: Explicit VR Little Endian
(0002, 0012) Implementation Class UID            UI: 1.3.6.1.4.1.5962.2
(0002, 0013) Implementation Version Name         SH: 'DCTOOL100'
(0002, 0016) Source Application Entity Title     AE: 'CLUNIE1'
-------------------------------------------------
(0008, 0008) Image Type                          CS: ['DERIVED', 'SECONDARY', 'OTHER']
(0008, 0012) Instance Creation Date              DA: '20040826'
(0008, 0013) Instance Creation Time              TM: '185434'
(0008, 0014) Instance Creator UID                UI: 1.3.6.1.4.1.5962.3
(0008, 0016) SOP Class UID                       UI: MR Image Storage
(0008, 0018) SOP Instance UID                    UI: 1.3.6.1.4.1.5962.1.1.4.1.1.20040826185059.5457
(0008, 0020) Study Date                          DA: '20040826'
(0008, 0021) Series Date                         DA: ''
(0008, 0022) Acquisition Date                    DA: ''
(0008, 0030) Study Time                          TM: '185059'
(0008, 0031) Series Time                         TM: ''
(0008, 0032) Acquisition Time                    TM: ''
(0008, 0050) Accession Number                    SH: ''
(0008, 0060) Modality                            CS: 'MR'
(0008, 0070) Manufacturer                        LO: 'TOSHIBA_MEC'
(0008, 0080) Institution Name                    LO: 'TOSHIBA'
(0008, 0090) Referring Physician's Name          PN: ''
(0008, 0201) Timezone Offset From UTC            SH: '-0400'
(0008, 1010) Station Name                        SH: '000000000'
(0008, 1060) Name of Physician(s) Reading Study  PN: '----'
(0008, 1070) Operators' Name                     PN: '----'
(0008, 1090) Manufacturer's Model Name           LO: 'MRT50H1'
(0010, 0010) Patient's Name                      PN: 'CompressedSamples^MR1'
(0010, 0020) Patient ID                          LO: '4MR1'
(0010, 0030) Patient's Birth Date                DA: ''
(0010, 0040) Patient's Sex                       CS: 'F'
(0010, 1020) Patient's Size                      DS: None
(0010, 1030) Patient's Weight                    DS: "80.0"
(0018, 0010) Contrast/Bolus Agent                LO: ''
(0018, 0020) Scanning Sequence                   CS: 'SE'
(0018, 0021) Sequence Variant                    CS: 'NONE'
(0018, 0022) Scan Options                        CS: ''
(0018, 0023) MR Acquisition Type                 CS: '3D'
(0018, 0050) Slice Thickness                     DS: "0.8"
(0018, 0080) Repetition Time                     DS: "4000.0"
(0018, 0081) Echo Time                           DS: "240.0"
(0018, 0083) Number of Averages                  DS: "1.0"
(0018, 0084) Imaging Frequency                   DS: "63.924339"
(0018, 0085) Imaged Nucleus                      SH: 'H'
(0018, 0086) Echo Number(s)                      IS: "1"
(0018, 0091) Echo Train Length                   IS: None
(0018, 1000) Device Serial Number                LO: '-0000200'
(0018, 1020) Software Versions                   LO: 'V3.51*P25'
(0018, 1314) Flip Angle                          DS: "90.0"
(0018, 5100) Patient Position                    CS: 'HFS'
(0020, 000d) Study Instance UID                  UI: 1.3.6.1.4.1.5962.1.2.4.20040826185059.5457
(0020, 000e) Series Instance UID                 UI: 1.3.6.1.4.1.5962.1.3.4.1.20040826185059.5457
(0020, 0010) Study ID                            SH: '4MR1'
(0020, 0011) Series Number                       IS: "1"
(0020, 0012) Acquisition Number                  IS: "0"
(0020, 0013) Instance Number                     IS: "1"
(0020, 0032) Image Position (Patient)            DS: [-83.9063, -91.2000, 6.6406]
(0020, 0037) Image Orientation (Patient)         DS: [1.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000]
(0020, 0052) Frame of Reference UID              UI: 1.3.6.1.4.1.5962.1.4.4.1.20040826185059.5457
(0020, 0060) Laterality                          CS: ''
(0020, 1040) Position Reference Indicator        LO: ''
(0020, 1041) Slice Location                      DS: "0.0"
(0020, 4000) Image Comments                      LT: 'Uncompressed'
(0028, 0002) Samples per Pixel                   US: 1
(0028, 0004) Photometric Interpretation          CS: 'MONOCHROME2'
(0028, 0010) Rows                                US: 8
(0028, 0011) Columns                             US: 8
(0028, 0030) Pixel Spacing                       DS: [0.3125, 0.3125]
(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, 0106) Smallest Image Pixel Value          SS: 0
(0028, 0107) Largest Image Pixel Value           SS: 4000
(0028, 1050) Window Center                       DS: "600.0"
(0028, 1051) Window Width                        DS: "1600.0"
(7fe0, 0010) Pixel Data                          OW: Array of 128 elements
(fffc, fffc) Data Set Trailing Padding           OB: Array of 126 elements

# authors : Guillaume Lemaitre <g.lemaitre58@gmail.com>
# license : MIT

import pydicom
from pydicom.data import get_testdata_files

print(__doc__)

# FIXME: add a full-sized MR image in the testing data
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

# get the pixel information into a numpy array
data = ds.pixel_array
print('The image has {} x {} voxels'.format(data.shape[0],
                                            data.shape[1]))
data_downsampling = data[::8, ::8]
print('The downsampled image has {} x {} voxels'.format(
    data_downsampling.shape[0], data_downsampling.shape[1]))

# copy the data back to the original data set
ds.PixelData = data_downsampling.tobytes()
# update the information regarding the shape of the data array
ds.Rows, ds.Columns = data_downsampling.shape

# print the image information given in the dataset
print('The information of the data set after downsampling: \n')
print(ds)

Total running time of the script: ( 0 minutes 0.010 seconds)

Gallery generated by Sphinx-Gallery