示例#1
0
 def test_reorder_labels(self):
     image = cp.asarray(np.random.uniform(size=(40, 60)))
     i, j = cp.mgrid[0:40, 0:60]
     labels = 1 + (i >= 20) + (j >= 30) * 2
     labels[labels == 4] = 5
     i, j = cp.mgrid[-3:4, -3:4]
     footprint = i * i + j * j <= 9
     expected = cp.zeros(image.shape, float)
     for imin, imax in ((0, 20), (20, 40)):
         for jmin, jmax in ((0, 30), (30, 60)):
             expected[imin:imax, jmin:jmax] = ndi.maximum_filter(
                 image[imin:imax, jmin:jmax], footprint=footprint
             )
     expected = expected == image
     with expected_warnings(["indices argument is deprecated"]):
         result = peak.peak_local_max(
             image,
             labels=labels,
             min_distance=1,
             threshold_rel=0,
             footprint=footprint,
             indices=False,
             exclude_border=False,
         )
     assert (result == expected).all()
示例#2
0
 def test_indices_with_labels(self):
     image = cp.asarray(np.random.uniform(size=(40, 60)))
     i, j = cp.mgrid[0:40, 0:60]
     labels = 1 + (i >= 20) + (j >= 30) * 2
     i, j = cp.mgrid[-3:4, -3:4]
     footprint = i * i + j * j <= 9
     expected = cp.zeros(image.shape, float)
     for imin, imax in ((0, 20), (20, 40)):
         for jmin, jmax in ((0, 30), (30, 60)):
             expected[imin:imax,
                      jmin:jmax] = ndi.maximum_filter(image[imin:imax,
                                                            jmin:jmax],
                                                      footprint=footprint)
     expected = cp.column_stack(cp.nonzero(expected == image))
     expected = expected[cp.argsort(image[tuple(expected.T)])[::-1]]
     result = peak.peak_local_max(
         image,
         labels=labels,
         min_distance=1,
         threshold_rel=0,
         footprint=footprint,
         indices=True,
         exclude_border=False,
     )
     result = result[cp.argsort(image[tuple(result.T)])[::-1]]
     assert (result == expected).all()
示例#3
0
def _get_peak_mask(image, min_distance, footprint, threshold_abs,
                   threshold_rel):
    """
    Return the mask containing all peak candidates above thresholds.
    """
    if footprint is not None:
        image_max = ndi.maximum_filter(image,
                                       footprint=footprint,
                                       mode="constant")
    else:
        size = 2 * min_distance + 1
        image_max = ndi.maximum_filter(image, size=size, mode="constant")
    mask = image == image_max
    if threshold_rel is not None:
        threshold = max(threshold_abs, threshold_rel * image.max())
    else:
        threshold = threshold_abs
    mask &= image > threshold
    return mask
示例#4
0
def test_multiple_modes():
    # Test that the filters with multiple mode cababilities for different
    # dimensions give the same result as applying a single mode.
    arr = cp.array([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 0.0]])

    mode1 = "reflect"
    mode2 = ["reflect", "reflect"]

    assert_array_equal(
        sndi.gaussian_filter(arr, 1, mode=mode1),
        sndi.gaussian_filter(arr, 1, mode=mode2),
    )
    assert_array_equal(sndi.prewitt(arr, mode=mode1),
                       sndi.prewitt(arr, mode=mode2))
    assert_array_equal(sndi.sobel(arr, mode=mode1), sndi.sobel(arr,
                                                               mode=mode2))
    assert_array_equal(sndi.laplace(arr, mode=mode1),
                       sndi.laplace(arr, mode=mode2))
    assert_array_equal(
        sndi.gaussian_laplace(arr, 1, mode=mode1),
        sndi.gaussian_laplace(arr, 1, mode=mode2),
    )
    assert_array_equal(
        sndi.maximum_filter(arr, size=5, mode=mode1),
        sndi.maximum_filter(arr, size=5, mode=mode2),
    )
    assert_array_equal(
        sndi.minimum_filter(arr, size=5, mode=mode1),
        sndi.minimum_filter(arr, size=5, mode=mode2),
    )
    assert_array_equal(
        sndi.gaussian_gradient_magnitude(arr, 1, mode=mode1),
        sndi.gaussian_gradient_magnitude(arr, 1, mode=mode2),
    )
    assert_array_equal(
        sndi.uniform_filter(arr, 5, mode=mode1),
        sndi.uniform_filter(arr, 5, mode=mode2),
    )
示例#5
0
def test_multiple_modes_sequentially():
    # Test that the filters with multiple mode cababilities for different
    # dimensions give the same result as applying the filters with
    # different modes sequentially
    arr = cp.array([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 0.0]])

    modes = ["reflect", "wrap"]

    expected = sndi.gaussian_filter1d(arr, 1, axis=0, mode=modes[0])
    expected = sndi.gaussian_filter1d(expected, 1, axis=1, mode=modes[1])
    assert_array_equal(expected, sndi.gaussian_filter(arr, 1, mode=modes))

    expected = sndi.uniform_filter1d(arr, 5, axis=0, mode=modes[0])
    expected = sndi.uniform_filter1d(expected, 5, axis=1, mode=modes[1])
    assert_array_equal(expected, sndi.uniform_filter(arr, 5, mode=modes))

    expected = sndi.maximum_filter1d(arr, size=5, axis=0, mode=modes[0])
    expected = sndi.maximum_filter1d(expected, size=5, axis=1, mode=modes[1])
    assert_array_equal(expected, sndi.maximum_filter(arr, size=5, mode=modes))

    expected = sndi.minimum_filter1d(arr, size=5, axis=0, mode=modes[0])
    expected = sndi.minimum_filter1d(expected, size=5, axis=1, mode=modes[1])
    assert_array_equal(expected, sndi.minimum_filter(arr, size=5, mode=modes))
示例#6
0
 def test_four_quadrants(self):
     image = cp.asarray(np.random.uniform(size=(20, 30)))
     i, j = cp.mgrid[0:20, 0:30]
     labels = 1 + (i >= 10) + (j >= 15) * 2
     i, j = cp.mgrid[-3:4, -3:4]
     footprint = i * i + j * j <= 9
     expected = cp.zeros(image.shape, float)
     for imin, imax in ((0, 10), (10, 20)):
         for jmin, jmax in ((0, 15), (15, 30)):
             expected[imin:imax, jmin:jmax] = ndi.maximum_filter(
                 image[imin:imax, jmin:jmax], footprint=footprint
             )
     expected = expected == image
     with expected_warnings(["indices argument is deprecated"]):
         result = peak.peak_local_max(
             image,
             labels=labels,
             footprint=footprint,
             min_distance=1,
             threshold_rel=0,
             indices=False,
             exclude_border=False,
         )
     assert cp.all(result == expected)
示例#7
0
文件: peak.py 项目: mritools/cupyimg
def _get_peak_mask(image, footprint, threshold, mask=None):
    """
    Return the mask containing all peak candidates above thresholds.
    """
    if footprint.size == 1 or image.size == 1:
        return image > threshold

    image_max = ndi.maximum_filter(image, footprint=footprint, mode="constant")

    out = image == image_max

    out = image == image_max

    # no peak for a trivial image
    image_is_trivial = np.all(out) if mask is None else np.all(out[mask])
    if image_is_trivial:  # synchronize
        out[:] = False
        if mask is not None:
            # isolated pixels in masked area are returned as peaks
            isolated_px = cp.logical_xor(mask, ndi.binary_opening(mask))
            out[isolated_px] = True

    out &= image > threshold
    return out
示例#8
0
def test_footprint_all_zeros():
    # regression test for gh-6876: footprint of all zeros segfaults
    arr = cp.random.randint(0, 100, (100, 100))
    kernel = cp.zeros((3, 3), bool)
    with assert_raises(ValueError):
        sndi.maximum_filter(arr, footprint=kernel)