imagej / imagej/imagej-ops

Threshold ops have name clashes

Open
#190 1 comment 0 reactions 1 assignee Claimed by @ctrueden View on GitHub
Dominant language
Java
Stars
94
Forks
44
PR merge metrics
No merged PRs in 30d

Description

When working through the `Ops_Threshold_IJ1_Analyze` script for the Ops scripting workshop, @bnorthan and I discovered that the threshold namespace has a couple of wrinkles.

The following code computes an applies an Otsu threshold to an image:

``` python
# @net.imagej.Dataset d
# @OpService ops
# @OUTPUT net.imglib2.img.Img thresholded
img = d.getImgPlus()
thresholded = ops.threshold().otsu(img)
```

So nice and convenient! But how does it work for float32 images? Well, by default, it bins the values linearly into 256 bins, then uses that histogram to compute the Otsu threshold.

So what if you want to override the number of bins? Then you can write:

``` python
# @net.imagej.Dataset d
# @OpService ops
# @OUTPUT net.imglib2.img.Img thresholded
img = d.getImgPlus()
histogram = ops.image().histogram(img, 1024)
value = ops.threshold().otsu(histogram)
print type(value) # should be the Type, _not_ ArrayImg!
thresholded = ops.threshold().apply(img, value)
```

But this latter invocation does not work, because the wrong `otsu` built-in method (`otsu(Img)` rather than `otsu(Histogram1d)`) is chosen by Jython.

You can work around Jython's limitations by writing:

``` python
# @net.imagej.Dataset d
# @OpService ops
# @OUTPUT net.imglib2.img.Img thresholded
img = d.getImgPlus()
histogram = ops.image().histogram(img, 1024)
value = ops.run("threshold.otsu", histogram)
print type(value) # should be the Type, _not_ ArrayImg!
thresholded = ops.threshold().apply(img, value)
```

But then you receive this error:

```
Multiple 'threshold.otsu' ops of priority 0.0:
1. (Img out?) =
net.imagej.ops.threshold.ApplyThresholdMethod$Otsu(
Img out?,
Img in)
2. (RealType out?) =
net.imagej.ops.threshold.otsu.ComputeOtsuThreshold(
RealType out?,
Histogram1d in)
```

Solving issue #103 would fix this issue, if the solution to that issue includes a strategy of preferring "perfect" parameter matches—in this case, we need `Histogram1d` to match `Histogram1d` before `Img`, even though a `Histogram1d` _is_ also an `Img`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.