def test_04_03_robust_background_upper_outliers(self): img = self.make_mog_image(.1, .05, .5, .2, .9, (45, 35)) t0 = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, upper_outlier_fraction=0) t05 = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, upper_outlier_fraction=0.05) self.assertNotEqual(t0, t05)
def test_04_04_robust_background_sd(self): img = self.make_mog_image(.5, .1, .8, .01, .99, (45, 35)) t2 = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, lower_outlier_fraction = 0, upper_outlier_fraction = 0) self.assertLess(abs(t2 - .7), .02) t3 = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, lower_outlier_fraction = 0, upper_outlier_fraction = 0, deviations_above_average = 2.5) self.assertLess(abs(t3 - .75), .02)
def test_03_02_adaptive_threshold_different(self): r = np.random.RandomState() r.seed(31) block = r.uniform(size=(10,10)) i,j = np.mgrid[0:10:2,0:10:2] block[i,j] *= .5 i,j = np.mgrid[0:50,0:50] img = block[i%10, j%10] * .5 # # Make the middle higher in intensity # img[20:30, 20:30] *= 2 global_threshold = T.get_global_threshold(T.TM_OTSU, block) adaptive_threshold = T.get_adaptive_threshold( T.TM_OTSU, img, global_threshold, adaptive_window_size = 10) # # Check that the gradients are positive for i,j<15 and negative # for i,j>=15 # gradient = convolve1d(adaptive_threshold, [-1, 0, 1], 0) self.assertTrue(np.all(gradient[20:25, 20:30] < 0)) self.assertTrue(np.all(gradient[25:30, 20:30] > 0)) gradient = convolve1d(adaptive_threshold, [-1, 0, 1], 1) self.assertTrue(np.all(gradient[20:30, 20:25] < 0)) self.assertTrue(np.all(gradient[20:30, 25:30] > 0))
def test_04_05_robust_background_median(self): img = self.make_mog_image(.3, .05, .5, .2, .9, (45, 35)) t = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, average_fn = np.median, deviations_above_average = 0, lower_outlier_fraction = 0, upper_outlier_fraction = 0) self.assertLess(abs(t - .3), .01)
def test_04_06_robust_background_mode(self): img = self.make_mog_image(.3, .05, .5, .2, .9, (45, 35)) img[(img > .25) & (img < .35)] = .304 t = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, average_fn = T.binned_mode, deviations_above_average = 0, lower_outlier_fraction = 0, upper_outlier_fraction = 0) self.assertAlmostEqual(t, .304)
def test_04_08_mad(self): img = self.make_mog_image(.3, .05, .5, .2, .95, (45, 35)) t = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img, variance_fn = T.mad, deviations_above_average = 2, lower_outlier_fraction = 0, upper_outlier_fraction = 0) norm = scipy.stats.norm(0, .05) # the MAD should be the expected value at the 75th percentile expected = .3 + 2 * norm.ppf(.75) self.assertLess(np.abs(t - expected), .02)
def test_03_01_adaptive_threshold_same(self): r = np.random.RandomState() r.seed(31) block = r.uniform(size=(10,10)) i,j = np.mgrid[0:10:2,0:10:2] block[i,j] *= .5 i,j = np.mgrid[0:50,0:50] img = block[i%10, j%10] global_threshold = T.get_global_threshold(T.TM_OTSU, block) adaptive_threshold = T.get_adaptive_threshold( T.TM_OTSU, img, global_threshold, adaptive_window_size = 10) np.testing.assert_almost_equal(adaptive_threshold, global_threshold)
def test_04_01_robust_background(self): img = self.make_mog_image(.1, .05, .5, .2, .975, (45, 35)) t = T.get_global_threshold(T.TM_ROBUST_BACKGROUND, img) self.assertLess(abs(t-.2), .025)