Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/imcflibs/imagej/shading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/imagej/shading-test.md
Original file line number Diff line number Diff line change
@@ -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()