skills/cv/morphological-lung-segmentation/SKILL.md
Segment lung regions from CT using HU thresholding, connected-component labeling, and morphological opening
npx skillsauth add wenmin-wu/ds-skills cv-morphological-lung-segmentationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Isolate lung tissue from CT volumes without deep learning. Threshold HU values to separate air-filled lung from dense tissue, use connected-component labeling to identify background regions touching image borders, then apply morphological opening to clean boundaries. Useful as a preprocessing mask before feeding lung ROIs into a model.
import numpy as np
from skimage import measure, morphology
from skimage.morphology import disk
def segment_lung_mask(image, threshold=-320):
"""Segment lungs from a 3D HU volume.
Args:
image: (D, H, W) array in Hounsfield Units
threshold: HU cutoff (air vs tissue boundary)
Returns:
masked volume with non-lung regions zeroed out
"""
segmented = np.zeros(image.shape)
for n in range(image.shape[0]):
binary = np.array(image[n] > threshold, dtype=np.int8) + 1
labels = measure.label(binary)
# Remove regions touching borders (background)
border_labels = np.unique(np.concatenate([
labels[0, :], labels[-1, :], labels[:, 0], labels[:, -1]
]))
for bl in border_labels:
binary[labels == bl] = 2
binary = morphology.opening(binary, disk(2))
binary = 1 - (binary - 1) # invert: lung=1, background=0
segmented[n] = binary * image[n]
return segmented
mask = segment_lung_mask(hu_volume)
data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF