Use Python to play DICOM file - Medical Image Processing Tool Pydicom Getting Started

tags: Medical image processing  python  dicom

The ultrasound data collected from the hospital is more chaotic because of the large time span, and the equipment difference is more chaotic. If you think about how to find these DICOM formatted files, then aiming at Pydicom. Chinese materials are also mixed, and they simply go to their official documents. The installation method is very simple in Anaconda Pip Install Pydicom

Reference:Pydicom official documentation
version: pydicom 2.0.0

Data reading

Data readingdmread()

import pydicom

fpath='us13.DCM'
dcm=pydicom.dcmread(fpath)

Sometimes the following error may occur

raise InvalidDicomError("File is missing DICOM File Meta Information "
  pydicom.errors.InvalidDicomError: File is missing DICOM File Meta Information header or the 'DICM' prefix is missing from the header. Use force=True to force reading.

This is due to the loss caused by information in the DICOM file or this file is not a DICOM file but contains DICOM data, and if it is confident that this file contains DICOM data, it should call the Force parameter to call the Force parameter to call the Force parameter according to the prompt.

dcm=pydicom.dcmread(fpath,force=True)

Access to information

After completing the above DICOM file, you can print out the header file information using the Print instruction.

import pydicom

fpath='us13.DCM'
dcm=pydicom.dcmread(fpath)
print(dcm)

Some screenshot of the output

Data writing

The simplest way can be usedsace_as()Save data

dcm.save_as('out.dcm')

By default,save_as()It is written in the file in the original way. This means that even if your data set does not meet the DICOM file format, it will still be strictly written in a given format. To determine that you are writing a data set in a DICOM file format, you can usewrite_like_originalKeyword parameters:

dcm.save_as('out.dcm', write_like_original=False)

The above operation will automatically add the missing header file information in the file. If it is not possible to successfully complete, it will report an error.

ValueError: Missing required File Meta Information elements from 'file_meta':
          (0002, 0010) TransferSyntaxUID

At this time, you need to manually make relevant information, and save it.

dcm.file_meta = FileMetaDataset()
dcm.file_meta.TransferSyntaxUID = '1.2.840.10008.1.2.1'
dcm.save_as('out.dcm', write_like_original=False)

Visualization using matplotlib

import pydicom
from matplotlib import pyplot as plt


fpath='US6.DCM'
dataset=pydicom.dcmread(fpath)

if 'PixelData' in dataset:
    rows = int(dataset.Rows)
    cols = int(dataset.Columns)
    print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
        rows=rows, cols=cols, size=len(dataset.PixelData)))
    if 'PixelSpacing' in dataset:
        print("Pixel spacing....:", dataset.PixelSpacing)

plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()

Intelligent Recommendation

Medical image DCM/DICOM file to JPG

import SimpleITK as sitk import numpy as np import cv2 import os import time from PIL import Image count = 1 path = “dcm/201015” filename = os.listdir(path) print(filename) for i in filena...

Getting started with pydicom rookie

1. Download python 2. Download pycharm 3. Configure the path of the .script file in the python directory to the system environment PATH, restart, and you can use the pip command 4. Install the pydicom...

DICOM format analysis and Pydicom simple processing

1. Introduction to DICOM DICOM (Digital Imaging and Communications in Medicine): Medical imaging imaging and communications standards. All patient medical images are stored in DICOM file format. This ...

Pydicom+SimpleITK operates DICOM image data and TAG

The current requirement is to divide the original DCM file into organ and then save the obtained image data as a new dcm file, and the new dcm file header should retain the basic information of the or...

More Recommendation

Save the picture as a DICOM file using Pydicom

Article catalog DICOM standard Image Save as DICOM file example Because the item needs, the picture needs to be stored as a DICOM format. There is no particularly good example on the Internet, and Pyd...

pydicom and SimpleITK respectively analyze dicom files in medical images

pydicom and SimpleITK respectively analyze dicom files in medical images First of all, both pydicom and SimpleITK are libraries that need to be imported into python in advance. If you are using pychar...

Python reads DICOM files with pydicom library

Python reads DICOM files and calls with Pydicom library Method 1: Read through Group, Element Method 2: Read through tag Method 3: Get () method Universal The following name is taken as an example. In...

Using pydicom to realize Dicom file reading and CT image window width and window level adjustment

1 Introduction In order to be able to edit Dicom images on Labelme, we have studied the reading of Dicom files under the python environment. The window width and window level of CT in Dicom images is ...

DICOM Medical Image Processing: First Talk on WEB PACS, Image Transmission

2019 Unicorn Enterprise Heavy Recruitment Standards for Python Engineers >>> background: As mentioned in the previous column, use CGI or FastCGI technology to forward user requests sent by th...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top