From d37f5bb5d35d7c4b3d4c3236a1214d209d19a92c Mon Sep 17 00:00:00 2001 From: Jan Eglinger Date: Fri, 15 Dec 2023 13:33:09 +0100 Subject: [PATCH 1/2] threshold: add option of gaussian filtering --- src/faim_wako_searchfirst/segment.py | 4 ++++ tests/test_main.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/faim_wako_searchfirst/segment.py b/src/faim_wako_searchfirst/segment.py index 48eeaf4..a7c8453 100644 --- a/src/faim_wako_searchfirst/segment.py +++ b/src/faim_wako_searchfirst/segment.py @@ -15,6 +15,7 @@ import numpy as np from cellpose import models from scipy.ndimage import binary_fill_holes +from skimage.filters import gaussian from skimage.measure import label, regionprops @@ -22,6 +23,7 @@ def threshold( img, threshold: int, include_holes: bool, + gaussian_sigma: float = 0.0, logger=logging, ): """Segment a given image by global thresholding. @@ -33,6 +35,8 @@ def threshold( :return: a label image representing the detected objects """ + if gaussian_sigma > 0: + img = gaussian(img, sigma=gaussian_sigma, preserve_range=True) mask = img > threshold if include_holes: mask = binary_fill_holes(mask) diff --git a/tests/test_main.py b/tests/test_main.py index 54b7688..2f88266 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -55,6 +55,15 @@ def test_partial(_image, tmp_path): ) assert np.max(labels) == 7 + # segment with gaussian + labels_blurred: np.ndarray = threshold( + _image, + threshold=128, + include_holes=True, + gaussian_sigma=5.5, + ) + assert np.max(labels_blurred) == 4 + # filter label2 = labels.copy() bounding_box( From 0826d4e64ed37d5ecec255ac804d3c36b23984c8 Mon Sep 17 00:00:00 2001 From: Jan Eglinger Date: Fri, 15 Dec 2023 13:40:57 +0100 Subject: [PATCH 2/2] README: add optional gaussian_sigma parameter --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bbcd906..73fb6a8 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ process: # choose method how to segment, filter, and sample the objects threshold: threshold: 128 include_holes: yes + gaussian_sigma: 2.0 # optional bounding_box: min_x: 64 min_y: 0