Ejemplo n.º 1
0
 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)]))
Ejemplo n.º 2
0
 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)]))
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    def test_09_entropy3(self):
        '''Test 3-class entropy with three normal distributions'''

        r = np.random.RandomState()
        r.seed(8)
        x1 = r.normal(.2, .1, 10000)
        x2 = r.normal(.5, .25, 5000)
        x3 = r.normal(.8, .1, 5000)
        data = np.hstack((x1, x2, x3))
        data = data[(data > 0) & (data < 1)]
        threshold1, threshold2 = entropy3(data)
        self.assertTrue(threshold1 > .2)
        self.assertTrue(threshold1 < .5)
        self.assertTrue(threshold2 > .5)
        self.assertTrue(threshold2 < .8)
Ejemplo n.º 5
0
    def test_09_entropy3(self):
        '''Test 3-class entropy with three normal distributions'''

        r = np.random.RandomState()
        r.seed(8)
        x1 = r.normal(.2, .1, 10000)
        x2 = r.normal(.5, .25, 5000)
        x3 = r.normal(.8, .1, 5000)
        data = np.hstack((x1, x2, x3))
        data = data[(data > 0) & (data < 1)]
        threshold1, threshold2 = entropy3(data)
        self.assertTrue(threshold1 > .2)
        self.assertTrue(threshold1 < .5)
        self.assertTrue(threshold2 > .5)
        self.assertTrue(threshold2 < .8)
Ejemplo n.º 6
0
    def test_09_entropy3(self):
        """Test 3-class entropy with three normal distributions"""

        r = np.random.RandomState()
        r.seed(8)
        x1 = r.normal(0.2, 0.1, 10000)
        x2 = r.normal(0.5, 0.25, 5000)
        x3 = r.normal(0.8, 0.1, 5000)
        data = np.hstack((x1, x2, x3))
        data = data[(data > 0) & (data < 1)]
        threshold1, threshold2 = entropy3(data)
        self.assertTrue(threshold1 > 0.2)
        self.assertTrue(threshold1 < 0.5)
        self.assertTrue(threshold2 > 0.5)
        self.assertTrue(threshold2 < 0.8)
Ejemplo n.º 7
0
 def test_05_06_otsu3_entropy_high(self):
     '''Test the three-class otsu, entropy, middle = background'''
     np.random.seed(0)
     image = np.hstack((np.random.exponential(1.5, size=300),
                        np.random.poisson(15, size=300),
                        np.random.poisson(30, size=300)))
     image.shape = (30, 30)
     image = stretch(image)
     limage, d = T.log_transform(image)
     t1, t2 = entropy3(limage)
     threshold = T.inverse_log_transform(t1, 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_THREE_CLASS
     module.assign_middle_to_foreground.value = I.O_FOREGROUND
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertTrue(np.all(output.pixel_data == expected))
Ejemplo n.º 8
0
 def test_05_06_otsu3_entropy_high(self):
     '''Test the three-class otsu, entropy, middle = background'''
     np.random.seed(0)
     image = np.hstack(
         (np.random.exponential(1.5, size=300),
          np.random.poisson(15, size=300), np.random.poisson(30, size=300)))
     image.shape = (30, 30)
     image = stretch(image)
     limage, d = T.log_transform(image)
     t1, t2 = entropy3(limage)
     threshold = T.inverse_log_transform(t1, 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_THREE_CLASS
     module.assign_middle_to_foreground.value = I.O_FOREGROUND
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertTrue(np.all(output.pixel_data == expected))
Ejemplo n.º 9
0
 def test_05_05_otsu3_entropy_low(self):
     '''Test the three-class otsu, entropy, middle = background'''
     np.random.seed(0)
     image = np.hstack((np.random.exponential(1.5, size=300),
                        np.random.poisson(15, size=300),
                        np.random.poisson(30, size=300)))
     image.shape = (30, 30)
     image = stretch(image)
     limage, d = T.log_transform(image)
     t1, t2 = entropy3(limage)
     threshold = T.inverse_log_transform(t2, d)
     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_THREE_CLASS
     module.assign_middle_to_foreground.value = I.O_BACKGROUND
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     m = workspace.measurements
     m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()]
     self.assertAlmostEqual(m_threshold, threshold)
Ejemplo n.º 10
0
 def test_05_05_otsu3_entropy_low(self):
     '''Test the three-class otsu, entropy, middle = background'''
     np.random.seed(0)
     image = np.hstack((np.random.exponential(1.5,size=300),
                        np.random.poisson(15,size=300),
                        np.random.poisson(30,size=300)))
     image.shape=(30,30)
     image = stretch(image)
     limage, d = T.log_transform(image)
     t1,t2 = entropy3(limage)
     threshold = T.inverse_log_transform(t2, d)
     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_THREE_CLASS
     module.assign_middle_to_foreground.value = I.O_BACKGROUND
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     m = workspace.measurements
     m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()]
     self.assertAlmostEqual(m_threshold, threshold)