How to find number of images in a DICOM study?

Learn how to find number of images in a dicom study? with practical examples, diagrams, and best practices. Covers dicom, imagej, dcm4che development techniques with visual explanations.

Counting Images in a DICOM Study: A Comprehensive Guide

Hero image for How to find number of images in a DICOM study?

Learn various methods to accurately determine the number of images within a DICOM study using command-line tools, programming libraries, and specialized software.

Accurately determining the number of images within a DICOM (Digital Imaging and Communications in Medicine) study is a common task in medical imaging workflows. Whether for quality control, data management, or research purposes, understanding how to efficiently count these images is crucial. This article explores several approaches, ranging from simple command-line utilities to more sophisticated programming methods using Python and Java, as well as specialized DICOM viewers like ImageJ.

Understanding DICOM Study Structure

A DICOM study typically consists of one or more series, and each series contains one or more images (instances). The images within a series share common attributes, while different series might represent different acquisition protocols or modalities (e.g., MRI T1, MRI T2, CT scan). When counting images, it's important to clarify if you need the total number of images across all series in a study, or the count within a specific series.

flowchart TD
    A[DICOM Study] --> B{Series 1}
    A --> C{Series 2}
    B --> D[Image 1.1]
    B --> E[Image 1.2]
    C --> F[Image 2.1]
    C --> G[Image 2.2]
    C --> H[Image 2.3]
    D & E & F & G & H --> I[Total Images in Study]

Hierarchical structure of a DICOM Study, Series, and Images.

Method 1: Command-Line Tools (dcmtk, dcm4che)

For quick checks and scripting, command-line tools are invaluable. dcmtk (DICOM Toolkit) and dcm4che are popular open-source suites that provide utilities for DICOM file manipulation. You can leverage these to list files and count them.

# Using 'find' and 'wc -l' for a directory containing DICOM files
find /path/to/dicom/study -type f -name "*.dcm" | wc -l

# Using 'dcmdump' from dcmtk to verify file type (optional, but good practice)
# This command will list all DICOM files and count them
find /path/to/dicom/study -type f -exec dcmdump {} + 2>/dev/null | grep -c "(0008,0016) UI #" # Checks for SOP Class UID tag

Counting DICOM files using find and dcmdump.

Method 2: Programmatic Approach (Python with pydicom)

For more complex scenarios, such as integrating into an application or needing to extract additional metadata, a programmatic approach is preferred. Python with the pydicom library is an excellent choice for parsing DICOM files.

import os
import pydicom

def count_dicom_images(study_path):
    image_count = 0
    for root, _, files in os.walk(study_path):
        for file_name in files:
            file_path = os.path.join(root, file_name)
            try:
                # Attempt to read as DICOM. pydicom.dcmread will raise an exception
                # if it's not a valid DICOM file.
                ds = pydicom.dcmread(file_path, force=True)
                # Check for a common image-related tag to ensure it's an image instance
                if 'SOPClassUID' in ds and ds.SOPClassUID.name.startswith('MR Image Storage') or \
                   ds.SOPClassUID.name.startswith('CT Image Storage') or \
                   ds.SOPClassUID.name.startswith('CR Image Storage') or \
                   ds.SOPClassUID.name.startswith('DX Image Storage'):
                    image_count += 1
            except pydicom.errors.InvalidDicomError:
                # Not a DICOM file, or corrupted
                continue
            except Exception as e:
                # Other potential errors during file reading
                print(f"Error reading {file_path}: {e}")
    return image_count

# Example usage:
study_directory = "/path/to/your/dicom/study"
total_images = count_dicom_images(study_directory)
print(f"Total DICOM images in study: {total_images}")

Python script using pydicom to count image instances in a DICOM study.

Method 3: Specialized Software (ImageJ/Fiji)

For users who prefer a graphical interface or need to perform further image analysis, software like ImageJ (or its distribution Fiji) is highly effective. ImageJ can open DICOM series and provide information about the number of slices/images.

1. Open ImageJ/Fiji

Launch ImageJ or Fiji on your system.

2. Import DICOM Series

Go to File > Import > Image Sequence... or File > Open... and select one of the DICOM files in your study directory. ImageJ will usually detect the entire series and prompt you to open it as a stack.

3. View Image Count

Once the series is loaded as a stack, the title bar of the image window will typically display information like (1/N), where N is the total number of images (slices) in that series. If you opened multiple series, you'll need to repeat this for each or use a plugin that aggregates study information.