Giter Site home page Giter Site logo

enhance-pet / nifti2dicom Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 3.0 5.28 MB

Welcome to Nifti2Dicom! Born from countless hours of yelling at screens, this software aims to swap NIfTI for DICOM without inviting any more hair-pulling or infernal uprisings. Because, honestly, who needs another software-induced existential crisis? ๐Ÿ”๐Ÿ’ป๐Ÿ˜ซ๐Ÿ”ฅ

License: Apache License 2.0

Python 100.00%

nifti2dicom's Introduction

nifti2dicom-logo

Nifti2Dicom ๐Ÿง ๐Ÿ’ฝ

PyPI version License: MIT

Hello there, brave soul! ๐ŸŒŸ Welcome to Nifti2Dicom - a project born out of sheer determination, despair, pain and probably a smidge too much caffeine. โ˜•๏ธ Ever felt like converting NIfTI to DICOM was akin to summoning a minor demon from the pandora's box? ๐Ÿ˜ˆ So did we. Which is why we created this snazzy tool to prevent any more unplanned infernal conferences.

Magic Powers (Features) ๐ŸŒŸ

๐ŸŒŒ Dimensional Doorways - Step into our magical portal! Whether you're jumping into a 3D realm or a more mysterious 4D time-warp, we've got you covered. Convert both 3D and 4D nifti images to DICOM. So, if you're clutching a 4D motion-corrected series in nifti, don't fret. We're your dimensional travel agency! ๐ŸŽจ The Colorful Canvas of Segmentations - Ever dreamt of painting the universe with multilabel nifti segmentations? Well, maybe not. But hey, we can convert those vibrant dreams into 3D DICOM for you. Just hand over your brush, or in this case, your label to region mapping (tutorial brewing in our cauldron), and watch the masterpiece unfold!

Prerequisites ๐Ÿ“‹

  • OS: Universal - because we don't discriminate. ๐ŸŒ
  • Python: Version 3.9 is required because even we have our limits. ๐Ÿ

Installation ๐Ÿ”ง

We highly recommend using a different realm (virtual environment) for installing nifti2dicom (or any other Python package, really).

Linux

python3 -m venv nifti2dicom
source nifti2dicom/bin/activate

Windows

python -m venv nifti2dicom
nifti2dicom\Scripts\activate.bat  

And now, wave a magic wand... just kidding. Do this:

pip install nifti2dicom

Usage ๐Ÿš€

Using the mighty Nifti2Dicom is (thankfully) less complicated than its origin story:

  1. Open your command line or terminal. ๐Ÿ’ป
  2. Enter the following command, replacing the placeholders with your actual paths and desired series description:

Converting 3d/4d images

For 3d

   nifti2dicom \
       -d <dicom_dir>               # Directory containing reference DICOM series
       -n <nifti_path>              # Path to the NIFTI file to be converted
       -o <output_dir>              # Directory where the converted DICOM files will be saved
       -desc "<series_description>" # Description for the DICOM series
       -t img                       # Specifies the type of conversion (image in this case)

For 4d

   nifti2dicom \
       -d <dicom_dir>               # Directory containing reference DICOM series
       -n <nifti_path>              # Path to the NIFTI file to be converted
       -o <output_dir>              # Directory where the converted DICOM files will be saved
       -desc "<series_description>" # Description for the DICOM series
       -t img                       # Specifies the type of conversion (image in this case)
       -v <sms | ux>                # Specifies the vendor, either "sms" or "ux"

Ignore the vendor tag, if you are working on 3d images. It is just relevant for 4D. The logic is the same for 3D.

Converting segmentations single/multilabel segmentations

    nifti2dicom \
       -d <dicom_dir>               # Directory containing reference DICOM series
       -n <nifti_path>              # Path to the NIFTI file to be converted
       -o <output_dir>              # Directory where the converted DICOM files will be saved
       -desc "<series_description>" # Description for the DICOM series
       -t seg                       # Specifies the type of conversion (segmentation in this case)
       -j <path_to_json>            # Path to the JSON file containing the organ index

For converting nifti segmentations to DICOM, you always need to a pass a json file, which contains the mapping of the labels to the region names. The sample .json file can be found here.

Example:

Segmentation conversion

nifti2dicom \
    -d ./refDICOM               # Reference directory with DICOM series
    -n ./brainSegmentation.nii  # Path to the NIFTI segmentation file
    -o ./convertedSegDICOM      # Output directory for the converted segmentation DICOM
    -desc "Brain Segmentation"  # Description for the DICOM series
    -t seg                      # Type of conversion: segmentation
    -j ./organ_index.json       # Path to the JSON file with organ index

Image conversion

3d conversion
nifti2dicom \
    -d ./refDICOM               # Reference directory with DICOM series
    -n ./brainMRI.nii           # Path to the NIFTI image file
    -o ./convertedImgDICOM      # Output directory for the converted image DICOM
    -desc "Fancy Brain Scan"    # Description for the DICOM series
    -t img                      # Type of conversion: image
4d conversion
nifti2dicom \
    -d ./refDICOM               # Reference directory with DICOM series
    -n ./brainMRI.nii           # Path to the NIFTI image file
    -o ./convertedImgDICOM      # Output directory for the converted image DICOM
    -desc "Fancy Brain Scan"    # Description for the DICOM series
    -t img                      # Type of conversion: image
    -v sms                      # Vendor: Siemens (you can replace this with the \
                                # appropriate vendor name, as of now support for united imaging \
                                # and siemens is provided. You can choose one of the two (sms or ux), the default value is ux)

Still confused? You can always type nifti2dicom -h for help!

Issues & Feedback ๐Ÿ›๐Ÿ—ฃ

If you stumble upon any pesky bugs, or have suggestions to prevent other unforeseen exorcisms, Open an issue. Also, if you ever come up with a way to bring peace between NIfTI and DICOM, we're all ears (and eyes ๐Ÿ‘€)!

License ๐Ÿ“œ

This project is licensed under the MIT License. Check out the LICENSE file to see the fine print.

Acknowledgments ๐Ÿ‘

  • To coffee, our eternal ally. โ˜•๏ธ
  • The patience of everyone who ever sat near a developer (me) while they mumbled about DICOM headers.
  • The spirit animal of this project: A platypus, because just like this software, it's unique, unexpected, and gets the job done (to a reasonable extent - we are managing expectations here)!

๐ŸŽฉ๐Ÿ”ฎ A Gentle Wizardly Reminder ๐Ÿ”ฎ๐ŸŽฉ

Dear adventurous user, while Nifti2Dicom is sprinkled with a generous dose of magic and wizardry, it's essential to remember that no spell is perfect. Just like the age-old "turn a frog into a prince" trick, sometimes things don't pan out (ask any fairy tale princess). For example our segmentation conversion was tested on slicer 3D with QuantitativeReporting plugin, and we are not sure if it will work on everything.

If you ever find yourself uttering "It doesn't work!" take a deep breath, consider the vastness of the cosmos, and remember โ€” our tool isn't the answer to every cosmic conundrum. It's not a magic bullet (or wand) that'll work wonders in every scenario. But fret not, intrepid one! Reach out, and together, let's see if we can make a tad more magic happen.

nifti2dicom's People

Contributors

lalithshiyam avatar keyn34 avatar

Stargazers

Ahmed Hosny avatar  avatar  avatar Lล‘rincz-Molnรกr Szabolcs-Botond avatar Jakob Wasserthal avatar Joshy Cyriac avatar Eric Kerfoot avatar  avatar  avatar Yang Ding avatar Tom Cohen avatar  avatar  avatar  avatar Praveen Sripad avatar

Watchers

 avatar  avatar  avatar

nifti2dicom's Issues

'LossyImageCompressionRatio' issue when using MOOSE segmentation CT as input

Hi,

Looks like pydicom is looking for LossyImageCompressionRatio attribute when using MOOSE segmentation as input.
I am using nifti2dicom 1.1.3 with patched sms 3D

MOOSE Segmentation info from dataset.json:

{
    "name": "Dataset001_PUMA",
    "description": "",
    "reference": "",
    "licence": "hands off!",
    "release": "0.0",
    "labels": {
        "background": 0,
        "legs": 1,
        "body": 2,
        "head": 3,
        "arms": 4
    },
    "numTraining": 121,
    "file_ending": ".nii.gz",
    "channel_names": {
        "0": "CT"
    }
}

labels_region_body.json as per sample :

{
    "1": "legs",
    "2": "body",
    "3": "head",
    "4": "arms"
}

Error:

nifti2dicom -d J:\MTIC\moose\test\S1\AC-CT -n J:\MTIC\moose\test\S1\moosez-clin_ct_body-2023-08-21-12-50-22\segmentations\CT_Body_CT_2_mtic_fusion_ct_imar_0000.nii.gz -o J:\MTIC\moose\test\S1\seg_dicom_output -desc "test CT moose" -t seg -j J:\MTIC\moose\test\labels_region_body.json

          _ ______  _ ___       ___
   ____  (_) __/ /_(_)__ \ ____/ (_)________  ____ ___
  / __ \/ / /_/ __/ /__/ // __  / / ___/ __ \/ __ `__ \
 / / / / / __/ /_/ // __// /_/ / / /__/ /_/ / / / / / /
/_/ /_/_/_/  \__/_//____/\__,_/_/\___/\____/_/ /_/ /_/
A package to convert NIfTI images to DICOM format using a reference DICOM series. Nifti2dicom is a part of the ENHANCE.PET (https://enhance.pet) framework


 ๐Ÿ” IDENTIFIED DATASETS:

 * Reference DICOM series directory: J:\MTIC\moose\test\S1\AC-CT
 * Loading NIfTI segmentation: J:\MTIC\moose\test\S1\moosez-clin_ct_body-2023-08-21-12-50-22\segmentations\CT_Body_CT_2_mtic_fusion_ct_imar_0000.nii.gz
 Processing segments... โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 100% 0:00:00
Traceback (most recent call last):
  File "k:\Miniconda3\envs\moose\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "k:\Miniconda3\envs\moose\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "k:\Miniconda3\envs\moose\Scripts\nifti2dicom.exe\__main__.py", line 7, in <module>
  File "k:\Miniconda3\envs\moose\lib\site-packages\nifti2dicom\converter.py", line 293, in main
    save_dicom_from_nifti_seg(args.nifti_path, args.dicom_dir, args.output_dir, organ_index)
  File "k:\Miniconda3\envs\moose\lib\site-packages\nifti2dicom\converter.py", line 233, in save_dicom_from_nifti_seg
    seg = hd.seg.Segmentation(
  File "k:\Miniconda3\envs\moose\lib\site-packages\highdicom\seg\sop.py", line 1275, in __init__
    src_img.LossyImageCompressionRatio
  File "k:\Miniconda3\envs\moose\lib\site-packages\pydicom\dataset.py", line 908, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'FileDataset' object has no attribute 'LossyImageCompressionRatio'

MRI Support

Hey there,

I was thrilled to finally find a tool that would convert NIfTI to DICOM, including segmentations. But unfortunately, MR is an unknown modality at the moment. I was wondering if you plan to support MRI in the near future?

Nifti to dicom with different size

My nifti images (4D) wich are the output of the trained network have different size size of their reference dicom format images. How could I use your library? I tried and I always receive the error of dimension mismatch.

4D flip

see below


Microsoft Windows [Version 10.0.19044.3324]
(c) Microsoft Corporation. All rights reserved.

C:\Users\MTIC>cd C:\QIMP_Data

C:\QIMP_Data>nifti2dicom\Scripts\activate.bat

(nifti2dicom) C:\QIMP_Data>nifti2dicom -d C:\QIMP_Data\MOOSE\2023-08__Studies\WEE^JOSEPHINE^^MRS_121.892_CT_2023-08-08_100303_PET^MTIC.FDG.RTP.Partial.Body.(Adult)_MTIC.CT.5.0.iMAR_n354__00000 -n C:\QIMP_Data\MOOSE\2023-08__Studies\CT_2_mtic_ct_50_imar.nii -o C:\QIMP_Data\MOOSE\2023-08__Studies\dicom_output -desc "Test N2D" -t img -v sms

โ†[38;5;141m _ ______ _ ___ ___
____ () __/ /()__ \ / ()_____ ____ ___
/ __ / / /
/ / // // __ / / / __ / __ `
/ / / / / __/ /
/ // // /_/ / / // // / / / / / /
/
/ //// _////_,//_/_// // //โ†[0m
โ†[38;5;141mA package to convert NIfTI images to DICOM format using a reference DICOM series. Nifti2dicom is a part of the ENHANCE.PET (https://enhance.pet/) frameworkโ†[0m

โ†[38;5;141m ๐Ÿ” IDENTIFIED DATASETS:โ†[0m

โ†[38;5;208m* Image dimensions: 3โ†[0m
โ†[38;5;40m* Loading NIfTI image: C:\QIMP_Data\MOOSE\2023-08__Studies\CT_2_mtic_ct_50_imar.niiโ†[0m
Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in run_code
File "C:\QIMP_Data\nifti2dicom\Scripts\nifti2dicom.exe_main
.py", line 7, in
File "C:\QIMP_Data\nifti2dicom\Lib\site-packages\nifti2dicom\converter.py", line 280, in main
save_dicom_from_nifti_image(args.dicom_dir, args.nifti_path, args.output_dir, args.series_description, args.vendor)
File "C:\QIMP_Data\nifti2dicom\Lib\site-packages\nifti2dicom\converter.py", line 132, in save_dicom_from_nifti_image
image_data = np.flip(image_data, (1, 3))
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\QIMP_Data\nifti2dicom\Lib\site-packages\numpy\lib\function_base.py", line 340, in flip
axis = _nx.normalize_axis_tuple(axis, m.ndim)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\QIMP_Data\nifti2dicom\Lib\site-packages\numpy\core\numeric.py", line 1380, in normalize_axis_tuple
axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\QIMP_Data\nifti2dicom\Lib\site-packages\numpy\core\numeric.py", line 1380, in
axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
numpy.exceptions.AxisError: axis 3 is out of bounds for array of dimension 3

(nifti2dicom) C:\QIMP_Data>

Feature Request: Support for RGB 3D NIfTI to RGB DICOM Conversion

Objective: Introduce the ability to convert RGB 3D NIfTI images into RGB DICOM images, specifically tailored for multiplexed images generated by PUMA.

Key DICOM Tags for Modification:

  1. SOP Class UID (0008,0016): Use 1.2.840.10008.5.1.4.1.1.7 for Secondary Capture Image Storage.
  2. Modality (0008,0060): Set to SC for Secondary Capture.
  3. Samples per Pixel (0028,0002): Set to 3 for RGB images.
  4. Photometric Interpretation (0028,0004): Use RGB.
  5. Rows (0028,0010) and Columns (0028,0011): Should match the dimensions of the NIfTI image slices.
  6. Bits Allocated (0028,0100), Bits Stored (0028,0101), and High Bit (0028,0102): Typically set to 8 for each color component in RGB images.
  7. Pixel Data (7FE0,0010): The actual RGB image data, encoded as RGB triplets.

Rationale: This feature will expand the package's capabilities to include the processing of multiplexed RGB images, enhancing its utility in medical image analysis workflows, especially those involving advanced visualization techniques.

Expected Outcome: Users will be able to convert multiplexed RGB 3D NIfTI images into DICOM format, maintaining high fidelity of the original data and ensuring compatibility with DICOM-based medical imaging systems.

MR Modality

Hi nifto2dicom
Thanks for your work and effort on this convertor, it is great.
As I tried to convert the MR nifti files to dicom, it failed, as giving back the error of Unknown modality.
I added the MR modality to the convertor.py same as CT, and it ended up with having dicom series upside down in all sagittal, coronal and axial views. Would you please deal with this bug, so it would be perfect convertor for all modalities.

Best,
BSZ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.