jythonimagejbackground-subtractionfiji

Run BackgroundSubtracter in jython script for Fiji


I am trying to create an Imagej/Fiji script to analyze microscopy images. As part of the pipeline, I want to use the rolling ball BackgroundSubtracter supplied in Fiji. According to the Imagej API, that should look something like this:

rollingBallBackground(ImageProcessor ip,
                              double radius,
                              boolean createBackground,
                              boolean lightBackground,
                              boolean useParaboloid,
                              boolean doPresmooth,
                              boolean correctCorners)

However, if I try to run this on one channel of a three-channel image:

from ij import IJ, ImagePlus
from ij import WindowManager as wm
from ij.plugin import ChannelSplitter
from ij.plugin.filter import BackgroundSubtracter

imp = wm.getCurrentImage()
c1, c2, c3 = ChannelSplitter.split(imp)
c1.show() # This works
c1 = c1.getProcessor()
threshold = BackgroundSubtracter.rollingBallBackground(c1,
                                                      50.,
                                                      False,
                                                      False,
                                                      True,
                                                      False,
                                                      False)

I receive an error:

TypeError: rollingBallBackground(): expected 8 args; got 7

If I append another argument, let's say another "False" or "1", I then get this error:

TypeError: rollingBallBackground(): self arg can't be coerced to ij.plugin.filter.BackgroundSubtracter

What am I doing wrong?


Solution

  • In my experience using this API when I see "Expected args+1; got args" but I definitely have the right number of arguments, it means that I accidentally tried to call a class method instead of an instance method (your second TypeError alludes to this). In this case there aren't any class methods defined for BackgroundSubtracter

    You were super close; to get this to work you need to create an instance of the BackgroundSubtracter and then call the method as you showed

    BackgroundSubtracter().rollingBallBackground(c1,
                                                 50.,
                                                 False,
                                                 False,
                                                 True,
                                                 False,
                                                 False)
    

    Also this method has no return value. threshold would have been None if your code ran as intended.

    I recommend reviewing what Java modifiers are. I'm not super familiar with Java but knowing what the modifiers mean is crucial to understanding what the Jython ImageJ docs are saying, and it's helped me a lot:

    public void rollingBallBackground(ImageProcessor ip,
                                      double radius,
                                      boolean createBackground,
                                      boolean lightBackground,
                                      boolean useParaboloid,
                                      boolean doPresmooth,
                                      boolean correctCorners)
    

    public means that the method is accessible outside of the class as opposed to private which means the method is not accessible.

    void means that there's no return value.

    If the word static was here, then you'd be able to call this method on the class. But it's missing so that means it can only be called on instances.