Centroid of 3D blobs

I have 3D white blobs properly labeled using -label_fg. How do I get the centroid coordinates (x,y,z) of each blob (connected component) using -barycenter? I have done ‘gmic slices* -append z -label_fg -barycenter’ but I think I am only getting one centroid of the first image in the slices* sequence, see output below. Is that expected?

(…)
[gmic]-101./ Append images [0,1,2,(…),98,99,100] along the ‘z’-axis, with alignment 0.
[gmic]-1./ Label foreground connected components on image [1], with tolerance 0 and low connectivity.
[gmic]-1./ Display image [0] = ‘slice.3CRM.000.norm.spots.pgm’.
[0] = ‘slice.3CRM.000.norm.spots.pgm’:
size = (2048,2048,101,1) [1616 Mio of floats].
data = (0,0,0,0,0,0,0,0,0,0,0,0,(…),0,0,0,0,0,0,0,0,0,0,0,0).
min = 0, max = 7392, mean = 3.61601, std = 130.83, coords_min = (0,0,0,0), coords_max = (1051,1940,95,0).

– Alex

Hi Alex,

You just need to create a loop that will isolate each label in a separate image before computing the barycenter.
Look at the example:

$ gmic 400,400 -noise 0.1,2 -dilate_circ 30 -label_fg 0 -repeat {iM-1} --==[0] {\$\>+1} -barycenter. -done -rm[0] -a x

This command creates a synthetic image of labels (with 400,400 -noise 0.1,2 -dilate_circ 30 -label_fg 0), the rest is the loop to compute the barycenter. At the end you get a Nx3 image for N centroids with (X,Y,Z) coordinates.

Hi David,

Thanks for the quick reply and solution. For future reference, I would like to understand the commands within the loop. What does " {iM-1} --==[0] {$>+1} " do? I will be happy to consult a documentation explaining these if available. Let me know.

Well, let’s do it step by step:

  • The command -repeat {iM-1} starts a loop (that will be ended by -done) with {iM-1} iterations. The curly braces here means the content will be evaluated as a math expression, and in that context, the variable iM designates the maximum value of the image [-1]. In this case, the image of label. And the maximum minus one is thus the number of non-zero labels.
  • The command --==[0] {\$\>+1} means 'return a new (boolean) image where each pixel value is either 0 or 1 whether the corresponding pixel value in image [0] (the image of labels here) is equal to $> or not (note the slashes are only here because we invoke it from the shell, and thus we want to avoid $ and > being substituted by the shell itself). $> is nothing else than the running indice (integer) of the loop (here, gets the values from 0 to {iM-2}.
    This command is used to isolate the pixels of the Nth centroid (so having label N), before calling the -barycentercommand on it (to get the barycenter coordinates).

For a complete reference documentation of all G’MIC commands and syntax, please read the language reference page. To start more smoothly, I strongly suggest you read the nice language tutorial pages, written by Garry Osgood.

The versatility of the G’MIC language makes it appear complex at a first glance, but you’ll see this is finally not a big deal to master it.

Thanks for the explanation and links to further documentation. I am often on the reference page but I am not sure I was able to grasp the constructs well. Maybe reading the language tutorial should help. I think a page with examples could be a good resource for start programming with the gmic language, as in learning by example.

1 Like

One short remark to this piece of code from David:

label_fg delivers an image with {iM} labels, hence repeat needs {iM} loops. Otherwise the last labelled object will not be processed.