From 4b8eb2a465794ce2a116ead4e19903bcc3a5821e Mon Sep 17 00:00:00 2001 From: arsalanmstn Date: Wed, 22 Oct 2025 12:53:16 +0200 Subject: [PATCH] Make image2blackwhite functions user-accessible --- pyopia/__init__.py | 2 +- pyopia/process.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pyopia/__init__.py b/pyopia/__init__.py index 95a6d3a..90c1ae3 100644 --- a/pyopia/__init__.py +++ b/pyopia/__init__.py @@ -1 +1 @@ -__version__ = "2.12.0" +__version__ = "2.15.0" diff --git a/pyopia/process.py b/pyopia/process.py index 21cca77..c17adf7 100644 --- a/pyopia/process.py +++ b/pyopia/process.py @@ -376,13 +376,16 @@ def measure_particles(imbw, max_particles=5000): return region_properties -def segment(img, threshold=0.98, minimum_area=12, fill_holes=True): +def segment(img, binary_method='fast', threshold=0.98, minimum_area=12, fill_holes=True): '''Create a binary image from a background-corrected image. Parameters ---------- img : np.array background-corrected image + binary_method : str + method used to convert the background-corrected image to a binary image. + Options are 'fast' or 'accurate', by default 'fast' threshold : float, optional segmentation threshold, by default 0.98 minimum_area : int, optional @@ -397,7 +400,11 @@ def segment(img, threshold=0.98, minimum_area=12, fill_holes=True): ''' logger.info('segment') - imbw = image2blackwhite_fast(img, threshold) + match binary_method: + case 'accurate': + imbw = image2blackwhite_accurate(img, threshold) + case 'fast': + imbw = image2blackwhite_fast(img, threshold) logger.info('clean') @@ -484,6 +491,9 @@ class Segment(): Parameters ---------- + binary_method : str + method used to convert the background-corrected image to a binary image. + Options are 'fast' or 'accurate'. Defaults to 'fast'. minimum_area : (int, optional) minimum number of pixels for particle detection. Defaults to 12. threshold : (float, optional) @@ -503,19 +513,21 @@ class Segment(): ''' def __init__(self, + binary_method='fast', minimum_area=12, threshold=0.98, fill_holes=True, segment_source='im_corrected'): + self.binary_method = binary_method self.minimum_area = minimum_area self.threshold = threshold self.fill_holes = fill_holes self.segment_source = segment_source def __call__(self, data): - data['imbw'] = segment(data[self.segment_source], threshold=self.threshold, fill_holes=self.fill_holes, - minimum_area=self.minimum_area) + data['imbw'] = segment(data[self.segment_source], binary_method=self.binary_method, threshold=self.threshold, + fill_holes=self.fill_holes, minimum_area=self.minimum_area) return data