clij / clijx

Other
4 stars 6 forks source link

nearest neighbour distances_two label images #35

Open pr4deepr opened 3 years ago

pr4deepr commented 3 years ago

Hi @haesleinhuepf I have been using clij to figure out how to measure the nearest neighbour between two label images, i.e, if I have two label maps, cell_1 and cell_2, I would like to measure for every cell in cell_1, how far away is the closest cell_2? As far as I can tell, the closest pluging I found is: Ext.CLIJx_labelProximalNeighborCountMap but this takes two label maps, and counts for every label in label map 1 how many labels are in a given distance range to it in label map 2. I would like the distances instead. I don't think there is any other function, so I wrote this:


run("Clear Results");

//pixelWidth=0.3787879;
pixelWidth=0.1596665;

// Init CLIJ
run("CLIJ2 Macro Extensions", "cl_device=");
cell_1 = "cell_1.tif";
cell_2="cell_2.tif";
Ext.CLIJ2_pushCurrentZStack(cell_1);
Ext.CLIJ2_pushCurrentZStack(cell_2);

Ext.CLIJ2_centroidsOfLabels(cell_1, cell1_pointlist);
Ext.CLIJ2_centroidsOfLabels(cell_2, cell_2_pointlist);

//distance of cell_1 to nearest cell_2
Ext.CLIJx_generateDistanceMatrix(cell1_pointlist, cell_2_pointlist, matrix);

//As nclosest distances goes through each column and finds minimum value, set first column as 0
//set first row as maximum value so it does not select background
Ext.CLIJ2_setColumn(matrix, 0,0);
Ext.CLIJ2_setRow(matrix, 0, 2147483647); //use Float.max_value in scripts instead

//find closest distances in each column, in this case using 1 as we are only looking at distance of nearest cell
Ext.CLIJ2_nClosestDistances(matrix, distances, idx_matrix, 1);

//shortest distances work for above purpose; just using the index matrix for validation
//Ext.CLIJx_shortestDistances(Image_distance_matrix, Image_destination_minimum_distances);

//convert distances to in microns
Ext.CLIJ2_multiplyImageAndScalar(distances, distance_calibrated, pixelWidth);

//get table output as one column
Ext.CLIJx_transposeXY(distance_calibrated, distance_transpose_xy);

//parameteric image with each cell having distance value in micron
Ext.CLIJ2_replaceIntensities(cell_1, distance_calibrated, label_map);
//get this in results table
Ext.CLIJ2_pullToResultsTable(distance_transpose_xy);

//get distance matrix
Ext.CLIJ2_pull(matrix);
//get point indices of cells
//Ext.CLIJ2_pull(idx_matrix);
Ext.CLIJ2_pull(distance_calibrated);
Ext.CLIJ2_pull(label_map);
Ext.CLIJ2_clear();

//delete value for background from results table
Table.deleteRows(0, 0);

Is this workflow ok? The order of the images passed means, it will measure distance of cell_1 to closest cell_2.

This seems to get what I need. Only issue is that the parametric image generated does not have decimal values as a label. How can I enable float values for labels? Perhaps not too urgent but I'd like to know if its possible.

Also, would this be of interest as a plugin in CLIJx?

Cheers Pradeep

cells.zip

pr4deepr commented 3 years ago

BTW, I've attached the images as a zip file above.. its quite small

haesleinhuepf commented 3 years ago

Hi @pr4deepr ,

Only issue is that the parametric image generated does not have decimal values as a label. How can I enable float values for labels?

Ext.CLIJ2_replaceIntensities(cell_1, distance_calibrated, label_map);

This function creates a new image label_map of the same type as cell_1. In general, this makes sense. In your case it doesnt'. If you change the type of the input image, you will have float numbers in the resulting image. Try adding these two lines at the very beginning of your script:

selectWindow("cell_1.tif");
run("32-bit");

Let me know if the suggested solution works. And btw. on image.sc I respond quicker than on github ;-)

Cheers, Robert