diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index 291842c1..c7ebb698 100644 --- a/src/imcflibs/imagej/shading.py +++ b/src/imcflibs/imagej/shading.py @@ -3,7 +3,9 @@ import os import ij # pylint: disable-msg=import-error - +from ij import IJ +from ij.plugin import ImageCalculator, Concatenator +from ij.process import StackStatistics, ImageProcessor from ..imagej import bioformats # pylint: disable-msg=no-name-in-module from ..imagej import misc, projections from ..log import LOG as log @@ -179,3 +181,38 @@ def process_files(files, outpath, model_file, fmt): if model: model.close() + +def simple_flatfield_correction(imp, sigma=20.0): + """Performs a simple flatfield correction to a given ImagePlus stack. + + The function returns a 32-bit corrected flatfield image. + + Parameters + ---------- + imp : ij.ImagePlus + The input stack to be projected. + sigma: float, optional + The sigma value for the Gaussian blur, default=20.0 + + Returns + ------- + ij.ImagePlus + The 32-bit image result of flatfield correction + + """ + flatfield = imp.duplicate() + sigma_str = "sigma=" + str(sigma) + + IJ.run(flatfield, "Gaussian Blur...", sigma_str) + stats = StackStatistics(flatfield) + + # Normalize image to the highest value of original (requires 32-bit image) + IJ.run(flatfield, "32-bit", "") + IJ.run( + flatfield, + "Divide...", + "value=" + str(stats.max)) + ic = ImageCalculator() + flatfield_corrected = ic.run("Divide create", imp, flatfield) + + return flatfield_corrected diff --git a/tests/imagej/shading-test.md b/tests/imagej/shading-test.md new file mode 100644 index 00000000..9371be38 --- /dev/null +++ b/tests/imagej/shading-test.md @@ -0,0 +1,18 @@ +### ---------------------- + + The following code block is a `python` script to be used in a Fiji with the shading branch's .jar already pasted into ./jars in the Fiji installation + + Recommended is to import an image you wish to test on (Shaded-blobs.png e.g) and then drag this script into Fiji and run it. + If a resulting image pops up (while using flatfield method), everything works finely. +### ---------------------- + +```python +from imcflibs.imagej import shading +# import imcflibs.imagej +import ij +from ij import IJ + +imp = IJ.getImage() +imcf_shading = shading.simple_flatfield_correction(imp) +# Or any other method in class shading +imcf_shading.show() \ No newline at end of file