Dicom hounsfield normalization

From wenmin-wu/ds-skills · View interactive version

Dicom hounsfield normalization is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Convert raw DICOM pixel arrays to Hounsfield Units using per-slice RescaleSlope/RescaleIntercept, with outside-scanner clamping

How to use this skill

Skill file

DICOM Hounsfield Normalization

Overview

Medical CT scanners store raw pixel values that must be converted to Hounsfield Units (HU) for meaningful analysis. Apply per-slice RescaleSlope and RescaleIntercept from DICOM metadata, then clamp outside-scanner regions to air (−1000 HU). This standardizes pixel values across different scanners and protocols.

Quick Start

import numpy as np
import pydicom

def transform_to_hu(slices):
    """Convert DICOM slices to Hounsfield Units.
    
    Args:
        slices: list of pydicom Dataset objects (one per CT slice)
    Returns:
        3D numpy array in HU scale
    """
    images = np.stack([s.pixel_array for s in slices]).astype(np.int16)
    images[images <= -1000] = 0  # outside-scanner → air
    for n in range(len(slices)):
        intercept = slices[n].RescaleIntercept
        slope = slices[n].RescaleSlope
        if slope != 1:
            images[n] = (slope * images[n].astype(np.float64)).astype(np.int16)
        images[n] += np.int16(intercept)
    return np.array(images, dtype=np.int16)

# Usage
slices = [pydicom.dcmread(f) for f in sorted(dicom_files)]
slices.sort(key=lambda s: float(s.ImagePositionPatient[2]))
hu_volume = transform_to_hu(slices)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.