# # Demo 3b: One-step recursive normalized cuts with Ncut metric # # author: Christos Choutouridis # date: 06/07/2025 # try: from scipy.io import loadmat import matplotlib.pyplot as plt import numpy as np #Project requirements from image_to_graph import image_to_graph from normalized_cuts import n_cuts, calculate_n_cut_value except ImportError as e: print("Missing package:", e) print("Run: pip install -r requirements.txt") exit(1) def plot_split(image, labels, title, fname): M, N, _ = image.shape segmented = labels.reshape(M, N) plt.imshow(segmented, cmap='tab10', vmin=0, vmax=1) plt.title(title) plt.axis('off') plt.tight_layout() plt.savefig(fname) print(f"Saved: {fname}") plt.close() def run_demo3b(): data = loadmat("dip_hw_3.mat") for name in ["d2a", "d2b"]: img = data[name] print(f"\n=== Image {name} ===") affinity = image_to_graph(img) labels = n_cuts(affinity, k=2) ncut_val = calculate_n_cut_value(affinity, labels) print(f" Ncut value: {ncut_val:.4f}") plot_split(img, labels, f"{name} - one step n_cuts (Ncut={ncut_val:.4f})", f"plots/demo3b_{name}_ncut.png") if __name__ == '__main__': run_demo3b()