def test_sum():
    # check the number of valid pixels in the neighborhood

    image8 = np.array(
        [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8
    )
    image16 = 400 * np.array(
        [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint16
    )
    elem = np.ones((3, 3), dtype=np.uint8)
    out8 = np.empty_like(image8)
    out16 = np.empty_like(image16)
    mask = np.ones(image8.shape, dtype=np.uint8)

    r = np.array([[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], [3, 6, 9, 6, 3], [2, 4, 6, 4, 2], [1, 2, 3, 2, 1]], dtype=np.uint8)
    rank.sum(image=image8, selem=elem, out=out8, mask=mask)
    assert_equal(r, out8)
    rank.sum_percentile(image=image8, selem=elem, out=out8, mask=mask, p0=0.0, p1=1.0)
    assert_equal(r, out8)
    rank.sum_bilateral(image=image8, selem=elem, out=out8, mask=mask, s0=255, s1=255)
    assert_equal(r, out8)

    r = 400 * np.array(
        [[1, 2, 3, 2, 1], [2, 4, 6, 4, 2], [3, 6, 9, 6, 3], [2, 4, 6, 4, 2], [1, 2, 3, 2, 1]], dtype=np.uint16
    )
    rank.sum(image=image16, selem=elem, out=out16, mask=mask)
    assert_equal(r, out16)
    rank.sum_percentile(image=image16, selem=elem, out=out16, mask=mask, p0=0.0, p1=1.0)
    assert_equal(r, out16)
    rank.sum_bilateral(image=image16, selem=elem, out=out16, mask=mask, s0=1000, s1=1000)
    assert_equal(r, out16)
Exemple #2
0
def moving_window( dark_arr, win_size=3 ):
    """
    Find average value of pixels in a square window. Used on a boolean array,
    this can be used to find the percentage of pixels marked as `True`.

    Parameters
    ----------
    dark_arr : boolean array
        This is an (RxCx1) array of pixels that are considered dark. The
        `dark_pixels` method in this module can be used to create this array.
    win_size : int (Default value = 3)
        The size of the moving window to be used. Lyzenga et al. 2006 uses a
        3x3 window so the default is 3.

    Returns
    -------
    array
        An array the same shape as `dark_arr` with values representing the
        proportion of pixels in the surrounding window that are `True` in
        `dark_arr`.
    """
    win = morphology.square( win_size )
    npix = win.size
    if np.ma.is_masked( dark_arr ):
        outarr = rank.sum( dark_arr.filled().astype('uint8'), win ) / float( npix )
        outarr = np.ma.MaskedArray( outarr, mask=dark_arr.mask, fill_value=dark_arr.fill_value )
    else:
        outarr = rank.sum( dark_arr.astype('uint8'), win ) / float( npix )
    return outarr
def check_all():
    np.random.seed(0)
    image = np.random.rand(25, 25)
    selem = morphology.disk(1)
    refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz"))

    assert_equal(refs["autolevel"], rank.autolevel(image, selem))
    assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(image, selem))
    assert_equal(refs["bottomhat"], rank.bottomhat(image, selem))
    assert_equal(refs["equalize"], rank.equalize(image, selem))
    assert_equal(refs["gradient"], rank.gradient(image, selem))
    assert_equal(refs["gradient_percentile"], rank.gradient_percentile(image, selem))
    assert_equal(refs["maximum"], rank.maximum(image, selem))
    assert_equal(refs["mean"], rank.mean(image, selem))
    assert_equal(refs["mean_percentile"], rank.mean_percentile(image, selem))
    assert_equal(refs["mean_bilateral"], rank.mean_bilateral(image, selem))
    assert_equal(refs["subtract_mean"], rank.subtract_mean(image, selem))
    assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(image, selem))
    assert_equal(refs["median"], rank.median(image, selem))
    assert_equal(refs["minimum"], rank.minimum(image, selem))
    assert_equal(refs["modal"], rank.modal(image, selem))
    assert_equal(refs["enhance_contrast"], rank.enhance_contrast(image, selem))
    assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(image, selem))
    assert_equal(refs["pop"], rank.pop(image, selem))
    assert_equal(refs["pop_percentile"], rank.pop_percentile(image, selem))
    assert_equal(refs["pop_bilateral"], rank.pop_bilateral(image, selem))
    assert_equal(refs["sum"], rank.sum(image, selem))
    assert_equal(refs["sum_bilateral"], rank.sum_bilateral(image, selem))
    assert_equal(refs["sum_percentile"], rank.sum_percentile(image, selem))
    assert_equal(refs["threshold"], rank.threshold(image, selem))
    assert_equal(refs["threshold_percentile"], rank.threshold_percentile(image, selem))
    assert_equal(refs["tophat"], rank.tophat(image, selem))
    assert_equal(refs["noise_filter"], rank.noise_filter(image, selem))
    assert_equal(refs["entropy"], rank.entropy(image, selem))
    assert_equal(refs["otsu"], rank.otsu(image, selem))
    assert_equal(refs["percentile"], rank.percentile(image, selem))
    assert_equal(refs["windowed_histogram"], rank.windowed_histogram(image, selem))