Skip to content
Back to skills

Connected Components Centroid Extraction

ASecurity

Extracts object centers from 3D segmentation masks using connected-component labeling and centroid computation.

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
developmentpython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill connected-components-centroid-extraction --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Connected Components Centroid Extraction?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Connected Components Centroid Extraction
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-connected-components-centroid-extraction/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-connected-components-centroid-extraction)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-connected-components-centroid-extraction
description: >
  Extracts object centers from 3D segmentation masks using connected-component labeling and centroid computation.
---
# 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

```python
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

- **Connectivity**: 26-connected (face+edge+corner) catches thin structures; 6-connected (face-only) is stricter
- **Min volume filter**: Remove components below N voxels to suppress false positives
- **cc3d vs scipy**: `cc3d` is 2-10x faster on large volumes
- **Multi-class**: Run per-class or use `cc3d.connected_components` with `connectivity=26`

## References

- [Baseline UNet train + submit](https://www.kaggle.com/code/fnands/baseline-unet-train-submit)
- [3d-unet using 2d image encoder](https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…