Skip to content

Commit 18e7453

Browse files
authored
Merge pull request #16 from imcf/shading
Add `simple_flatfield_correction()` to `shading` module
2 parents 6528928 + 1f9f9fb commit 18e7453

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/imcflibs/imagej/shading.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import os
44

55
import ij # pylint: disable-msg=import-error
6-
6+
from ij import IJ
7+
from ij.plugin import ImageCalculator, Concatenator
8+
from ij.process import StackStatistics, ImageProcessor
79
from ..imagej import bioformats # pylint: disable-msg=no-name-in-module
810
from ..imagej import misc, projections
911
from ..log import LOG as log
@@ -179,3 +181,38 @@ def process_files(files, outpath, model_file, fmt):
179181

180182
if model:
181183
model.close()
184+
185+
def simple_flatfield_correction(imp, sigma=20.0):
186+
"""Performs a simple flatfield correction to a given ImagePlus stack.
187+
188+
The function returns a 32-bit corrected flatfield image.
189+
190+
Parameters
191+
----------
192+
imp : ij.ImagePlus
193+
The input stack to be projected.
194+
sigma: float, optional
195+
The sigma value for the Gaussian blur, default=20.0
196+
197+
Returns
198+
-------
199+
ij.ImagePlus
200+
The 32-bit image result of flatfield correction
201+
202+
"""
203+
flatfield = imp.duplicate()
204+
sigma_str = "sigma=" + str(sigma)
205+
206+
IJ.run(flatfield, "Gaussian Blur...", sigma_str)
207+
stats = StackStatistics(flatfield)
208+
209+
# Normalize image to the highest value of original (requires 32-bit image)
210+
IJ.run(flatfield, "32-bit", "")
211+
IJ.run(
212+
flatfield,
213+
"Divide...",
214+
"value=" + str(stats.max))
215+
ic = ImageCalculator()
216+
flatfield_corrected = ic.run("Divide create", imp, flatfield)
217+
218+
return flatfield_corrected

tests/imagej/shading-test.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### ----------------------
2+
3+
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
4+
5+
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.
6+
If a resulting image pops up (while using flatfield method), everything works finely.
7+
### ----------------------
8+
9+
```python
10+
from imcflibs.imagej import shading
11+
# import imcflibs.imagej
12+
import ij
13+
from ij import IJ
14+
15+
imp = IJ.getImage()
16+
imcf_shading = shading.simple_flatfield_correction(imp)
17+
# Or any other method in class shading
18+
imcf_shading.show()

0 commit comments

Comments
 (0)