From c190fb82478169f4ce0859b33772381359502a6d Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Thu, 29 Feb 2024 16:28:53 +0100 Subject: [PATCH 1/7] Add method for simple flatfield correction --- src/imcflibs/imagej/shading.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index 291842c1..b3e40cbd 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,29 @@ 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 and returns a 32-bit corrected flatfield image. + Parameters + ---------- + imp : ij.ImagePlus + The input stack to be projected. + sigma: double, default 20.0 + The sigma value for the Gaussian blur, default = 20.0 + Returns + ------- + ij.ImagePlus + The 32-bit image result of the flatfield correction + + """ + flatfield = imp.duplicate() + sigma_str = "sigma=" + str(sigma) + IJ.run(flatfield, "Gaussian Blur...", sigma_str) # Apply a gaussian blur + stats = StackStatistics(flatfield) + IJ.run(flatfield, "32-bit", "") # Make a 32 bit version of the image + IJ.run(flatfield, "Divide...", "value=" + str(stats.max)) # Normalize 32 bit image to the highest value of original + ic = ImageCalculator() + flatfield_corrected = ic.run("Divide create", imp, flatfield) + + return flatfield_corrected \ No newline at end of file From b6ffe4d6b3a2b1b9d40cb7d58ba2dfc53632d6c1 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Tue, 5 Mar 2024 15:08:53 +0100 Subject: [PATCH 2/7] Add markdown file detailing a testing script for the shading/flatfield correction method. --- tests/imagej/shading-test.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/imagej/shading-test.md 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 From 001ea40ac28e6e48fcfb833da2dec486eda5aab7 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Wed, 7 Aug 2024 15:00:08 +0200 Subject: [PATCH 3/7] Add testing file for the flatfield method Markdown file for testing the shading/flatfield method. Instructions at the top --- tests/imagej/shading-test.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/imagej/shading-test.md 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 From 51928d06d2aba9fa895972f4cbf50013a6088bc6 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Wed, 7 Aug 2024 15:08:34 +0200 Subject: [PATCH 4/7] Format code for readability and clarity --- src/imcflibs/imagej/shading.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index b3e40cbd..12be6a35 100644 --- a/src/imcflibs/imagej/shading.py +++ b/src/imcflibs/imagej/shading.py @@ -184,7 +184,9 @@ def process_files(files, outpath, model_file, fmt): def simple_flatfield_correction(imp, sigma=20.0): """ - Performs a simple flatfield correction to a given ImagePlus stack and returns a 32-bit corrected flatfield image. + Performs a simple flatfield correction to a given ImagePlus stack + + The function returns a 32-bit corrected flatfield image. Parameters ---------- imp : ij.ImagePlus @@ -202,8 +204,11 @@ def simple_flatfield_correction(imp, sigma=20.0): IJ.run(flatfield, "Gaussian Blur...", sigma_str) # Apply a gaussian blur stats = StackStatistics(flatfield) IJ.run(flatfield, "32-bit", "") # Make a 32 bit version of the image - IJ.run(flatfield, "Divide...", "value=" + str(stats.max)) # Normalize 32 bit image to the highest value of original + IJ.run( + flatfield, + "Divide...", + "value=" + str(stats.max)) # Normalize 32 bit image to the highest value of original ic = ImageCalculator() flatfield_corrected = ic.run("Divide create", imp, flatfield) - return flatfield_corrected \ No newline at end of file + return flatfield_corrected From a3448ff09f4b66a84f05ca910afd72743f8596c9 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Wed, 7 Aug 2024 15:56:34 +0200 Subject: [PATCH 5/7] Correct docstring to conform to convention Add period to docstring, change from double to float and remove redundant "the". --- src/imcflibs/imagej/shading.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index 12be6a35..71470470 100644 --- a/src/imcflibs/imagej/shading.py +++ b/src/imcflibs/imagej/shading.py @@ -184,19 +184,19 @@ def process_files(files, outpath, model_file, fmt): def simple_flatfield_correction(imp, sigma=20.0): """ - Performs a simple flatfield correction to a given ImagePlus stack + 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: double, default 20.0 - The sigma value for the Gaussian blur, default = 20.0 + sigma: float, optional + The sigma value for the Gaussian blur, default=20.0 Returns ------- ij.ImagePlus - The 32-bit image result of the flatfield correction + The 32-bit image result of flatfield correction """ flatfield = imp.duplicate() From 862bbefc8874302e5e1f0da4ba8a6f940665d5d7 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Wed, 7 Aug 2024 16:10:00 +0200 Subject: [PATCH 6/7] Remove redundant comments --- src/imcflibs/imagej/shading.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index 71470470..cd71d929 100644 --- a/src/imcflibs/imagej/shading.py +++ b/src/imcflibs/imagej/shading.py @@ -201,13 +201,16 @@ def simple_flatfield_correction(imp, sigma=20.0): """ flatfield = imp.duplicate() sigma_str = "sigma=" + str(sigma) - IJ.run(flatfield, "Gaussian Blur...", sigma_str) # Apply a gaussian blur + + IJ.run(flatfield, "Gaussian Blur...", sigma_str) stats = StackStatistics(flatfield) - IJ.run(flatfield, "32-bit", "") # Make a 32 bit version of the image + + # 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)) # Normalize 32 bit image to the highest value of original + "value=" + str(stats.max)) ic = ImageCalculator() flatfield_corrected = ic.run("Divide create", imp, flatfield) From 98392d4bc36cf6188a74f3729c2bee2b82df281e Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Fri, 9 Aug 2024 10:37:25 +0200 Subject: [PATCH 7/7] Adjust docstring formatting Removed one blank line and added one before each section heading --- src/imcflibs/imagej/shading.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/shading.py b/src/imcflibs/imagej/shading.py index cd71d929..c7ebb698 100644 --- a/src/imcflibs/imagej/shading.py +++ b/src/imcflibs/imagej/shading.py @@ -183,16 +183,17 @@ def process_files(files, outpath, model_file, fmt): model.close() def simple_flatfield_correction(imp, sigma=20.0): - """ - Performs a simple flatfield correction to a given ImagePlus stack. + """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