Preserving Metadata When Converting NIfTI to DICOM

NIfTI to DICOM Workflow: From Research Images to Clinical Formats

Converting neuroimaging data from NIfTI (Neuroimaging Informatics Technology Initiative) format to DICOM (Digital Imaging and Communications in Medicine) is a common requirement when moving datasets from research environments into clinical systems, PACS, or when preparing data for clinical review. This article outlines a clear, practical workflow covering preparation, tool selection, conversion steps, metadata handling, validation, and common pitfalls.

1. Overview: why convert?

  • Compatibility: Clinical systems and PACS typically require DICOM for storage, viewing, and integration with hospital workflows.
  • Metadata needs: DICOM encodes rich patient/study/series metadata required for clinical use.
  • Regulatory/archival reasons: DICOM supports provenance, timestamps, and standard identifiers useful for records and audits.

2. Preparation

  • Confirm intent: Decide whether converted files will be used for clinical review, archiving, or interoperability with clinical tools—this affects required metadata fidelity and patient identifiers.
  • Gather source files: Ensure you have the NIfTI (.nii or .nii.gz) files and any associated JSON sidecars (e.g., BIDS format) or provenance files that contain acquisition parameters.
  • Assemble metadata: Collect patient/study-level metadata: patient name/ID, DOB, sex, study date/time, referring physician, modality, and series descriptions. For MRI/CT, collect acquisition-specific fields: TR/TE, slice thickness, pixel spacing, orientation, and echo number if applicable.

3. Tool selection

Common tools and libraries:

  • dcm2niix (reverse conversion not primary but helpful for metadata extraction)
  • nibabel (Python) — read/write NIfTI and access header fields
  • pydicom (Python) — create and modify DICOM objects
  • dicom-nifti converters: HeuDiConv (primarily DICOM → BIDS), custom scripts using nibabel+pydicom, and specialized tools like “nifti2dicom” utilities or MRI vendor converters Choose: For reproducible, scriptable workflows, use Python with nibabel + pydicom. For simple GUI-based needs, look for dedicated converters that preserve metadata.

4. Mapping metadata: NIfTI header → DICOM tags

  • Extract spatial info: affine matrix in NIfTI provides voxel-to-world mapping; convert to DICOM Image Position (Patient) and Image Orientation (Patient).
  • Pixel data: NIfTI stores voxel intensities; map to DICOM Pixel Data, Bits Allocated, Bits Stored, High Bit, and Photometric Interpretation.
  • Scalars and scaling: Respect scl_slope and scl_inter if present to reproduce correct intensity values.
  • Slice ordering: Ensure proper slice order and spacing (Image Position and Slice Thickness) so slices appear correctly in clinical viewers.
  • Clinical tags: Populate PatientName, PatientID, StudyInstanceUID, SeriesInstanceUID, StudyDate/Time, Modality, Manufacturer, and SeriesDescription. Generate persistent UIDs (e.g., using pydicom.uid.generate_uid()).

5. Conversion process (scripted approach with Python)

  1. Read NIfTI using nibabel: load image data, affine matrix, header fields, and any sidecar JSON.
  2. Prepare DICOM dataset template: start from a minimal DICOM file or create new FileDataset with appropriate filemeta (Transfer Syntax UID, SOP Class UID).
  3. For each slice (2D frame) in the NIfTI volume:
    • Compute Image Position (Patient) from affine.
    • Set Image Orientation (Patient) from affine row/column direction cosines.
    • Assign InstanceNumber and SliceLocation.
    • Convert pixel array to correct dtype and byte order; set BitsAllocated, BitsStored, HighBit.
    • Insert PixelData and any required per-frame tags.
    • Add shared series/study tags for all slices.
    • Save slice as individual DICOM file or assemble into multi-frame DICOM (Enhanced MR or Secondary Capture) depending on target system.
  4. If converting to multi-frame enhanced DICOM, follow the Enhanced MR Image Module structure and relevant per-frame functional groups.

Sample code outline (conceptual):

Code

# read NIfTI with nibabel img = nib.load(‘image.nii.gz’) data = img.get_fdata() affine = img.affine# for each slice: for i in range(data.shape[2]):

slice_pixels = data[:, :, i] ds = create_dicom_dataset_template() ds.ImagePositionPatient = compute_position(affine, i) ds.ImageOrientationPatient = compute_orientation(affine) ds.PixelData = slice_pixels.tobytes() ds.save_as(f'slice_{i:03d}.dcm') 

(Implement proper UID generation, header population, datatype conversions, and file_meta.)

6. Metadata preservation and anonymization

  • If transferring research data into clinical systems, ensure patient identifiers are accurate and appropriate. For de-identified research data, ensure you do not accidentally populate PHI fields if the target system expects real patient data.
  • When anonymizing, remove or replace PatientName, PatientID, AccessionNumber, and any private tags. Keep Study/Series UIDs consistent if linking across datasets is needed.

7. Validation and QA

  • Open converted DICOMs in multiple viewers (e.g., OsiriX, RadiAnt, Horos, 3D Slicer with DICOM plugin) to check orientation, spacing, and intensity.
  • Verify DICOM tags: StudyInstanceUID and SeriesInstanceUID are unique and consistent; check Modality and SOP Class.
  • Compare distances and angles between voxels using slice positions; visually inspect alignment with any original coordinate references.

8. Common pitfalls

  • Wrong orientation/left–right flips from incorrect affine-to-DICOM mapping.
  • Incorrect pixel value scaling if scl_slope/scl_inter ignored.
  • Mismatched data types leading to clipping or loss of dynamic range.
  • Missing or incorrect UIDs causing PACS rejection.
  • Multi-frame vs single-slice expectations in target viewers — some require Enhanced DICOM format.

9. Example use cases

  • Preparing research MRI scans for multidisciplinary clinical review.
  • Archiving study datasets into institutional PACS for long-term retention.
  • Creating DICOMs for third-party analysis tools that accept only DICOM input.

10. Resources and further reading

  • nibabel documentation — reading NIfTI headers and affines.
  • pydicom documentation — building and writing DICOM files.
  • DICOM standard (PS3) — modules for Enhanced MR Image, Image Pixel, and SOP Classes.
  • 3D Slicer and other viewers for validation.

Converting NIfTI to DICOM requires careful handling of spatial metadata, pixel scaling, and DICOM tag population. With a scripted approach using nibabel and pydicom you can build reproducible pipelines that produce clinically compatible DICOMs while preserving provenance and image fidelity.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *