pydicom | Read, modify and write DICOM files with python code | Computer Vision library

 by   pydicom Python Version: 2.4.4 License: Non-SPDX

kandi X-RAY | pydicom Summary

kandi X-RAY | pydicom Summary

pydicom is a Python library typically used in Artificial Intelligence, Computer Vision applications. pydicom has no bugs, it has no vulnerabilities and it has high support. However pydicom build file is not available and it has a Non-SPDX License. You can install using 'pip install pydicom' or download it from GitHub, PyPI.

pydicom is a pure Python package for working with DICOM files. It lets you read, modify and write DICOM 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. If you're looking for a Python library for DICOM networking then you might be interested in another of our projects: pynetdicom.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              pydicom has a highly active ecosystem.
              It has 1634 star(s) with 444 fork(s). There are 62 watchers for this library.
              There were 1 major release(s) in the last 6 months.
              There are 45 open issues and 899 have been closed. On average issues are closed in 443 days. There are 9 open pull requests and 0 closed requests.
              OutlinedDot
              It has a negative sentiment in the developer community.
              The latest version of pydicom is 2.4.4

            kandi-Quality Quality

              pydicom has 0 bugs and 0 code smells.

            kandi-Security Security

              pydicom has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              pydicom code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              pydicom has a Non-SPDX License.
              Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.

            kandi-Reuse Reuse

              pydicom releases are available to install and integrate.
              Deployable package is available in PyPI.
              pydicom has no build file. You will be need to create the build yourself to build the component from source.
              Installation instructions, examples and code snippets are available.
              It has 128985 lines of code, 2663 functions and 146 files.
              It has high code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed pydicom and discovered the below as its top functions. This is intended to give you an instant insight into pydicom implemented functionality, and help decide if they suit your requirements.
            • Save a dataset to a file
            • Apply LUTs .
            • Reads a partial file from the given file object .
            • Writes a FileSet to the given Path .
            • Encodes the current frame using the supplied transfer syntax .
            • Reshapes a pixel array .
            • Helper method to generate pixel data .
            • Converts a set of named components to a class name .
            • Returns a windowing operation .
            • Performs a dcm read from a file .
            Get all kandi verified functions for this library.

            pydicom Key Features

            No Key Features are available at this moment for pydicom.

            pydicom Examples and Code Snippets

            pydicom-seg,Getting Started,Saving a multi-class segmentation
            Pythondot img1Lines of Code : 19dot img1License : Permissive (MIT)
            copy iconCopy
            segmentation: SimpleITK.Image = ...  # A segmentation image with integer data type
                                                 # and a single component per voxel
            dicom_series_paths = [...]  # Paths to an imaging series related to the segmentation
            source_ima  
            pydicom tools,RT Structure Setの読み込み
            Pythondot img2Lines of Code : 14dot img2License : Permissive (MIT)
            copy iconCopy
            from pydicom_toos import RTSS
            ss = RTSS('path/to/rtss')
            
            # 輪郭名一覧を表示
            print(ss.structures)
            
            # z座標をkeyとする輪郭点座標の辞書へアクセス
            points = ss.points['Structure Name']
            
            # z座標をkeyとするmatplotlib.path.Pathクラスのオブジェクトへアクセス
            paths = ss.paths['Structure Name']
            
            # 輪郭の体積を計算
            v  
            pydicom-seg,Getting Started,Loading binary segments
            Pythondot img3Lines of Code : 13dot img3License : Permissive (MIT)
            copy iconCopy
            import pydicom
            import pydicom_seg
            import SimpleITK as sitk
            
            dcm = pydicom.dcmread('segmentation.dcm')
            
            reader = pydicom_seg.SegmentReader()
            result = reader.read(dcm)
            
            for segment_number in result.available_segments:
                image_data = result.segment_da  
            Get all images of a multi-frame DICOM file
            Pythondot img4Lines of Code : 6dot img4License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            plt.imshow(ds.pixel_array[75]) 
            
            for i, slice in enumerate(ds.pixel_array):
                plt.imshow(slice)
                plt.savefig(f'slice_{i:03n}.png')
            
            Is there a Python DICOM Modality Worklist setup library?
            Pythondot img5Lines of Code : 17dot img5License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            from pydicom.dataset import Dataset, FileMetaDataset
            from pydicom.uid import ExplicitVRLittleEndian
            
            ds = Dataset()
            
            # Add file meta information elements
            ds.file_meta = FileMetaDataset()
            ds.file_meta.TransferSyntaxUID = ExplicitVRLittleEnd
            Is there a Python DICOM Modality Worklist setup library?
            Pythondot img6Lines of Code : 85dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import os
            from os import path
            from pydicom.dataset import Dataset, FileMetaDataset
            from pydicom.uid import ExplicitVRLittleEndian
                    
            wl_file_name = "directory/file.wl"
            txt_file_name = "directory/file.txt"
            
            # Create data set
            ds = Data
            Conversion not working properly using dicom2nifti
            Pythondot img7Lines of Code : 12dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import SimpleITK as sitk
            
            reader = sitk.ImageSeriesReader()
            dicom_names = reader.GetGDCMSeriesFileNames('M0_1')
            reader.SetFileNames(dicom_names)
            image = reader.Execute()
            
            # Added a call to PermuteAxes to change the axes of the data
            image =
            Error when converting DICOM image to pixel_array using tensorflow_io
            Pythondot img8Lines of Code : 32dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import tensorflow_io as tfio
            
            image_bytes = tf.io.read_file(image_path)
            pixel_data = tfio.image.decode_dicom_image(
                image_bytes,
                dtype=tf.uint16)
            
            # type conversion and reshaping is not required
            # as can be checked with the print s
            dummy mask for sample DICOM Image
            Pythondot img9Lines of Code : 4dot img9License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            mask = np.zeros((440, 440), dtype=np.int32)
            polys = np.array([[235,224], [274,248], [285,281], [271,311], [239,308]], dtype=np.int32)
            cv2.fillConvexPoly(mask, polys, 1)
            
            How to access all tags from a DICOM group number?
            Pythondot img10Lines of Code : 12dot img10License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import pydicom
            
            ds = pydicom.dcmread("path/to/file")
            # Must use the full group + element tag
            print(ds[0x00100000:0x00110000])
            
            import pydicom
            
            ds = pydicom.dcmread("path/to/file")
            # Just need the element group numbe

            Community Discussions

            QUESTION

            Get all images of a multi-frame DICOM file
            Asked 2022-Apr-11 at 16:47

            I'm trying get all images in a multi-frame DICOM file. Right now I was successfully able to see and save a single image in a single-frame DICOM file, by using the pydicom and matplotlib libraries, like so:

            ...

            ANSWER

            Answered 2022-Apr-11 at 16:47

            DICOM is essentially a 'three-dimensional' image format: it can store lots of images or 'slices'. However, since colour images are already 3-dimensional (height, width, RGB channels), DICOM datasets can actually be 4-dimensional (apparently... I didn't know this). At least, I think that's why there are four elements in the shape of your dataset.

            My guess is that the dataset contains 150 images, each of which having 768 by 1024 pixels and 3 channels.

            So assuming ds.pixel_array is array-like, you should be able to see the middle slice like so:

            Source https://stackoverflow.com/questions/71830591

            QUESTION

            Conversion not working properly using dicom2nifti
            Asked 2022-Feb-15 at 02:38

            I have a series of dicom files (.dcm) for each patient that I want to convert to nii.gz files, but nothing happens (even error info) with the following conversion...

            ...

            ANSWER

            Answered 2022-Feb-15 at 02:38

            I haven't used dicom2nifti, but you can convert DICOM to Nifti using SimpleITK.

            Here's the code that ought to do the job:

            Source https://stackoverflow.com/questions/71042522

            QUESTION

            How to encapsulate the H.264 bitstream of video file in C++
            Asked 2022-Feb-09 at 08:59

            I'm trying to convert a video file (.mp4) to a Dicom file.
            I have succeeded to do it by storing single images (one per frame of the video) in the Dicom,
            but the result is a too large file, it's not good for me.
            Instead I want to encapsulate the H.264 bitstream as it is stored in the video file, into the Dicom file.
            I've tried to get the bytes of the file as follows:

            ...

            ANSWER

            Answered 2022-Feb-09 at 08:59

            The trick is to redirect the value of the attribute PixelData to a file stream. With this, the video is loaded in chunks and on demand (i.e. when the attribute is accessed). But you have to create the whole structure explicitly, that is:

            • The Pixel Data element
            • The Pixel Sequence with...
            • ...the offset table
            • ...a single item containing the contents of the MPEG file

            Code

            Source https://stackoverflow.com/questions/71034159

            QUESTION

            Error when converting DICOM image to pixel_array using tensorflow_io
            Asked 2022-Feb-03 at 00:15

            I am trying to create a TensorFlow Dataset from DICOM images using the tf.data API and tensorflow_io, and I want to perform some pre-processing using Hounsfield units from the images. The DICOM images have a shape of (512,512). I have extracted the PixelData from the image and want to convert it to a numpy array of appropriate shape using the following code:

            ...

            ANSWER

            Answered 2022-Feb-01 at 20:02

            The function tfio.image.decode_dicom_data decodes the tag information and not the pixel information.

            To read the pixel data use tfio.image.decode_dicom_image instead.

            Source https://stackoverflow.com/questions/70910987

            QUESTION

            dummy mask for sample DICOM Image
            Asked 2022-Jan-30 at 17:49

            I am trying to create a dummy mask on a sample DICOM Image using pydicom, Opencv and numpy

            Getting error -

            ...

            ANSWER

            Answered 2022-Jan-20 at 23:01

            CV_32S in the error means the expected datatype is np.int32

            Also, you're iterating over the poly - meaning you're calling fillConvexPoly on single points. You have to call it on the list of points.

            The following should work:

            Source https://stackoverflow.com/questions/70793638

            QUESTION

            Long initialization time for model.fit when using tensorflow dataset from generator
            Asked 2022-Jan-28 at 10:45

            This is my first question on stack overflow. I apologise in advance for the poor formatting and indentation due to my troubles with the interface.

            Environment specifications:

            Tensorflow version - 2.7.0 GPU (tested and working properly)

            Python version - 3.9.6

            CPU - Intel Core i7 7700HQ

            GPU - NVIDIA GTX 1060 3GB

            RAM - 16GB DDR4 2400MHz

            HDD - 1TB 5400 RPM

            Problem Statement:

            I wish to train a TensorFlow 2.7.0 model to perform multilabel classification with six classes on CT scans stored as DICOM images. The dataset is from Kaggle, link here. The training labels are stored in a CSV file, and the DICOM image names are of the format ID_"random characters".dcm. The images have a combined size of 368 GB.

            Approach used:

            1. The CSV file containing the labels is imported into a pandas DataFrame and the image filenames are set as the index.

            2. A simple data generator is created to read the DICOM image and the labels by iterating on the rows of the DataFrame. This generator is used to create a training dataset using tf.data.Dataset.from_generator. The images are pre-processed using bsb_window().

            3. The training dataset is shuffled and split into a training(90%) and validation set(10%)

            4. The model is created using Keras Sequential, compiled, and fit using the training and validation datasets created earlier.

            code:

            ...

            ANSWER

            Answered 2022-Jan-28 at 10:45

            I FOUND THE ANSWER

            The problem was in the following code:

            Source https://stackoverflow.com/questions/70557782

            QUESTION

            How to access all tags from a DICOM group number?
            Asked 2022-Jan-28 at 01:39

            I would like to extract from a DICOM all the dicom tags with the same group number. For example, I would like to extract all tags belonging to the group 0010. I tried this, and of course it made an error:

            ...

            ANSWER

            Answered 2022-Jan-28 at 01:39

            As @mrbean-bremen and @darcymason have said, there's two ways to get a range of elements via their tag values. You can return an arbitrary range of elements using slicing:

            Source https://stackoverflow.com/questions/70880951

            QUESTION

            loading dicoms with pydicom and sitk results different outputs
            Asked 2021-Dec-23 at 11:11

            My problem is a bit wired. I am working on Prostate MRI dataset which contains dicom images. When I load dicom files using Simple ITK the output numpy array's dtype will be float64 . But when I load same dicom files using pydicom , the output numpy array's dtype will be uint16 And the problem isn't just this. Pixel intensities would be different when using different module. So my question is why they look different and which one is correct and why these modules load data differently? this is the code I'm using to load dcm files.

            ...

            ANSWER

            Answered 2021-Dec-23 at 11:11

            The difference is that pydicom loads the original data as saved in the dataset (which is usually uint16 for MR data), while SimpleITK does some preprocessing (most likely applying the LUT) and returns the processed data as a float array.

            In pydicom, to get data suitable for display, you have to apply some lookup table yourself, usually the one coming with the image.

            If you have a modality LUT (not very common for MR data), you first have to apply that using apply_modality_lut, while for the VOI LUT you use apply_voi_lut. This will apply both modality and VOI LUT as found in the dataset:

            Source https://stackoverflow.com/questions/70459023

            QUESTION

            Trying to input a RGB fMRI DICOM image, modify it, and save it as a grayscale dicom image in Python using pydicom
            Asked 2021-Dec-21 at 06:51

            I am trying to take in an RGB fMRI scan as input and output the same scan but in grayscale with the color parts "burned" white essentially.

            Whenever I try and modify any of the Data Elements, such as Photometric Interpretation and Samples Per Pixel, and use save_as to write the new dicom file, I am unable to open that dicom scan with the dicom viewer giving me the error that it isn't a dicom image.

            My code is below.

            Any help is appreciate. Thanks in advance.

            ...

            ANSWER

            Answered 2021-Dec-21 at 06:51

            The main problem is that your array has the wrong type - it is float instead of byte, so what is saved to the pixel data is a byte representation of float values (which are 4 bytes each). Also, you are setting BitsAllocated to 16, meaning that you expect 2 bytes per pixel, but in your computation you only have a byte range, e.g. you need only 8 bits per pixel.

            Lastly, you had a typo in PhotometricInterpretation ("MONOCRHOME2" instead of "MONOCHROME2"). Here is a possible solution, that converts the float array to a byte array:

            Source https://stackoverflow.com/questions/70427525

            QUESTION

            How to edit nested/child DICOM tags for sequences using pyDicom?
            Asked 2021-Dec-02 at 06:41

            I'm trying to set all the TIME (VR = TM) tags contained within the DICOM dataset to a placeholder value, using pyDicom.

            I can remove the value of all the TIME (VR = TM) tags that are within the root of the DICOM metadata:

            ...

            ANSWER

            Answered 2021-Dec-01 at 17:25

            Using the walk method with a callback is probably the easiest way to do this. You could see the source code for remove_private_tags() as an example, but your function would check for VR of 'TM' and act on those data elements.

            Source https://stackoverflow.com/questions/70188212

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install pydicom

            For more information, including installation instructions for the development version, see the installation guide.

            Support

            The pydicom user guide, tutorials, examples and API reference documentation is available for both the current release and the development version on GitHub Pages.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            Install
          • PyPI

            pip install pydicom

          • CLONE
          • HTTPS

            https://github.com/pydicom/pydicom.git

          • CLI

            gh repo clone pydicom/pydicom

          • sshUrl

            git@github.com:pydicom/pydicom.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link