CLIJ2

Logo

GPU accelerated image processing for everyone

CLIJ2 home

Combining meausrements in tables

This notebook demonstrates how measurements from CLIJ2 methods can be combined in ImageJs results tables.

Author: Robert Haase January 2021

Load example data

// clean up first
run("Close All");
run("Clear Results");

// Get test data
//run("Blobs (25K)");
open("C:/structure/data/blobs.tif");
getDimensions(width, height, channels, slices, frames);
input = getTitle();
makeRectangle(74, 44, 100, 100);
run("Crop");

blobs.tif

Initialize GPU and segment the image

run("CLIJ2 Macro Extensions", "cl_device=");
Ext.CLIJ2_clear();
Ext.CLIJ2_push(input);
Ext.CLIJ2_thresholdOtsu(input, binary);
Ext.CLIJ2_connectedComponentsLabeling(binary, labels);
Ext.CLIJ2_pull(labels);
run("glasbey_on_dark");

CLIJ2_connectedComponentsLabeling_result32

Measurements

We perform a measurement which results in a table. To keep the resulting example table simple, we delete all columns but IDENTIFIER and MEAN_INTENSITY. Note, the first row corresponds to measurement resulting from label 0, corresponding to background.

// put many columns in
Ext.CLIJ2_statisticsOfBackgroundAndLabelledPixels(input, labels);
// remove all columns but one
headings = split(Table.headings(), "	");
for (i = 0; i < lengthOf(headings); i++) {
	column_name = headings[i];
	if (column_name != "MEAN_INTENSITY" && column_name != "IDENTIFIER" && column_name != " ") {
		Table.deleteColumn(column_name);
	}
}
IDENTIFIERMEAN_INTENSITY
054.885
1167.857
2210.679
3160.000
4195.953
5184.619
6201.840
7137.143
8196.889
9197.208
10189.802
11138.857
12195.151
13180.417
14136.000

Adding a column

We will now measure the centroid distance of every label to the nearest neighbor and put it in the table as a new column:

// determine nearest neighbor distance
Ext.CLIJ2_centroidsOfLabels(labels, centroids);
Ext.CLIJ2_generateDistanceMatrix(centroids, centroids, distance_matrix);
Ext.CLIJ2_averageDistanceOfNClosestPoints(distance_matrix, distance_list, 1);

// save measurements to table in new column
append_new_rows = false;
Ext.CLIJx_pullToResultsTableColumn(distance_list, "nearest_neighbor_distance", append_new_rows);
IDENTIFIERMEAN_INTENSITYnearest_neighbor_distance
054.8850.000
1167.85732.002
2210.67929.744
3160.00028.212
4195.95326.458
5184.61924.928
6201.84029.744
7137.14326.458
8196.88924.928
9197.20819.415
10189.80229.255
11138.85719.415
12195.15131.551
13180.41718.719
14136.00018.719

We will now determine if labels are on the edge of the image and add a column with 1 if the label touches an edge and 0 if not.

Ext.CLIJx_flagLabelsOnEdges(labels, flag_vector);

append_new_rows = false;
Ext.CLIJx_pullToResultsTableColumn(flag_vector, "is_on_image_edge", append_new_rows);
IDENTIFIERMEAN_INTENSITYnearest_neighbor_distanceis_on_image_edge
054.8850.0001
1167.85732.0021
2210.67929.7441
3160.00028.2121
4195.95326.4580
5184.61924.9281
6201.84029.7440
7137.14326.4581
8196.88924.9281
9197.20819.4150
10189.80229.2551
11138.85719.4150
12195.15131.5511
13180.41718.7191
14136.00018.7191

Last but not least, we clean up GPU memory :-)

Ext.CLIJ2_clear();