Connected components centroid extraction

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

Connected components centroid extraction is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Extracts object centers from 3D segmentation masks using connected-component labeling and centroid computation.

How to use this skill

Skill file

Connected-Components Centroid Extraction

Overview

After segmenting a 3D volume into a binary or multi-class mask, convert pixel-level predictions into discrete object detections. Connected-component labeling groups contiguous voxels, then each component's centroid gives the object's 3D coordinate. Essential for particle picking, cell detection, and nodule localization.

Quick Start

import cc3d
import numpy as np

def extract_centroids(segmentation, threshold=0.5):
    binary = (segmentation > threshold).astype(np.uint8)
    labels, n = cc3d.connected_components(binary, return_N=True)
    centroids = []
    for label_id in range(1, n + 1):
        coords = np.argwhere(labels == label_id)
        centroids.append(coords.mean(axis=0))
    return np.array(centroids)  # shape: (N, 3) — z, y, x

Workflow

  1. Threshold the probability map to binary mask
  2. Run 3D connected-component labeling (cc3d or scipy.ndimage.label)
  3. Filter components by volume (discard tiny noise or oversized blobs)
  4. Compute centroid of each component via np.argwhere().mean()
  5. Output list of (z, y, x) coordinates

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.