Dilate and erode label images in CLIJ

You want to make the labels no longer touching, right? How about this (full code on the bottom):

Starting point image5:
image

Detect Label Edges

Ext.CLIJ2_detectLabelEdges(image5, image6);

Ext.CLIJ2_pull(image6);

image

Binary Not

Ext.CLIJ2_binaryNot(image6, image7);
Ext.CLIJ2_release(image6);

Ext.CLIJ2_pull(image7);

image

Mask

Ext.CLIJ2_mask(image5, image7, image8);
Ext.CLIJ2_release(image5);
Ext.CLIJ2_release(image7);

Ext.CLIJ2_pull(image8);
run("glasbey_on_dark");

image

Minimum2D Box

radius_x = 4.0;
radius_y = 4.0;
Ext.CLIJ2_minimum2DBox(image8, image9, radius_x, radius_y);
Ext.CLIJ2_release(image8);

Ext.CLIJ2_pull(image9);
run("glasbey_on_dark");
Ext.CLIJ2_release(image9);

image

Full script

/*
To make this script run in Fiji, please activate 
the clij, clij2 and IJMMD update sites in your Fiji 
installation. Read more: https://clij.github.io
*/
// Init GPU
run("CLIJ2 Macro Extensions", "cl_device=");

// Load image from disc 
open("http://imagej.nih.gov/ij/images/blobs.gif");
image2 = getTitle();
Ext.CLIJ2_push(image2);

/*
## Threshold Otsu
*/
Ext.CLIJ2_thresholdOtsu(image2, image3);
Ext.CLIJ2_release(image2);

Ext.CLIJ2_pull(image3);

/*
## Connected Components Labeling Box
*/
Ext.CLIJ2_connectedComponentsLabelingBox(image3, image4);
Ext.CLIJ2_release(image3);

Ext.CLIJ2_pull(image4);

/*
## Extend Labeling Via Voronoi
*/
Ext.CLIJ2_extendLabelingViaVoronoi(image4, image5);
Ext.CLIJ2_release(image4);

Ext.CLIJ2_pull(image5);
run("glasbey_on_dark");

/*
## Detect Label Edges
*/
Ext.CLIJ2_detectLabelEdges(image5, image6);

Ext.CLIJ2_pull(image6);

/*
## Binary Not
*/
Ext.CLIJ2_binaryNot(image6, image7);
Ext.CLIJ2_release(image6);

Ext.CLIJ2_pull(image7);

/*
## Mask
*/
Ext.CLIJ2_mask(image5, image7, image8);
Ext.CLIJ2_release(image5);
Ext.CLIJ2_release(image7);

Ext.CLIJ2_pull(image8);

/*
## Minimum2D Box
*/
radius_x = 4.0;
radius_y = 4.0;
Ext.CLIJ2_minimum2DBox(image8, image9, radius_x, radius_y);
Ext.CLIJ2_release(image8);

Ext.CLIJ2_pull(image9);
run("glasbey_on_dark");
Ext.CLIJ2_release(image9);

Let me know if this does the job and sorry for the code quality, it’s generated code :wink:

Cheers,
Robert

2 Likes

Yep, that does the trick!

image

Thanks again for the extensive feedback, it’s really appreciated.
In the end, if you leave out all the displaying and comments, it’s only four lines of code; not so bad! (And of course it’s still superfast :+1:)

	Ext.CLIJ2_detectLabelEdges(labelmap, labeledges);
	Ext.CLIJ2_binaryNot(labeledges, labeledges_inverted);
	Ext.CLIJ2_mask(labelmap, labeledges_inverted, labels_separated);
	Ext.CLIJ2_minimum2DBox(labels_separated, labels_eroded, 1, 1);

Bram

1 Like

@NicoKiaru
I case you are really using this code, you should add one line at the end:

result.setCalibration( labelMask.getCalibration() );
2 Likes