Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

SAM for medical imaging

Segment Anything Model (SAM) for medical imaging segmentation, using DICOM files for automated, efficient analysis in diagnosis and treatment with deep learning advancements.

This is Segment Anything (SAM) on DICOM: Meta’s promptable model, a small adapter that turns a DICOM slice into the RGB array SAM expects, and a lung-mask example. It is not a generic medical-image-segmentation explainer — that is medical image segmentation. It is not SAMJ inside Fiji — that plugin is on the ImageJ page.

What SAM is

Meta’s Segment Anything Model is a promptable segmenter. You give it an image plus a point, a box, text, or a coarse mask. It returns a mask. Three parts:

  • Image encoder — a Vision Transformer (ViT) on a high-resolution input
  • Prompt encoder — sparse prompts (points, boxes, text) and dense prompts (masks)
  • Mask decoder — image embedding + prompt embeddings → a mask

The point of the design is zero-shot transfer: new image types without retraining the whole stack. Medical CT is still a new image type. Upstream SAM eats PNG and JPEG, not DICOM and not Hounsfield units.

SAM architecture: image encoder, prompt encoder, mask decoder

DICOM adapter

The fork is amine0110/SAM-Medical-Imaging. The missing piece is a reader that turns a DICOM into 8-bit BGR. Min–max normalize the pixel array, scale to 0–255, then COLOR_GRAY2BGR. (The first version of this snippet had broken names: HOUNDSFILD_MIN, HOUNS_MIN, normalized. Use this listing.)

def prepare_dicoms(dcm_file, show=False):
    pixel = pydicom.dcmread(dcm_file).pixel_array.astype(np.float32)
    hu_min = float(np.min(pixel))
    hu_max = float(np.max(pixel))
    hu_range = max(hu_max - hu_min, 1.0)
    normalized = (pixel - hu_min) / hu_range
    uint8_image = np.uint8(normalized * 255)
    bgr = cv2.cvtColor(uint8_image, cv2.COLOR_GRAY2BGR)
    if show:
        cv2_imshow(bgr)  # Google Colab
    return bgr

That is a window, not a clinical HU preset. For lung you will usually window first (e.g. centre −600, width 1500) and then scale. SAM still wants three channels.

Output

Prompt the model on the prepared slice (a click in the lung, or a box). The mask below is a DICOM chest slice with the lung filled.

SAM mask on a DICOM chest slice, lung outlined

Code: github.com/amine0110/SAM-Medical-Imaging. Fiji users who want SAM as a click tool should use SAMJ on the ImageJ page, not this Python fork.

PYCAD builds the imaging side of this when the mask has to live in a clinic app. Case studies.

We build custom medical imaging platforms — advanced DICOM viewers, AI segmentation, and the clinical systems around them.

Get in Touch

Copyright © 2026 PYCAD. All Rights Reserved.