ImageJ Ops for Local Threshold

Hello!

I was wondering whether someone has a working jython example of using Ops for local thresholding?

Using “Browse Ops” I get the impression that one needs to use, e.g.,
threshold.LocalNiblack to generate the method and then use it with threshold.apply.LocalThreshold.

But I wasn’t sure how to specify all the parameters and the precise syntax.

Cheers, Christian

1 Like

Hi Christian

Eventually there should be a quick way to call the local threshold, like there is for the non-local autothreshold (something like out = ops.threshold().localNiblack(in)) However I don’t think that is implemented yet (atleast I can’t find it).

Right now the following script works for me. I had to import a bunch of imglib2 stuff to get it to work.

# @OpService ops
# @Dataset data 
# @OUTPUT ImgPlus out

from net.imagej.ops import Ops
from net.imglib2.type.logic import BitType
from net.imglib2.util import ValuePair 
from net.imglib2.algorithm.neighborhood import RectangleShape
from net.imglib2.outofbounds import OutOfBoundsMirrorFactory
from net.imglib2.outofbounds.OutOfBoundsMirrorFactory import Boundary

img=data.getImgPlus()
out=ops.create().img(img, BitType())

niblack=ops.op(Ops.Threshold.LocalNiblack, BitType(), ValuePair(None, img), 0.0, 0.0)

out=ops.threshold().apply(out, img, niblack, RectangleShape(3,False), OutOfBoundsMirrorFactory(Boundary.SINGLE))
6 Likes

Thanks a lot!
This is in fact how I roughly expected the code to look like.
Thanks a lot for figuring out all the little details regarding the syntax!!!
I agree, that’d be good to have a simple one line call at some point.

With the current release of ImageJ Ops the scripts have indeed become a little less verbose:

# @OpService ops
# @Dataset data 
# @OUTPUT ImgPlus out

from net.imagej.ops import Ops
from net.imglib2.type.logic import BitType
from net.imglib2.algorithm.neighborhood import RectangleShape

img=data.getImgPlus()
out=ops.create().img(img, BitType())
out=ops.threshold().localNiblackThreshold(out, img, RectangleShape(3,False), 0.0, 0.0);

This also applies for the other local thresholding techniques. You can take a look at the available methods in the Ops Browser (Plugins > Utilities > Browse Ops…). There you will also find the information that the OutOfBoundsFactory in @bnorthan’s script is optional (as denoted by the ?).

If you have any ideas on how to further improve that stuff, I’d be more than pleased to work on it.

All the best,
Stefan

4 Likes

@stelfrich this works great in my development version. It doesn’t work in my (just updated) release version of Fiji. Is the newest version of Ops shipping with Fiji??

1 Like

@bnorthan That’s interesting. It works in my installation with the Java-8 update site enabled. I am not completely sure if that’s the case for the release versions.

1 Like

Are you using Java 8 with the Java-8 update site activated? The latest release imagej-ops-0.25.0.jar is shipped via Java-8 only, whereas the last release compatible with Java 6 (and served via the ImageJ update site) is imagej-ops-0.23.0.jar.

2 Likes

I am using Java8 but I ussually run ImageJ through my develpment environment. I actually hadn’t activated the java8 update site for fiji. Thanks.