def make_laplacian(mask): edges = _make_edges(mask_to_idx(mask), NGB_SIZE) n = edges.max() + 1 neg_weights = -np.ones(edges.shape[0]) L = sparse.coo_matrix((neg_weights, edges.T), shape=(n, n)) diag = np.vstack((np.arange(n), np.arange(n))) degrees = -np.ravel(L.sum(axis=1)) return sparse.coo_matrix((np.hstack((neg_weights, degrees)), np.hstack((edges.T, diag))), shape=(n, n))
import numpy as np from nipy.algorithms.segmentation._segmentation import _make_edges im = np.random.rand(5, 4, 7) msk = (im > .5).astype('uint') edges = _make_edges(msk, 6)