Produces a touch-matrix where the n nearest neighbors are marked as touching neighbors.
Takes a distance matrix (e.g. derived from a pointlist of centroids) and marks for every column the n smallest distances as neighbors. The resulting matrix can be use as if it was a touch-matrix (a.k.a. adjacency graph matrix).
Inspired by a similar implementation in imglib2 
Note: The implementation is limited to square matrices.
distance_marix : Image touch_matrix_destination : Image n : int number of neighbors
Availability: Available in Fiji by activating the update sites clij and clij2. This function is part of clijx_-0.30.1.21.jar.
Ext.CLIJx_generateNNearestNeighborsMatrix(Image distance_matrix, Image touch_matrix_destination, Number n);
// init CLIJ and GPU import net.haesleinhuepf.clijx.CLIJx; import net.haesleinhuepf.clij.clearcl.ClearCLBuffer; CLIJx clijx = CLIJx.getInstance(); // get input parameters ClearCLBuffer distance_matrix = clijx.push(distance_matrixImagePlus); touch_matrix_destination = clijx.create(distance_matrix); int n = 10;
// Execute operation on GPU clijx.generateNNearestNeighborsMatrix(distance_matrix, touch_matrix_destination, n);
// show result touch_matrix_destinationImagePlus = clijx.pull(touch_matrix_destination); touch_matrix_destinationImagePlus.show(); // cleanup memory on GPU clijx.release(distance_matrix); clijx.release(touch_matrix_destination);
% init CLIJ and GPU clijx = init_clatlabx(); % get input parameters distance_matrix = clijx.pushMat(distance_matrix_matrix); touch_matrix_destination = clijx.create(distance_matrix); n = 10;
% Execute operation on GPU clijx.generateNNearestNeighborsMatrix(distance_matrix, touch_matrix_destination, n);
% show result touch_matrix_destination = clijx.pullMat(touch_matrix_destination) % cleanup memory on GPU clijx.release(distance_matrix); clijx.release(touch_matrix_destination);
import pyclesperanto_prototype as cle cle.generate_n_nearest_neighbors_matrix(distance_matrix, touch_matrix_destination, n)