cv-3d-center-crop-zero-pad
Select center slices from a 3D volume and zero-pad along depth when the scan has fewer slices than the target count
SKILL.mdskills/cv/3d-center-crop-zero-pad/SKILL.md
GitHub3D Center Crop with Zero Padding
Overview
Medical imaging volumes vary in slice count across patients and scanners. To feed them into a fixed-depth 3D CNN, select slices centered around the midpoint and zero-pad the depth dimension if the scan has fewer slices than required. This preserves the most informative region while maintaining a consistent input shape.
Quick Start
import numpy as np
def load_volume_fixed_depth(files, num_slices=64, img_size=256, load_fn=None):
"""Load center slices from a sorted file list, zero-pad if too few."""
middle = len(files) // 2
half = num_slices // 2
p1 = max(0, middle - half)
p2 = min(len(files), middle + half)
slices = [load_fn(f, img_size) for f in files[p1:p2]]
vol = np.stack(slices).T # (H, W, D)
if vol.shape[-1] < num_slices:
pad = np.zeros((img_size, img_size, num_slices - vol.shape[-1]))
vol = np.concatenate([vol, pad], axis=-1)
return np.expand_dims(vol, 0) # (1, H, W, D)
volume = load_volume_fixed_depth(sorted_dcm_files, num_slices=64, load_fn=read_dcm)
Workflow
- Sort slice files by position (filename or DICOM metadata)
- Compute center index and symmetric range around it
- Load only the center slices
- If fewer slices than target, append zero arrays along depth
- Add channel dimension for 3D CNN input
Key Decisions
- Zero-pad vs repeat-pad: zeros are standard; repeating edge slices can reduce boundary artifacts
- Pad location: appending at the end is simplest; symmetric padding centers content better
- num_slices: match the model's expected depth (typically 32, 64, or 128)
- vs resize: interpolating along depth changes inter-slice spacing; zero-pad preserves it