Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segment.threshold: add option of gaussian filtering #44

Merged
merged 2 commits into from
Dec 15, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/faim_wako_searchfirst/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
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


def threshold(
img,
threshold: int,
include_holes: bool,
gaussian_sigma: float = 0.0,
logger=logging,
):
"""Segment a given image by global thresholding.
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down