def test_threshold_otsu_image(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:-1, 1:-1] = True workspace, module = make_workspace(data, mask=mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.local_operation.value = cellprofiler.modules.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global, t_guide = module.get_threshold(image, workspace) t_guide_expected = skimage.filters.threshold_otsu(data[mask]) t_local_expected = module._get_adaptive_threshold(numpy.where(mask, data, numpy.nan), skimage.filters.threshold_otsu,) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected) numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
def test_threshold_otsu_uniform_data(): data = numpy.ones((10, 10), dtype=numpy.float32) data *= 0.2 workspace, module = make_workspace(data) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global, t_guide = module.get_threshold(image, workspace) t_guide_expected = 0.2 t_local_expected = module._get_adaptive_threshold(data, skimage.filters.threshold_otsu) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_array_almost_equal(t_local, t_local_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected)
def test_threshold_robust_background_adaptive(): numpy.random.seed(73) data = numpy.random.rand(10, 10) workspace, module = make_workspace(data, dimensions=2) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.adaptive_window_size.value = 3 module.local_operation.value = cellprofiler.modules.threshold.TM_ROBUST_BACKGROUND module.averaging_method.value = cellprofiler.modules.threshold.RB_MEAN module.variance_method.value = cellprofiler.modules.threshold.RB_SD t_local, t_uncorrected, t_guide = module.get_threshold(image, workspace) t_guide_expected = module.get_threshold_robust_background(data) t_guide_expected = module._correct_global_threshold(t_guide_expected) t_local_expected = module._get_adaptive_threshold(data, module.get_threshold_robust_background) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected) numpy.testing.assert_almost_equal(t_local, t_local_expected)
def test_threshold_li_adaptive_image(): numpy.random.seed(73) data = numpy.random.rand(10, 10) workspace, module = make_workspace(data) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.local_operation.value = cellprofiler.modules.threshold.TM_LI module.adaptive_window_size.value = 3 t_local, t_global, t_guide = module.get_threshold(image, workspace) t_guide_expected = skimage.filters.threshold_li(data) t_local_expected = module._get_adaptive_threshold(data, skimage.filters.threshold_li) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected) numpy.testing.assert_almost_equal(t_local, t_local_expected)
def test_threshold_sauvola_image_masked(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:3, 1:3] = True workspace, module = make_workspace(data, mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.local_operation.value = cellprofiler.modules.threshold.TM_SAUVOLA module.adaptive_window_size.value = 3 t_local, t_global, t_guide = module.get_threshold(image, workspace) t_guide_expected = skimage.filters.threshold_li(data[mask]) t_local_expected = skimage.filters.threshold_sauvola(numpy.where(mask, data, 0), window_size=3) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected) numpy.testing.assert_almost_equal(t_local, t_local_expected)
def test_threshold_otsu3_volume_log(): numpy.random.seed(73) data = numpy.random.rand(10, 10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:-1, 1:-1, 1:-1] = True workspace, module = make_workspace(data, mask=mask, dimensions=3) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.local_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_THREE_CLASS module.assign_middle_to_foreground.value = ( cellprofiler.modules.threshold.O_FOREGROUND) module.adaptive_window_size.value = 3 module.log_transform.value = True module.log_transform.value = True t_local, t_global, t_guide = module.get_threshold(image, workspace) transformed_data, d = centrosome.threshold.log_transform(data) t_guide_expected = skimage.filters.threshold_multiotsu( transformed_data[mask], nbins=128)[0] t_local_expected = numpy.zeros_like(data) masked = numpy.where(mask, transformed_data, numpy.nan) for index, plane in enumerate(masked): t_local_expected[index] = module._get_adaptive_threshold( plane, skimage.filters.threshold_multiotsu, nbins=128, ) t_guide_expected = centrosome.threshold.inverse_log_transform( t_guide_expected, d) t_local_expected = centrosome.threshold.inverse_log_transform( t_local_expected, d) t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected) numpy.testing.assert_almost_equal(t_guide, t_guide_expected, decimal=5) assert t_local.ndim == 3 numpy.testing.assert_array_almost_equal(t_local, t_local_expected)