""" Visualization Utilities for Bone Tumor Segmentation. Provides publication-quality visualization: - Mask overlay on X-rays - Side-by-side model comparisons - Confidence/attention heatmaps - Prompt visualization (boxes, points) All figures at 300 DPI with consistent fonts. """ import os import numpy as np import cv2 import matplotlib.pyplot as plt import matplotlib.patches as patches from matplotlib.gridspec import GridSpec from typing import List, Dict, Optional, Tuple # Publication settings plt.rcParams.update({ 'font.family': 'sans-serif', 'font.sans-serif': ['Arial', 'DejaVu Sans'], 'font.size': 10, 'axes.titlesize': 12, 'axes.labelsize': 10, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'figure.dpi': 300, 'savefig.dpi': 300, 'savefig.bbox': 'tight', 'savefig.pad_inches': 0.1, }) # Color scheme for overlays MASK_COLORS = { 'gt': (0, 255, 0), # Green for ground truth 'pred': (255, 0, 0), # Red for prediction 'overlap': (255, 255, 0), # Yellow for overlap 'sam2': (0, 120, 255), # Blue for SAM2 'medsam': (255, 165, 0), # Orange for MedSAM 'unet': (255, 0, 128), # Pink for UNet } def overlay_mask(image: np.ndarray, mask: np.ndarray, color: Tuple[int, int, int] = (255, 0, 0), alpha: float = 0.35) -> np.ndarray: """ Overlay a binary mask on an image with transparency. Args: image: (H, W, 3) RGB image mask: (H, W) binary mask color: RGB color for the overlay alpha: Transparency (0=transparent, 1=opaque) Returns: Overlaid image """ overlay = image.copy() mask_bool = mask.astype(bool) # Create colored overlay colored = np.zeros_like(image) colored[mask_bool] = color # Blend overlay[mask_bool] = ( (1 - alpha) * image[mask_bool] + alpha * colored[mask_bool] ).astype(np.uint8) # Add contour contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(overlay, contours, -1, color, 2) return overlay def draw_bbox(image: np.ndarray, box: np.ndarray, color: Tuple[int, int, int] = (0, 255, 0), thickness: int = 2, label: str = None) -> np.ndarray: """Draw a bounding box on an image.""" img = image.copy() x1, y1, x2, y2 = box.astype(int) cv2.rectangle(img, (x1, y1), (x2, y2), color, thickness) if label: cv2.putText(img, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) return img def draw_points(image: np.ndarray, points: np.ndarray, labels: np.ndarray = None, radius: int = 5) -> np.ndarray: """Draw point prompts on an image.""" img = image.copy() for i, pt in enumerate(points): x, y = int(pt[0]), int(pt[1]) if labels is not None and labels[i] == 0: color = (255, 0, 0) # Red for negative else: color = (0, 255, 0) # Green for positive cv2.circle(img, (x, y), radius, color, -1) cv2.circle(img, (x, y), radius + 1, (255, 255, 255), 1) return img def visualize_prediction(image: np.ndarray, gt_mask: np.ndarray, pred_mask: np.ndarray, prompts: List[Dict] = None, title: str = "", save_path: str = None): """ Visualize a single prediction with GT overlay. Shows: Original | GT Overlay | Prediction Overlay | Comparison """ fig, axes = plt.subplots(1, 4, figsize=(20, 5)) # Original axes[0].imshow(image) axes[0].set_title('Original X-ray') axes[0].axis('off') # GT overlay gt_overlay = overlay_mask(image, gt_mask, color=MASK_COLORS['gt'], alpha=0.35) if prompts: for p in prompts: gt_overlay = draw_bbox(gt_overlay, p['box'], color=(255, 255, 0), thickness=2) axes[1].imshow(gt_overlay) axes[1].set_title('Ground Truth (box)') axes[1].axis('off') # Prediction overlay pred_overlay = overlay_mask(image, pred_mask, color=MASK_COLORS['pred'], alpha=0.35) axes[2].imshow(pred_overlay) axes[2].set_title('Prediction') axes[2].axis('off') # Comparison (green=GT, red=pred, yellow=overlap) comparison = image.copy() gt_only = gt_mask.astype(bool) & ~pred_mask.astype(bool) pred_only = pred_mask.astype(bool) & ~gt_mask.astype(bool) overlap = gt_mask.astype(bool) & pred_mask.astype(bool) comparison[gt_only] = (0.6 * comparison[gt_only] + 0.4 * np.array([0, 255, 0])).astype(np.uint8) comparison[pred_only] = (0.6 * comparison[pred_only] + 0.4 * np.array([255, 0, 0])).astype(np.uint8) comparison[overlap] = (0.6 * comparison[overlap] + 0.4 * np.array([255, 255, 0])).astype(np.uint8) axes[3].imshow(comparison) axes[3].set_title('Overlap (G=GT, R=Pred, Y=Both)') axes[3].axis('off') if title: fig.suptitle(title, fontsize=14, fontweight='bold') plt.tight_layout() if save_path: os.makedirs(os.path.dirname(save_path), exist_ok=True) plt.savefig(save_path, dpi=300, bbox_inches='tight') plt.close() else: plt.show() def visualize_model_comparison(image: np.ndarray, gt_mask: np.ndarray, predictions: Dict[str, np.ndarray], metrics: Dict[str, Dict] = None, title: str = "", save_path: str = None): """ Side-by-side comparison of multiple models. Args: image: Original X-ray gt_mask: Ground truth mask predictions: Dict mapping model_name → predicted mask metrics: Dict mapping model_name → metrics dict title: Figure title save_path: Where to save """ n_models = len(predictions) fig, axes = plt.subplots(1, n_models + 2, figsize=(5 * (n_models + 2), 5)) # Original axes[0].imshow(image) axes[0].set_title('X-ray', fontweight='bold') axes[0].axis('off') # GT gt_overlay = overlay_mask(image, gt_mask, color=MASK_COLORS['gt']) axes[1].imshow(gt_overlay) axes[1].set_title('Ground Truth', fontweight='bold') axes[1].axis('off') # Each model model_colors = { 'UNet': MASK_COLORS['unet'], 'SAM2': MASK_COLORS['sam2'], 'MedSAM': MASK_COLORS['medsam'], } for i, (model_name, pred_mask) in enumerate(predictions.items()): color = model_colors.get(model_name, (255, 0, 0)) pred_overlay = overlay_mask(image, pred_mask, color=color) axes[i + 2].imshow(pred_overlay) subtitle = model_name if metrics and model_name in metrics: m = metrics[model_name] subtitle += f"\nDice: {m.get('dice', 0):.3f} | IoU: {m.get('iou', 0):.3f}" axes[i + 2].set_title(subtitle, fontweight='bold') axes[i + 2].axis('off') if title: fig.suptitle(title, fontsize=14, fontweight='bold', y=1.02) plt.tight_layout() if save_path: os.makedirs(os.path.dirname(save_path), exist_ok=True) plt.savefig(save_path, dpi=300, bbox_inches='tight') plt.close() else: plt.show() def create_qualitative_grid(images: List[np.ndarray], gt_masks: List[np.ndarray], all_predictions: List[Dict[str, np.ndarray]], all_metrics: List[Dict[str, Dict]] = None, model_names: List[str] = None, save_path: str = None, max_rows: int = 5): """ Create a grid of qualitative results for publication. Each row is one image; columns are: Original, GT, Model1, Model2, ... """ if model_names is None and len(all_predictions) > 0: model_names = list(all_predictions[0].keys()) n_rows = min(len(images), max_rows) n_cols = 2 + len(model_names) fig, axes = plt.subplots(n_rows, n_cols, figsize=(4 * n_cols, 4 * n_rows)) if n_rows == 1: axes = axes[np.newaxis, :] # Column headers col_titles = ['X-ray', 'Ground Truth'] + model_names for j, title in enumerate(col_titles): axes[0, j].set_title(title, fontweight='bold', fontsize=11) model_colors = { 'UNet': MASK_COLORS['unet'], 'SAM2 (box)': MASK_COLORS['sam2'], 'SAM2 (point)': (100, 200, 255), 'MedSAM': MASK_COLORS['medsam'], } for i in range(n_rows): image = images[i] gt = gt_masks[i] # Original axes[i, 0].imshow(image) axes[i, 0].axis('off') # GT gt_overlay = overlay_mask(image, gt, color=MASK_COLORS['gt']) axes[i, 1].imshow(gt_overlay) axes[i, 1].axis('off') # Models preds = all_predictions[i] for j, model_name in enumerate(model_names): if model_name in preds: color = model_colors.get(model_name, (255, 0, 0)) pred_overlay = overlay_mask(image, preds[model_name], color=color) axes[i, j + 2].imshow(pred_overlay) if all_metrics and model_name in all_metrics[i]: m = all_metrics[i][model_name] dice_str = f"D={m.get('dice', 0):.2f}" axes[i, j + 2].text( 0.02, 0.98, dice_str, transform=axes[i, j + 2].transAxes, fontsize=9, color='white', verticalalignment='top', bbox=dict(boxstyle='round', facecolor='black', alpha=0.7) ) else: axes[i, j + 2].imshow(np.zeros_like(image)) axes[i, j + 2].axis('off') plt.tight_layout() if save_path: os.makedirs(os.path.dirname(save_path), exist_ok=True) plt.savefig(save_path, dpi=300, bbox_inches='tight') plt.close() else: plt.show() if __name__ == '__main__': # Quick test with synthetic data print("Visualization module loaded successfully.") print("Use visualize_prediction() or visualize_model_comparison() for plotting.")