def gbernsen(f, se, contrast_threshold, gthresh): ''' thresholded = gbernsen(f, se, contrast_threshold, gthresh) Generalised Bernsen local thresholding Parameters ---------- f : ndarray input image se : boolean ndarray structuring element to use for "locality" contrast_threshold : integer contrast threshold gthresh : numeric, optional global threshold to fall back in low contrast regions Returns ------- thresholded : binary ndarray See Also -------- bernsen : function Bernsen thresholding with a circular region ''' from mahotas.convolve import rank_filter fmax = rank_filter(f, se, se.sum()-1) fmin = rank_filter(f, se, 0) fptp = fmax - fmin fmean = fmax/2. + fmin/2. # Do not use (fmax + fmin) as that may overflow return np.choose(fptp < contrast_threshold, (fmean < gthresh, fmean > f))
def gbernsen(f, se, contrast_threshold, gthresh): ''' thresholded = gbernsen(f, se, contrast_threshold, gthresh) Generalised Bernsen local thresholding Parameters ---------- f : ndarray input image se : boolean ndarray structuring element to use for "locality" contrast_threshold : integer contrast threshold gthresh : numeric, optional global threshold to fall back in low contrast regions Returns ------- thresholded : binary ndarray See Also -------- bernsen : function Bernsen thresholding with a circular region ''' from mahotas.convolve import rank_filter fmax = rank_filter(f, se, se.sum() - 1) fmin = rank_filter(f, se, 0) fptp = fmax - fmin fmean = fmax / 2. + fmin / 2. # Do not use (fmax + fmin) as that may overflow return np.choose(fptp < contrast_threshold, (fmean < gthresh, fmean > f))
def test_rank_filter(): np.random.seed(22) A = np.random.random_integers(0,255, (32,32)) Bc = np.ones((3,3)) for r in xrange(9): B1 = rank_filter(A,Bc,r) B2 = _slow_rank_filter(A,r) assert np.all(B1[1:-1,1:-1] == B2)
def test_rank_filter(): np.random.seed(22) A = np.random.randint(0, 256, (32, 32)) Bc = np.ones((3, 3)) for r in range(9): B1 = rank_filter(A, Bc, r, mode='constant') B2 = _slow_rank_filter(A, r) assert np.all(B1 == B2)
def test_rank_filter(): np.random.seed(22) A = np.random.random_integers(0,255, (32,32)) Bc = np.ones((3,3)) for r in range(9): B1 = rank_filter(A, Bc, r, mode='constant') B2 = _slow_rank_filter(A,r) assert np.all(B1 == B2)