import numpy as np from scipy import ndimage as ndi def build_forearm_shell_mask(seg_mask, spacing, dilation_mm=3.0): """ Gera uma máscara 3D representando a 'casca' do antebraço. - seg_mask: np.ndarray binária [Z,Y,X] - spacing: [sz, sy, sx] em mm - dilation_mm: expansão (mm) em torno da anatomia Retorna: np.ndarray binária [Z,Y,X] """ seg_mask = seg_mask.astype(bool) sp = float(np.mean(spacing)) vpm = 1.0 / max(sp, 1e-6) it = int(max(1, round(dilation_mm * vpm))) # Expande e suaviza a máscara para gerar a casca shell = ndi.binary_dilation(seg_mask, iterations=it) shell = ndi.binary_closing(shell, iterations=2) shell = ndi.binary_fill_holes(shell) shell = ndi.binary_opening(shell, iterations=1) shell = ndi.binary_closing(shell, iterations=1) return shell.astype(np.uint8)