Search code examples
javapluginsimagej

ImageJ - Subtract Background


I'm using the "Subtract Background" operation in a ImageJ plugin and I'm trying to use the BackgroundSubtracter class for to do this operation but a have not the same result that when I use the macro sentences. For example:

IJ.run(img, "Subtract Background...", "rolling=" + rollballsize + " light");

And change to:

BackgroundSubtracter bs = new BackgroundSubtracter();
bs.rollingBallBackground(img.getProcessor(), (double)rollballsize, false, true, false, true, true);

And with the second code i just have a white image... Result of both codes

If someone can explain me I will be very grateful. Thanks.


Solution

  • When running background subtraction on 16-bit or 32-bit images via Process > Subtract Background... or via the high level IJ.run() command, the plugin automatically resets the intensity display range to the min and max of the image.

    To get the exact same visualization when using the low level function call, you have to call the ImageProcessor's resetMinAndMax() method:

    BackgroundSubtracter bs = new BackgroundSubtracter();
    bs.rollingBallBackground(img.getProcessor(), (double)rollballsize, false, true, false, true, true);
    img.getProcessor().resetMinAndMax();
    

    For details, see the source of BackgroundSubtracter.