Numpy.where in imagej-ops

Hi,

I computed a temporal gradient of an 2D image sequence using imglib2 from jython/imageJ. This gradient img_tg carries positive values when intensity has ‘grown’ and negative when ‘shrunken’. Now, I want to separate these into two images.

In CPythin with numpy I would use:

img_grow   = numpy.where(img_tg>0,  img_tg, 0)
img_shrink = numpy.where(img_tg<0, -img_tg, 0)

Is there sth equivalent in imglib2 / imgaej-ops?

Thanks in advance for any tips and hints!
Chris

2 Likes

Hi @Christoph_Sommer

I tried to code up an example here

I had to use a map and create an op inline

UnaryComputerOp<T, T> op=new AbstractUnaryComputerOp<T, T>() {

			@Override
			public void compute(T input, T output) {
				if (input.getRealFloat()>120) {
					output.set(input);
				}
				else {
					output.setReal(0);
				}
			}
		};
		
		ij.op().map(Views.iterable(out), Views.iterable(image), op);

Perhaps others will have some suggestions on how to make that more concise and/or a cleaner way to do what you want.

There is a related thread here https://forum.image.sc/t/custom-equation-op/

As I mentioned in that thread it could be convenient to have a map that took an intensity based lambda as an input. So you could do the following.

ops.image().apply(image, i-> i<t?i:0);

I ended up implementing an “x,y” equation using a Lambda but I didn’t get around to doing the intensity based version with a map. If people think it will be useful I am happy to do that once the new version of ops is released.

1 Like

Disclaimer: This is only interesting for you if you already are using kotlin or are willing to give it a try. Also, it is very experimental and I will not be able to maintain anything until June:

You can use kotlin extension functions to get a more numpy like workflow for imglib2.

I started a small project, imklib that does that. While it does not offer where or >, <, this should give you an idea (run with kscript):

1 Like

@bnorthan Thanks, I think that could be really useful op! In the meanwhile I’ll use thresholds and multiplication for my problem.

@hanslovsky thanks for the hint to kotlin, I’ll put it on my list of things to checkout!