Smooth Kmeans
The smooth_kmeans() function performs a hybrid segmentation on a DataCube using KMeans clustering followed by spatial smoothing via Markov Random Field (MRF) regularization.
Example
This examples shows the clustering of a syntic generated DataCube.
1import wizard
2from wizard._utils.example import generate_pattern_stack
3from wizard import smooth_kmeans
4import matplotlib.pyplot as plt
5
6# generate synthetic data with spatial patterns
7data = generate_pattern_stack(10, 600, 400, n_circles=3, n_rects=3, n_triangles=3)
8
9# create a DataCube
10dc = wizard.DataCube(data)
11
12# apply hybrid segmentation
13segmentation = smooth_kmeans(dc, n_clusters=4, mrf_iterations=5, kernel_size=12, sigma=1.0)
14
15# visualize a wavelength slice and the segmentation
16fig, axes = plt.subplots(1, 2, figsize=(10, 5))
17
18axes[0].imshow(dc[5])
19axes[0].set_title("Original Slice (λ index 5)")
20axes[0].set_axis_off()
21
22axes[1].imshow(segmentation)
23axes[1].set_title("Segmented (KMeans + MRF)")
24axes[1].set_axis_off()
25
26plt.tight_layout()
27plt.show()
The output demonstrates how spectral features and spatial structure are simultaneously captured in the segmentation result.