GithubHelp home page GithubHelp logo

Comments (10)

ctrueden avatar ctrueden commented on June 25, 2024 1

@stefanv Good question! Reading the source, yes, the ImageJ implementation is still quadratic complexity. It has four nested loops: y in [-radius, height+radius], x in [-radius, width+radius], yp in [-radius, radius], and xp in [-radius, radius]. So it's O(r**2 * w * h).

Here are empirical results on a 1024x1024 float32 image:

image

(I'm not sure what the artifacts are at smaller radiiβ€”at first I thought it could be the JDK's JIT, but rerunning the same script yields the same artifacts, so maybe not...)

I looked briefly at the skimage implementation, and it looks reasonable at a glance, using Cython for speed. I see in #4851 that there is even a benchmark. @FirefoxMetzger Does the data above match the expected performance in your experience, or is possible that a regression might have been introduced?

from scikit-image.

jni avatar jni commented on June 25, 2024 1

@ctrueden these shrinkFactors seem pertinent:

https://github.com/imagej/ImageJ/blob/7c00dc5295ffda22fa98e504a1d6cb228441bbe3/ij/plugin/filter/BackgroundSubtracter.java#L779-L795

πŸ˜‚

from scikit-image.

FirefoxMetzger avatar FirefoxMetzger commented on June 25, 2024 1

Here is the old issue: #5193

Could this be the source of the performance degradation? (aka, it's the curse of dimensionality)

from scikit-image.

jni avatar jni commented on June 25, 2024 1

It's polynomial in the radius, with exponent = image.ndim.

I would like the suggestion to suggest either approximating with top-hat or downscaling-then-upscaling. I think for something generally smooth like background intensity, that's actually a good approach.

Otherwise, improving the docs is always a good idea! πŸ™

from scikit-image.

stefanv avatar stefanv commented on June 25, 2024

Thanks for the report, @ctrueden! The skimage version clearly needs some work. Despite starting from a faster baseline, do you also see quadratic scaling with radius for the Fiji implementation? If not, it hints that we may need a different implementation.

from scikit-image.

jni avatar jni commented on June 25, 2024

(It is used before background substraction here and then the image is upscaled again.)

from scikit-image.

FirefoxMetzger avatar FirefoxMetzger commented on June 25, 2024

We had an issue like this before. Last time it was different defaults between our implementation and the Fiji (ImageJ ?) implementation for RGB images.

Our version can roll an N-dimensional ball in the image, i.e., it can process RGB images directly. Theirs could - at least at the time - only process grayscale. It's been a while, so I don't remember the exact algorithm but I do recall that they converted the image (HSV perhaps?) applied rolling ball to a single channel, and then converted back to RGB.

We should have the previous issue in our history somewhere ... let's see if we can find it.

from scikit-image.

jni avatar jni commented on June 25, 2024

Looks like that was a different case of Fiji being much more magical than us. I'm not 100% sure whether it is in scope for skimage to automatically downscale. But we should probably change the default radius at least, πŸ˜… and maybe warn for larger kernels/images that downscaling the image then upscaling the resulting background would be a good approach.

Or we can add relevant kwargs to the function, dunno.

from scikit-image.

jni avatar jni commented on June 25, 2024

(And there is yet a third way Fiji is clever, btw, which is that by default Fiji doesn't use rolling ball but rather what I call "rolling umbrella spokes" πŸ˜‚, see this imagesc thread.)

from scikit-image.

ctrueden avatar ctrueden commented on June 25, 2024

@FirefoxMetzger @jni Thanks for the links to the prior discussions! Very helpful.

Perhaps it would make sense to put these links into the docstring on the rolling_ball, with an explanation of the limitations of the algorithm itself, as well as a list of alternatives? That way we avoid being "magical" but at the same time educate people on how to "design their own preferred magic" that accomplishes their goals.

Perhaps something like this?

diff --git a/skimage/restoration/_rolling_ball.py b/skimage/restoration/_rolling_ball.py
index 2f8c69e11..c349bf6c5 100644
--- a/skimage/restoration/_rolling_ball.py
+++ b/skimage/restoration/_rolling_ball.py
@@ -53,10 +53,18 @@ def rolling_ball(image, *, radius=100, kernel=None, nansafe=False, num_threads=N
     noise). If this is a problem in your image, you can apply mild
     gaussian smoothing before passing the image to this function.
 
+    This algorithm runs in quadratic time to the size of the ball radius,
+    meaning it can take a long time as the radius grows beyond 30 or so [2, 3].
+    It is an exact N-dimensional calculation; if all you need is an
+    approximation, another option to consider is a top-hat filter [4].
+
     References
     ----------
     .. [1] Sternberg, Stanley R. "Biomedical image processing." Computer 1
            (1983): 22-34. :DOI:`10.1109/MC.1983.1654163`
+    .. [2] https://github.com/scikit-image/scikit-image/issues/5193
+    .. [3] https://github.com/scikit-image/scikit-image/issues/7423
+    .. [4] https://forum.image.sc/t/59267/7
 
     Examples
     --------

from scikit-image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.