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 run(self, workspace): image = workspace.image_set.get_image(self.image_name.value, must_be_grayscale=True) orig_pixels = image.pixel_data if image.has_mask: mask = image.mask else: mask = np.ones(orig_pixels.shape, bool) if self.method == M_SOBEL: if self.direction == E_ALL: output_pixels = sobel(orig_pixels, mask) elif self.direction == E_HORIZONTAL: output_pixels = hsobel(orig_pixels, mask) elif self.direction == E_VERTICAL: output_pixels = vsobel(orig_pixels, mask) else: raise NotImplementedError( "Unimplemented direction for Sobel: %s", self.direction.value) elif self.method == M_LOG: sigma = self.get_sigma() size = int(sigma * 4) + 1 output_pixels = laplacian_of_gaussian(orig_pixels, mask, size, sigma) elif self.method == M_PREWITT: if self.direction == E_ALL: output_pixels = prewitt(orig_pixels) elif self.direction == E_HORIZONTAL: output_pixels = hprewitt(orig_pixels, mask) elif self.direction == E_VERTICAL: output_pixels = vprewitt(orig_pixels, mask) else: raise NotImplementedError( "Unimplemented direction for Prewitt: %s", self.direction.value) elif self.method == M_CANNY: high_threshold = self.manual_threshold.value low_threshold = self.low_threshold.value if (self.wants_automatic_low_threshold.value or self.wants_automatic_threshold.value): sobel_image = sobel(orig_pixels, mask) low, high = otsu3(sobel_image[mask]) if self.wants_automatic_low_threshold.value: low_threshold = low * self.threshold_adjustment_factor.value if self.wants_automatic_threshold.value: high_threshold = high * self.threshold_adjustment_factor.value output_pixels = canny(orig_pixels, mask, self.get_sigma(), low_threshold, high_threshold) elif self.method == M_ROBERTS: output_pixels = roberts(orig_pixels, mask) else: raise NotImplementedError( "Unimplemented edge detection method: %s" % self.method.value) output_image = cpi.Image(output_pixels, parent_image=image) workspace.image_set.add(self.output_image_name.value, output_image) if self.show_window: workspace.display_data.orig_pixels = orig_pixels workspace.display_data.output_pixels = output_pixels
def run(self, workspace): image = workspace.image_set.get_image(self.image_name.value, must_be_grayscale=True) orig_pixels = image.pixel_data if image.has_mask: mask = image.mask else: mask = np.ones(orig_pixels.shape, bool) if self.method == M_SOBEL: if self.direction == E_ALL: output_pixels = sobel(orig_pixels, mask) elif self.direction == E_HORIZONTAL: output_pixels = hsobel(orig_pixels, mask) elif self.direction == E_VERTICAL: output_pixels = vsobel(orig_pixels, mask) else: raise NotImplementedError("Unimplemented direction for Sobel: %s", self.direction.value) elif self.method == M_LOG: sigma = self.get_sigma() size = int(sigma * 4) + 1 output_pixels = laplacian_of_gaussian(orig_pixels, mask, size, sigma) elif self.method == M_PREWITT: if self.direction == E_ALL: output_pixels = prewitt(orig_pixels) elif self.direction == E_HORIZONTAL: output_pixels = hprewitt(orig_pixels, mask) elif self.direction == E_VERTICAL: output_pixels = vprewitt(orig_pixels, mask) else: raise NotImplementedError("Unimplemented direction for Prewitt: %s", self.direction.value) elif self.method == M_CANNY: high_threshold = self.manual_threshold.value low_threshold = self.low_threshold.value if (self.wants_automatic_low_threshold.value or self.wants_automatic_threshold.value): sobel_image = sobel(orig_pixels, mask) low, high = otsu3(sobel_image[mask]) if self.wants_automatic_low_threshold.value: low_threshold = low * self.threshold_adjustment_factor.value if self.wants_automatic_threshold.value: high_threshold = high * self.threshold_adjustment_factor.value output_pixels = canny(orig_pixels, mask, self.get_sigma(), low_threshold, high_threshold) elif self.method == M_ROBERTS: output_pixels = roberts(orig_pixels, mask) elif self.method == M_KIRSCH: output_pixels = kirsch(orig_pixels) else: raise NotImplementedError("Unimplemented edge detection method: %s" % self.method.value) output_image = cpi.Image(output_pixels, parent_image=image) workspace.image_set.add(self.output_image_name.value, output_image) if self.show_window: workspace.display_data.orig_pixels = orig_pixels workspace.display_data.output_pixels = output_pixels
def test_08_otsu3(self): '''Test 3-class Otsu 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 = otsu3(data) self.assertTrue(threshold1 > .2) self.assertTrue(threshold1 < .5) self.assertTrue(threshold2 > .5) self.assertTrue(threshold2 < .8)
def test_08_otsu3(self): """Test 3-class Otsu 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 = otsu3(data) self.assertTrue(threshold1 > 0.2) self.assertTrue(threshold1 < 0.5) self.assertTrue(threshold2 > 0.5) self.assertTrue(threshold2 < 0.8)
def test_06_01_canny(self): '''Test the canny method''' i, j = np.mgrid[-20:20, -20:20] image = np.logical_and(i > j, i ** 2 + j ** 2 < 300).astype(np.float32) np.random.seed(0) image = image * .5 + np.random.uniform(size=image.shape) * .3 image = np.ascontiguousarray(image, np.float32) workspace, module = self.make_workspace(image) module.method.value = F.M_CANNY module.wants_automatic_threshold.value = True module.wants_automatic_low_threshold.value = True module.wants_automatic_sigma.value = True module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) t1, t2 = otsu3(FIL.sobel(image)) result = FIL.canny(image, np.ones(image.shape, bool), 1.0, t1, t2) self.assertTrue(np.all(output.pixel_data == result))
def test_06_01_canny(self): '''Test the canny method''' i, j = np.mgrid[-20:20, -20:20] image = np.logical_and(i > j, i**2 + j**2 < 300).astype(np.float32) np.random.seed(0) image = image * .5 + np.random.uniform(size=image.shape) * .3 image = np.ascontiguousarray(image, np.float32) workspace, module = self.make_workspace(image) module.method.value = F.M_CANNY module.wants_automatic_threshold.value = True module.wants_automatic_low_threshold.value = True module.wants_automatic_sigma.value = True module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) t1, t2 = otsu3(FIL.sobel(image)) result = FIL.canny(image, np.ones(image.shape, bool), 1.0, t1, t2) self.assertTrue(np.all(output.pixel_data == result))
def test_05_04_otsu3_wv_high(self): '''Test the three-class otsu, weighted variance middle = foreground''' 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 = otsu3(limage) threshold = T.inverse_log_transform(t1, 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_WEIGHTED_VARIANCE module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_FOREGROUND module.run(workspace) m = workspace.measurements m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()] self.assertAlmostEqual(m_threshold, threshold)
def test_05_04_otsu3_wv_high(self): '''Test the three-class otsu, weighted variance middle = foreground''' 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 = otsu3(limage) threshold = T.inverse_log_transform(t1, 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_WEIGHTED_VARIANCE module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_FOREGROUND module.run(workspace) m = workspace.measurements m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()] self.assertAlmostEqual(m_threshold, threshold)