Exemple #1
0
def test_adapthist_grayscale_Nd():
    """
    Test for n-dimensional consistency with float images
    Note: Currently if img.ndim == 3, img.shape[2] > 4 must hold for the image
    not to be interpreted as a color image by @adapt_rgb
    """
    # take 2d image, subsample and stack it
    img = util.img_as_float(cp.asarray(data.astronaut()))
    img = rgb2gray(img)
    a = 15
    img2d = util.img_as_float(img[0:-1:a, 0:-1:a])
    img3d = cp.stack([img2d] * (img.shape[0] // a), axis=0)

    # apply CLAHE
    adapted2d = exposure.equalize_adapthist(img2d,
                                            kernel_size=5,
                                            clip_limit=0.05)
    adapted3d = exposure.equalize_adapthist(img3d,
                                            kernel_size=5,
                                            clip_limit=0.05)

    # check that dimensions of input and output match
    assert img2d.shape == adapted2d.shape
    assert img3d.shape == adapted3d.shape

    # check that the result from the stack of 2d images is similar
    # to the underlying 2d image
    assert (cp.mean(cp.abs(adapted2d - adapted3d[adapted3d.shape[0] // 2])) <
            0.02)
Exemple #2
0
def test_adapthist_clip_limit():
    img_u = data.moon()
    img_f = util.img_as_float(img_u)

    # uint8 input
    img_clahe = exposure.equalize_adapthist(img_u, clip_limit=1)
    assert_array_equal(img_f, img_clahe)

    # float64 input
    img_clahe = exposure.equalize_adapthist(img_f, clip_limit=1)
    assert_array_equal(img_f, img_clahe)
Exemple #3
0
def test_adapthist_constant():
    """Test constant image, float and uint"""
    img = cp.zeros((8, 8))
    img += 2
    img = img.astype(np.uint16)
    adapted = exposure.equalize_adapthist(img, 3)
    assert cp.min(adapted) == cp.max(adapted)

    img = cp.zeros((8, 8))
    img += 0.1
    img = img.astype(np.float64)
    adapted = exposure.equalize_adapthist(img, 3)
    assert cp.min(adapted) == cp.max(adapted)
Exemple #4
0
def test_adapthist_grayscale():
    """Test a grayscale float image"""
    img = util.img_as_float(data.astronaut())
    img = rgb2gray(img)
    img = cp.dstack((img, img, img))
    adapted = exposure.equalize_adapthist(img,
                                          kernel_size=(57, 51),
                                          clip_limit=0.01,
                                          nbins=128)
    assert img.shape == adapted.shape
    assert_almost_equal(float(peak_snr(img, adapted)), 100.140, 3)
    assert_almost_equal(float(norm_brightness_err(img, adapted)), 0.0529, 3)
Exemple #5
0
def test_adapthist_alpha():
    """Test an RGBA color image"""
    img = util.img_as_float(data.astronaut())
    alpha = cp.ones((img.shape[0], img.shape[1]), dtype=float)
    img = cp.dstack((img, alpha))
    adapted = exposure.equalize_adapthist(img)
    assert adapted.shape != img.shape
    img = img[:, :, :3]
    full_scale = exposure.rescale_intensity(img)
    assert img.shape == adapted.shape
    assert_almost_equal(float(peak_snr(full_scale, adapted)), 109.393, 2)
    assert_almost_equal(float(norm_brightness_err(full_scale, adapted)),
                        0.0248, 3)
Exemple #6
0
def test_adapthist_color():
    """Test an RGB color uint16 image"""
    img = util.img_as_uint(data.astronaut())
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        hist, bin_centers = exposure.histogram(img)
        assert len(w) > 0
    adapted = exposure.equalize_adapthist(img, clip_limit=0.01)

    assert adapted.min() == 0
    assert adapted.max() == 1.0
    assert img.shape == adapted.shape
    full_scale = exposure.rescale_intensity(img)
    assert_almost_equal(float(peak_snr(full_scale, adapted)), 109.393, 1)
    assert_almost_equal(float(norm_brightness_err(full_scale, adapted)), 0.02,
                        2)
    return data, adapted
Exemple #7
0
def test_adapthist_borders():
    """Test border processing"""
    img = rgb2gray(cp.asarray(util.img_as_float(data.astronaut())))

    # maximize difference between orig and processed img
    img /= 100.0
    img[img.shape[0] // 2, img.shape[1] // 2] = 1.0

    # check borders are processed for different kernel sizes
    border_index = -1
    for kernel_size in range(51, 71, 2):
        adapted = exposure.equalize_adapthist(img, kernel_size, clip_limit=0.5)
        # Check last columns are processed
        assert (norm_brightness_err(adapted[:, border_index],
                                    img[:, border_index]) > 0.1)
        # Check last rows are processed
        assert (norm_brightness_err(adapted[border_index, :],
                                    img[border_index, :]) > 0.1)