If you're moving from natural-image ML into medical imaging, the first surprise is the data. There's no folder of neat JPEGs. You get DICOM files straight from the scanner, and your annotation tool or training framework may expect NIfTI.
This guide explains both formats, shows how to load them in Python, and covers the mistakes that most often corrupt a dataset without raising an error.
DICOM in a nutshell
DICOM (Digital Imaging and Communications in Medicine) is the standard format for storing and moving medical images in hospitals. A few things to know:
- A DICOM file contains pixel data plus a large metadata header: patient info, modality, scanner settings, pixel spacing, slice position and more.
- A 3D scan (CT, MRI) is usually stored as a folder of 2D slice files, called a series, not a single volume file.
- The header often contains patient-identifying information, so raw DICOM needs de-identification before it is shared or used for training.
NIfTI in a nutshell
NIfTI (.nii or .nii.gz) is common in research and neuroimaging.
- A whole 3D (or 4D) volume lives in one file.
- The header stores an affine matrix that maps voxel indices to real-world coordinates.
- It carries far less metadata than DICOM, which makes it convenient for ML pipelines, but it also means some clinical context is lost.
| DICOM | NIfTI | |
|---|---|---|
| Typical layout | Many files per scan | One file per volume |
| Metadata | Extensive (incl. patient data) | Minimal |
| Main use | Clinical storage and transfer | Research, ML pipelines |
| Spatial info | Per-slice tags | Single affine matrix |
Loading DICOM with pydicom
import pydicom ds = pydicom.dcmread("slice_001.dcm") print(ds.Modality) # e.g. "CT" print(ds.PixelSpacing) # in-plane spacing in mm print(ds.SliceThickness) # slice thickness in mm pixels = ds.pixel_array # 2D numpy array CT gotcha: convert to Hounsfield Units
For CT, raw pixel values are not yet in Hounsfield Units (HU). You need the rescale tags:
hu = pixels * float(ds.RescaleSlope) + float(ds.RescaleIntercept) If you skip this step, intensities may be inconsistent across scanners, and any windowing or normalization you apply afterwards will be wrong.
Loading a full DICOM series with SimpleITK
Reading slices one by one risks wrong ordering. SimpleITK sorts a series by position for you:
import SimpleITK as sitk reader = sitk.ImageSeriesReader() file_names = reader.GetGDCMSeriesFileNames("path/to/series_folder") reader.SetFileNames(file_names) image = reader.Execute() print(image.GetSize()) # (x, y, z) print(image.GetSpacing()) # (sx, sy, sz) in mm sitk.WriteImage(image, "volume.nii.gz") The last line converts the series into a single NIfTI volume.
Loading NIfTI with nibabel
import nibabel as nib img = nib.load("volume.nii.gz") data = img.get_fdata() # numpy array affine = img.affine # voxel -> world transform spacing = img.header.get_zooms() # voxel size in mm print(data.shape, spacing) print(nib.aff2axcodes(affine)) # orientation, e.g. ('R', 'A', 'S') aff2axcodes is a quick way to check orientation. It tells you which direction each array axis points.
Pitfalls that silently break datasets
1. Orientation mismatches. Two files can have the same shape but different axis orientation, so one is effectively flipped relative to the other. Masks and images that look fine separately can be misaligned when combined. Check aff2axcodes and reorient to a common standard before training.
2. Ignoring voxel spacing. Medical volumes are often anisotropic, for example 0.5 mm in-plane but 5 mm between slices. If you resize or feed them to a 3D model without resampling, anatomy gets distorted. Resample to a consistent spacing first.
3. Wrong slice order. Sorting DICOM slices by filename does not guarantee anatomical order. Sort by position tags, or let a library such as SimpleITK handle it.
4. Mixing series. A study folder can contain several series (localizers, different phases, different reconstructions). Make sure you load the one you intend to.
5. Mask and image grid mismatch. If a segmentation mask was exported from a different tool, check that shape, spacing and affine match the image. A good habit is an assertion in your loader:
import numpy as np assert image_data.shape == mask_data.shape, "Shape mismatch" assert np.allclose(image_affine, mask_affine, atol=1e-3), "Affine mismatch" 6. Leaking patient data. Raw DICOM headers can contain names, IDs and dates. De-identify before sharing or training, and follow your applicable privacy rules (such as HIPAA or GDPR).
A sensible pipeline
- Receive DICOM series and de-identify them.
- Verify the series (modality, spacing, slice count, ordering).
- Convert to NIfTI for annotation and training.
- Standardize orientation and spacing.
- Annotate with clear written guidelines.
- Check that every mask matches its image grid.
- Spot-check quality with overlap metrics (Dice, IoU).
Where annotation fits in
Clean file handling is only half the job. The labels matter just as much. If you'd rather hand the labeling side to specialists, Pareidolia Systems provides medical image segmentation, annotation of medical images and quality control for healthcare AI teams, working across multiple annotation platforms.
Summary
- DICOM is the clinical format: many files, rich (and sensitive) metadata.
- NIfTI is the research format: one file, a clean affine, less metadata.
- Most silent bugs come from orientation, spacing, slice order and grid mismatches.
- Verify and standardize before you annotate or train.
Have you run into other medical imaging format gotchas? Share them in the comments.

