MRI Segmentation

MRI segmentation assigns labels to voxels or pixels for anatomy, lesions, tumor regions, edema, or organs. It is a volumetric specialization of semantic segmentation, and it often supplies measurements for medical image analysis rather than just pictures.

Dice overlap and physical volume

For a volume , the model predicts a mask . Dice overlap for one structure is

Here is the reference mask, is the predicted mask, and counts voxels in the selected structure. The numerator doubles the overlapping voxels; the denominator totals the predicted and reference sizes, so Dice rewards overlap while penalizing both missed voxels and false positives.

Physical volume uses voxel spacing:

when spacings are in millimeters.

Worked example

This snippet compares a predicted 3D lesion mask with ground truth, reporting voxel counts, Dice score, and physical volume estimates.

import numpy as np
 
gt = np.zeros((4,4,4), bool); gt[1:3,1:3,1:3] = 1
pred = gt.copy(); pred[1,1,1] = 0; pred[3,2,2] = 1
inter = np.logical_and(gt, pred).sum()
dice = 2 * inter / (gt.sum() + pred.sum())
voxel_ml = .8 * .8 * 2 / 1000
print("gt_voxels", int(gt.sum()), "pred_voxels", int(pred.sum()))
print("dice", round(dice, 3), "volume_ml_gt", round(gt.sum()*voxel_ml, 4), "volume_ml_pred", round(pred.sum()*voxel_ml, 4))

Observed output:

gt_voxels 8 pred_voxels 8
dice 0.875 volume_ml_gt 0.0102 volume_ml_pred 0.0102

The volumes match, but Dice exposes that one voxel was missed and one false voxel was added. Boundary metrics from detection and segmentation metrics may be needed when millimeter-level contour accuracy matters.

Caveats

Slice-level splits leak patient anatomy. Resampling can change small lesions. Dice can look high on large organs while clinically important boundaries are wrong. Multi-sequence MRI requires consistent registration and missing-sequence handling before any mri classification or segmentation claim is credible.

References