Compute 3D center of mass

Hello,

I have a ImageStack object and need to compute its CenterOfMass in 3D.
I would need to call it from jython. I looked around but could not find it:

https://imagej.nih.gov/ij/developer/api/ij/process/ImageStatistics.html
https://imagej.nih.gov/ij/developer/api/ij/process/StackStatistics.html

It would also be important that the code is very fast and does not “waste time” by computing other things.

Is there something around or should I write an own java class?

Hi @Christian_Tischer,

there is an DefaultCenterOfGravityOp that might be helpful:

# @OpService ops
# @ImgPlus img

print(ops.geom().centerOfGravity(img));

Best,
Stefan

3 Likes

The 3D Analysis suite has the capabiltiy to measure the center of mass of 3D objects:

http://imagejdocu.tudor.lu/doku.php?id=plugin:analysis:3d_analysis:start

1 Like

Thanks a lot. Below code works for me:

# @OpService ops
# @ImgPlus img

from net.imglib2.img.imageplus import ImagePlusImgs
from ij import IJ
import time

imp = IJ.getImage()

start_time = time.time()  
img = ImagePlusImgs.from(imp)
print("    converted image in [s]: "+str(round(time.time()-start_time,3)))

start_time = time.time()  
cog = ops.geom().centerOfGravity(img)
print("    computed CoG in [s]: "+str(round(time.time()-start_time,3)))

For a 1000x1000x100 16-bit image it took about 3s on my small laptop.

converted image in [s]: 0.0
computed CoG in [s]: 3.032

Thanks!
However I think I would have to define an object first, right?
Also, I would depend on a library that is not distributed with FIJI by default; so for distributing my code to other people I guess this would be slightly less easy, would it?!

Actually, you shouldn’t have to do that conversion (img = ImagePlusImgs.from(imp)). With a somewhat recent version of Fiji, img will contain the currently active image (has worked for me, when I tested the script).

You can use the particle analyzer to measure and define objects in a stack with e.g. the 3D objects counter. How looks the data in your stack?
(BoneJ has a similar but according to the website faster 3D particle analyzer for binary stacks, see: http://bonej.org/particles).

I think you can ask the author to create an update site. it’s an active project according to the updates.

1 Like

Yes that would be possible, but for now I only want to compute for the whole image stack without particle detection.

Thanks for letting me know, but I need it to work inside a script, so it should not work on the active image but on an imp object.

You can also do this via ops. A connected component on the binary image gives you a labeling which allows you to get the regions. Regions can then be used standalone to calculate geometric features of objects or in combination with the source image to calculate features based on the intensity values. Actually, some features are implemented n-dimensional, so you can use them for 1d, 2d or 3d.

See:

  • ops.geom(), ops.haralick(), ops.stats(), ops.moments(), ops.lbp(), ops.tamura(), ops.zernike() etc…

We (=@tibuch) are currently going over the implementations again, validate them one more time and also implement pixel based geometric features which not require a Polygon or Mesh.

I think we could implement a way faster method for CoG, i.e. overload the existing one by one which is simply multi-threaded. Also the Op matching in this case may slow down the computation a bit.

2 Likes

A post was split to a new topic: IJ1<->IJ2 synchronization bug