def spectral(image_data: np.ndarray) -> np.ndarray: """ Run Spectral Clustering on given image. Resources * https://sklearn.org/auto_examples/cluster/plot_face_segmentation.html#sphx-glr-auto-examples-cluster-plot-face-segmentation-py * https://sklearn.org/auto_examples/cluster/plot_segmentation_toy.html#sphx-glr-auto-examples-cluster-plot-segmentation-toy-py """ scale = 0.3 scaled_size = ( int(image_data.shape[1] * scale), int(image_data.shape[0] * scale) ) # image_data_scaled is (m * scale, n * scale, 3) image_data_scaled = cv2.resize(image_data, scaled_size) # reduced is (m * scale * n * scale, 1) reduced = flatten_image(image_data_scaled) # labels is (m * scale * n * scale, 1) labels = SpectralClustering( n_clusters=2, eigen_solver="amg", assign_labels="discretize", n_jobs=-1 ).fit_predict(reduced) labels.resize(scaled_size[1], scaled_size[0]) # label_mask is (m * scale, n * scale, 1) label_mask = (get_foreground_label(labels) * 255).astype("uint8") prediction = apply_prediction(label_mask, image_data_scaled) prediction_upscale = cv2.resize( prediction, (image_data.shape[1], image_data.shape[0]) ) return prediction_upscale