Z-projection using imglib2

Hi everyone!

I am a lapsed Java programmer that is now trying to write ImageJ2 stuff. I’m currently working on a plugin but having a very hard time getting even basic stuff to work. My currently problem is a simple maximum Z-projection. My current code looks like this:

int[] projected_dimensions = new int[cropped.numDimensions() - 1];
Img<FloatType> proj = (Img<FloatType>) ij.op().create().img(new FinalDimensions(projected_dimensions), new FloatType());
 UnaryComputerOp maxOp = Computers.unary(ij.op(),Ops.Stats.Max.class,RealType.class, Iterable.class);
 Img<T> projection = (Img<T>) ij.op().transform().project(proj, cropped, maxOp, 2);

“cropped” is the image I want to Z-project (actually a RandomAccessibleInterval). At the moment, this code gives a division by zero error (Exception in thread “Run$_main” java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero) on the projection line. Is there anything obviously wrong with my code? Am I going at this the right way?

Thanks in advance!

1 Like

Hi @erickratamero

I have a similar example here. I just ran it and it seems to work.

Note that starting on line 38 there is some logic to set up the output dimensions and the projected dimension. I look up which dimension Z is, then use the other dimensions to define the size of the new dataset.

In your code it looks like you created projected_dimensions but I can’t see where you actually set the values in it.


You’re right! I was creating projected_dimensions but never filling it in. Thanks! It seems to be working now.

1 Like

In case you don’t need to be absolutely type-safe, you can also use the OpService#run() and OpService#op() methods, as illustrated in this Groovy script:

#@ Img img
#@ OpService ops

result = ops.run("create.img", [img.dimension(0), img.dimension(1)])
maxOp = ops.op("stats.max", img)
ops.run("project", result, img, maxOp, 2)