示例#1
0
def test_threshold_minimum():
    camera = util.img_as_ubyte(camerad)

    threshold = threshold_minimum(camera)
    assert_array_equal(threshold, 85)

    astronaut = util.img_as_ubyte(astronautd)
    threshold = threshold_minimum(astronaut)
    assert_array_equal(threshold, 114)
示例#2
0
def test_li_astro_image():
    image = util.img_as_ubyte(astronautd)
    threshold = threshold_li(image)
    ce_actual = _cross_entropy(image, threshold)
    assert 64 < threshold < 65
    assert ce_actual < _cross_entropy(image, threshold + 1)
    assert ce_actual < _cross_entropy(image, threshold - 1)
示例#3
0
def test_li_camera_image():
    image = util.img_as_ubyte(camerad)
    threshold = threshold_li(image)
    ce_actual = _cross_entropy(image, threshold)
    assert 78 < threshold_li(image) < 79
    assert ce_actual < _cross_entropy(image, threshold + 1)
    assert ce_actual < _cross_entropy(image, threshold - 1)
示例#4
0
def test_gray2rgba_dtype():
    img_f64 = cp.random.random((5, 5))
    img_f32 = img_f64.astype('float32')
    img_u8 = img_as_ubyte(img_f64)
    img_int = img_u8.astype(int)

    for img in [img_f64, img_f32, img_u8, img_int]:
        assert gray2rgba(img).dtype == img.dtype
示例#5
0
def test_isodata_camera_image():
    camera = util.img_as_ubyte(camerad)

    threshold = threshold_isodata(camera)
    assert np.floor((camera[camera <= threshold].mean() +
                     camera[camera > threshold].mean()) / 2.0) == threshold
    assert threshold == 102

    assert_array_equal(threshold_isodata(camera, return_all=True), [102, 103])
示例#6
0
def test_isodata_coins_image():
    coins = util.img_as_ubyte(coinsd)

    threshold = threshold_isodata(coins)
    assert np.floor((coins[coins <= threshold].mean() +
                     coins[coins > threshold].mean()) / 2.0) == threshold
    assert threshold == 107

    assert_array_equal(threshold_isodata(coins, return_all=True), [107])
示例#7
0
def test_isodata_moon_image_negative_float():
    moon = util.img_as_ubyte(moond).astype(cp.float64)
    moon -= 100

    assert -14 < threshold_isodata(moon) < -13

    thresholds = threshold_isodata(moon, return_all=True)
    # fmt: off
    assert_array_almost_equal(thresholds,
        [-13.83789062, -12.84179688, -11.84570312, 22.02148438,   # noqa
          23.01757812,  24.01367188,  38.95507812, 39.95117188])  # noqa
示例#8
0
def test_li_coins_image():
    image = util.img_as_ubyte(coinsd)
    threshold = threshold_li(image)
    ce_actual = _cross_entropy(image, threshold)
    assert 94 < threshold_li(image) < 95
    assert ce_actual < _cross_entropy(image, threshold + 1)
    # in the case of the coins image, the minimum cross-entropy is achieved one
    # threshold below that found by the iterative method. Not sure why that is
    # but `threshold_li` does find the stationary point of the function (ie the
    # tolerance can be reduced arbitrarily but the exact same threshold is
    # found), so my guess is some kind of histogram binning effect.
    assert ce_actual < _cross_entropy(image, threshold - 2)
示例#9
0
def test_isodata_moon_image():
    moon = util.img_as_ubyte(moond)

    threshold = threshold_isodata(moon)
    assert np.floor((moon[moon <= threshold].mean() +
                     moon[moon > threshold].mean()) / 2.0) == threshold
    assert threshold == 86

    thresholds = threshold_isodata(moon, return_all=True)
    for threshold in thresholds:
        assert np.floor((moon[moon <= threshold].mean() +
                         moon[moon > threshold].mean()) / 2.0) == threshold
    assert_array_equal(thresholds, [86, 87, 88, 122, 123, 124, 139, 140])
示例#10
0
def test_isodata_moon_image_negative_int():
    moon = util.img_as_ubyte(moond).astype(cp.int32)
    moon -= 100

    threshold = threshold_isodata(moon)
    assert np.floor((moon[moon <= threshold].mean() +
                     moon[moon > threshold].mean()) / 2.0) == threshold
    assert threshold == -14

    thresholds = threshold_isodata(moon, return_all=True)
    for threshold in thresholds:
        assert np.floor((moon[moon <= threshold].mean() +
                         moon[moon > threshold].mean()) / 2.0) == threshold
    assert_array_equal(thresholds, [-14, -13, -12, 22, 23, 24, 39, 40])
示例#11
0
    def _build_expected_output(self):
        funcs = (grey.erosion, grey.dilation, grey.opening, grey.closing,
                 grey.white_tophat, grey.black_tophat)
        selems_2D = (selem.square, selem.diamond, selem.disk, selem.star)

        image = img_as_ubyte(
            transform.downscale_local_mean(
                color.rgb2gray(cp.array(data.coffee())), (20, 20)))

        output = {}
        for n in range(1, 4):
            for strel in selems_2D:
                for func in funcs:
                    key = '{0}_{1}_{2}'.format(strel.__name__, n,
                                               func.__name__)
                    output[key] = func(image, strel(n))

        return output
示例#12
0
def test_gray2rgba_alpha():
    img = cp.random.random((5, 5))
    img_u8 = img_as_ubyte(img)

    # Default
    alpha = None
    rgba = gray2rgba(img, alpha)

    assert_array_equal(rgba[..., :3], gray2rgb(img))
    assert_array_equal(rgba[..., 3], 1.0)

    # Scalar
    alpha = 0.5
    rgba = gray2rgba(img, alpha)

    assert_array_equal(rgba[..., :3], gray2rgb(img))
    assert_array_equal(rgba[..., 3], alpha)

    # Array
    alpha = cp.random.random((5, 5))
    rgba = gray2rgba(img, alpha)

    assert_array_equal(rgba[..., :3], gray2rgb(img))
    assert_array_equal(rgba[..., 3], alpha)

    # Warning about alpha cast
    alpha = 0.5
    with expected_warnings(["alpha can't be safely cast to image dtype"]):
        rgba = gray2rgba(img_u8, alpha)
        assert_array_equal(rgba[..., :3], gray2rgb(img_u8))

    # Invalid shape
    alpha = cp.random.random((5, 5, 1))
    # expected_err_msg = ("could not broadcast input array from shape (5,5,1) "
    #                     "into shape (5,5)")

    with pytest.raises(ValueError):  # as err:
        rgba = gray2rgba(img, alpha)
示例#13
0
def test_threshold_minimum_histogram():
    camera = util.img_as_ubyte(camerad)
    hist = histogram(camera.ravel(), 256, source_range='image')
    threshold = threshold_minimum(hist=hist)
    assert_array_equal(threshold, 85)
示例#14
0
def test_otsu_camera_image():
    camera = util.img_as_ubyte(camerad)
    assert 101 < threshold_otsu(camera) < 103
示例#15
0
def test_isodata_camera_image_histogram():
    camera = util.img_as_ubyte(camerad)
    hist = histogram(camera.ravel(), 256, source_range='image')
    threshold = threshold_isodata(hist=hist)
    assert threshold == 102
示例#16
0
def test_isodata_camera_image_counts():
    camera = util.img_as_ubyte(camerad)
    counts, bin_centers = histogram(camera.ravel(), 256, source_range='image')
    threshold = threshold_isodata(hist=counts)
    assert threshold == 102
示例#17
0
def test_yen_coins_image():
    coins = util.img_as_ubyte(coinsd)
    assert 109 < threshold_yen(coins) < 111
示例#18
0
def test_otsu_camera_image_histogram():
    camera = util.img_as_ubyte(camerad)
    hist = histogram(camera.ravel(), 256, source_range="image")
    assert 101 < threshold_otsu(hist=hist) < 103
示例#19
0
def test_yen_camera_image_counts():
    camera = util.img_as_ubyte(camerad)
    counts, bin_centers = histogram(camera.ravel(), 256, source_range='image')
    assert 145 < threshold_yen(hist=counts) < 147
示例#20
0
 def test_hed_rgb_roundtrip(self):
     img_rgb = img_as_ubyte(self.img_rgb)
     new = img_as_ubyte(hed2rgb(rgb2hed(img_rgb)))
     assert_array_equal(new, img_rgb)
示例#21
0
def test_otsu_camera_image_counts():
    camera = util.img_as_ubyte(camerad)
    counts, bin_centers = histogram(camera.ravel(), 256, source_range="image")
    assert 101 < threshold_otsu(hist=counts) < 103
示例#22
0
def test_otsu_coins_image():
    coins = util.img_as_ubyte(coinsd)
    assert 106 < threshold_otsu(coins) < 108
示例#23
0
def test_otsu_astro_image():
    img = util.img_as_ubyte(astronautd)
    with expected_warnings(['grayscale']):
        assert 109 < threshold_otsu(img) < 111
示例#24
0
def test_multiotsu_astro_image():
    img = util.img_as_ubyte(astronautd)
    with expected_warnings(['grayscale']):
        assert_array_almost_equal(threshold_multiotsu(img), [58, 149])
示例#25
0
 def test_hdx_rgb_roundtrip(self):
     from cucim.skimage.color.colorconv import hdx_from_rgb, rgb_from_hdx
     img_rgb = self.img_rgb
     conv = combine_stains(separate_stains(img_rgb, hdx_from_rgb),
                           rgb_from_hdx)
     assert_array_equal(img_as_ubyte(conv), img_rgb)
示例#26
0
def test_threshold_minimum_counts():
    camera = util.img_as_ubyte(camerad)
    counts, bin_centers = histogram(camera.ravel(), 256, source_range='image')
    threshold = threshold_minimum(hist=counts)
    assert_array_equal(threshold, 85)
示例#27
0
def test_yen_camera_image():
    camera = util.img_as_ubyte(camerad)
    assert 145 < threshold_yen(camera) < 147
示例#28
0
def test_yen_camera_image_histogram():
    camera = util.img_as_ubyte(camerad)
    hist = histogram(camera.ravel(), 256, source_range="image")
    assert 145 < threshold_yen(hist=hist) < 147
示例#29
0
def test_equalize_ubyte():
    img = util.img_as_ubyte(test_img)
    img_eq = exposure.equalize_hist(img)

    cdf, bin_edges = exposure.cumulative_distribution(img_eq)
    check_cdf_slope(cdf)