def contour_pm_watershed(contour_pm, sigma=2, h=0, tissue_mask=None, padding_mask=None, min_area=None, max_area=None): if tissue_mask is None: tissue_mask = np.ones_like(contour_pm) padded = None if padding_mask is not None and np.any(padding_mask == 0): contour_pm, padded = crop_with_padding_mask(contour_pm, padding_mask, return_mask=True) tissue_mask = crop_with_padding_mask(tissue_mask, padding_mask) maxima = peak_local_max(extrema.h_maxima(ndi.gaussian_filter( np.invert(contour_pm), sigma=sigma), h=h), indices=False, footprint=np.ones((3, 3))) maxima = label(maxima).astype(np.int32) # Passing mask into the watershed function will exclude seeds outside # of the mask, which gives fewer and more accurate segments maxima = watershed( contour_pm, maxima, watershed_line=True, mask=tissue_mask) > 0 if min_area is not None and max_area is not None: maxima = label(maxima, connectivity=1).astype(np.int32) areas = np.bincount(maxima.ravel()) size_passed = np.arange(areas.size)[np.logical_and( areas > min_area, areas < max_area)] maxima *= np.isin(maxima, size_passed) np.greater(maxima, 0, out=maxima) if padded is None: return maxima.astype(np.bool) else: padded[padded == 1] = maxima.flatten() return padded.astype(np.bool)
def locate_with_mask(img, padding_mask=None, min_peak_int=None): if padding_mask is not None and np.any(padding_mask == 0): img = rowit.crop_with_padding_mask(img, padding_mask) if min_peak_int is None: p = 95 else: p = percentileofscore(img.flatten(), min_peak_int) with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='Image contains no local maxima.') warnings.filterwarnings( 'ignore', message='All local maxima were in the margins.') spot_table = locate(img, 3, preprocess=False, percentile=p, separation=2) return spot_table