def test_06_NaN(self): """Regression test of Otsu with NaN in input (issue #624)""" r = np.random.RandomState() r.seed(6) data = r.uniform(size = 100) data[r.uniform(size = 100) > .8] = np.NaN self.assertEqual(otsu(data), otsu(data[~ np.isnan(data)])) self.assertEqual(entropy(data), entropy(data[~ np.isnan(data)])) self.assertEqual(otsu3(data), otsu3(data[~ np.isnan(data)])) self.assertEqual(entropy3(data), entropy3(data[~ np.isnan(data)]))
def get_otsu_threshold(image, mask = None, two_class_otsu = True, use_weighted_variance = True, assign_middle_to_foreground = True): if not mask is None: image = image[mask] else: image = np.array(image.flat) image = image[image >= 0] if len(image) == 0: return 1 image, d = log_transform(image) if two_class_otsu: if use_weighted_variance: threshold = otsu(image) else: threshold = entropy(image) else: if use_weighted_variance: t1, t2 = otsu3(image) else: t1,t2 = entropy3(image) threshold = t1 if assign_middle_to_foreground else t2 threshold = inverse_log_transform(threshold, d) return threshold
def test_07_entropy(self): '''Test entropy with two normal distributions''' r = np.random.RandomState() r.seed(7) x1 = r.normal(.2, .1, 10000) x2 = r.normal(.5, .25, 5000) data = np.hstack((x1, x2)) data = data[(data > 0) & (data < 1)] threshold = entropy(data) self.assertTrue(threshold > .2) self.assertTrue(threshold < .5)
def test_05_02_otsu_entropy(self): '''Test the entropy version of Otsu''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5, size=600), np.random.poisson(15, size=300))) image.shape = (30, 30) image = stretch(image) limage, d = T.log_transform(image) threshold = entropy(limage) threshold = T.inverse_log_transform(threshold, d) expected = image > threshold workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_scope.value = I.TS_GLOBAL module.threshold_method.value = T.TM_OTSU module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_TWO_CLASS module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) self.assertTrue(np.all(output.pixel_data == expected))
def test_05_02_otsu_entropy(self): '''Test the entropy version of Otsu''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5,size=600), np.random.poisson(15,size=300))) image.shape=(30,30) image = stretch(image) limage, d = T.log_transform(image) threshold = entropy(limage) threshold = T.inverse_log_transform(threshold, d) expected = image > threshold workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_scope.value = I.TS_GLOBAL module.threshold_method.value = T.TM_OTSU module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_TWO_CLASS module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) self.assertTrue(np.all(output.pixel_data == expected))