Exemple #1
0
def test_scharr_vertical():
    """Scharr on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    assert_allclose(result[j == 0], 1)
    assert (np.all(result[np.abs(j) > 1] == 0))
Exemple #2
0
def test_ellipsoid_levelset():
    test = ellipsoid(2, 2, 2, levelset=True)[1:-1, 1:-1, 1:-1]
    test_anisotropic = ellipsoid(2, 2, 4, spacing=(1., 1., 2.), levelset=True)
    test_anisotropic = test_anisotropic[1:-1, 1:-1, 1:-1]

    expected = np.array([[[2., 1.25, 1., 1.25,
                           2.], [1.25, 0.5, 0.25, 0.5, 1.25],
                          [1., 0.25, 0., 0.25,
                           1.], [1.25, 0.5, 0.25, 0.5, 1.25],
                          [2., 1.25, 1., 1.25, 2.]],
                         [[1.25, 0.5, 0.25, 0.5, 1.25],
                          [0.5, -0.25, -0.5, -0.25, 0.5],
                          [0.25, -0.5, -0.75, -0.5, 0.25],
                          [0.5, -0.25, -0.5, -0.25, 0.5],
                          [1.25, 0.5, 0.25, 0.5, 1.25]],
                         [[1., 0.25, 0., 0.25, 1.],
                          [0.25, -0.5, -0.75, -0.5, 0.25],
                          [0., -0.75, -1., -0.75, 0.],
                          [0.25, -0.5, -0.75, -0.5, 0.25],
                          [1., 0.25, 0., 0.25, 1.]],
                         [[1.25, 0.5, 0.25, 0.5, 1.25],
                          [0.5, -0.25, -0.5, -0.25, 0.5],
                          [0.25, -0.5, -0.75, -0.5, 0.25],
                          [0.5, -0.25, -0.5, -0.25, 0.5],
                          [1.25, 0.5, 0.25, 0.5, 1.25]],
                         [[2., 1.25, 1., 1.25,
                           2.], [1.25, 0.5, 0.25, 0.5, 1.25],
                          [1., 0.25, 0., 0.25,
                           1.], [1.25, 0.5, 0.25, 0.5, 1.25],
                          [2., 1.25, 1., 1.25, 2.]]])

    assert_allclose(test, expected)
    assert_allclose(test_anisotropic, expected)
Exemple #3
0
def test_prewitt_vertical():
    """Prewitt on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.prewitt(image) * np.sqrt(2)
    assert_allclose(result[j == 0], 1)
    assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
Exemple #4
0
def test_multi_page_gif():
    img = imread(os.path.join(data_dir, 'no_time_for_that_tiny.gif'))
    assert img.shape == (24, 25, 14, 3), img.shape
    img2 = imread(os.path.join(data_dir, 'no_time_for_that_tiny.gif'),
                  img_num=5)
    assert img2.shape == (25, 14, 3)
    assert_allclose(img[5], img2)
 def check():
     expected = self.refs[filter]
     result = getattr(rank, filter)(self.image, self.selem)
     if filter == "entropy":
         # There may be some arch dependent rounding errors
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # https://github.com/scikit-image/scikit-image/issues/2528
         assert_allclose(expected, result, atol=0, rtol=1E-15)
     elif filter == "otsu":
         # OTSU May also have some optimization dependent failures
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # Pixel 3, 5 was found to be problematic. It can take either
         # a value of 41 or 81 depending on the specific optimizations
         # used.
         assert result[3, 5] in [41, 81]
         result[3, 5] = 81
         # Pixel [19, 18] is also found to be problematic for the same
         # reason.
         assert result[19, 18] in [141, 172]
         result[19, 18] = 172
         assert_array_almost_equal(expected, result)
     else:
         assert_array_almost_equal(expected, result)
def test_pepper():
    seed = 42
    cam = img_as_float(camera())
    data_signed = cam * 2. - 1.  # Same image, on range [-1, 1]

    cam_noisy = random_noise(cam, seed=seed, mode='pepper', amount=0.15)
    peppermask = cam != cam_noisy

    # Ensure all changes are to 1.0
    assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(peppermask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.15

    # Check to make sure pepper gets added properly to signed images
    orig_zeros = (data_signed == -1).sum()
    cam_noisy_signed = random_noise(data_signed,
                                    seed=seed,
                                    mode='pepper',
                                    amount=.15)

    proportion = (float((cam_noisy_signed == -1).sum() - orig_zeros) /
                  (cam.shape[0] * cam.shape[1]))
    assert 0.11 < proportion <= 0.15
Exemple #7
0
def test_multi_page_gif():
    img = imread(os.path.join(data_dir, 'no_time_for_that_tiny.gif'))
    assert img.shape == (24, 25, 14, 3), img.shape
    img2 = imread(os.path.join(data_dir, 'no_time_for_that_tiny.gif'),
                  img_num=5)
    assert img2.shape == (25, 14, 3)
    assert_allclose(img[5], img2)
Exemple #8
0
def test_farid_vertical():
    """Farid on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.farid(image) * np.sqrt(2)
    assert (np.all(result[j == 0] == result[j == 0][0]))
    assert_allclose(result[np.abs(j) > 2], 0, atol=1e-10)
Exemple #9
0
def test_picture_slice():
    array = _array_2d_to_RGBA(np.arange(0, 10)[np.newaxis, :])
    pic = novice.Picture(array=array)

    x_slice = slice(3, 8)
    subpic = pic[:, x_slice]
    assert_allclose(subpic.array, array[x_slice, :])
Exemple #10
0
 def check():
     expected = self.refs[filter]
     result = getattr(rank, filter)(self.image, self.selem)
     if filter == "entropy":
         # There may be some arch dependent rounding errors
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # https://github.com/scikit-image/scikit-image/issues/2528
         assert_allclose(expected, result, atol=0, rtol=1E-15)
     elif filter == "otsu":
         # OTSU May also have some optimization dependent failures
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # Pixel 3, 5 was found to be problematic. It can take either
         # a value of 41 or 81 depending on the specific optimizations
         # used.
         assert result[3, 5] in [41, 81]
         result[3, 5] = 81
         # Pixel [19, 18] is also found to be problematic for the same
         # reason.
         assert result[19, 18] in [141, 172]
         result[19, 18] = 172
         assert_array_equal(expected, result)
     else:
         assert_array_equal(expected, result)
Exemple #11
0
def test_multi_page_gif():
    img = imread(fetch('data/no_time_for_that_tiny.gif'))
    assert img.shape == (24, 25, 14, 3), img.shape
    img2 = imread(fetch('data/no_time_for_that_tiny.gif'),
                  img_num=5)
    assert img2.shape == (25, 14, 3)
    assert_allclose(img[5], img2)
Exemple #12
0
 def check():
     expected = self.refs[filter]
     if outdt is not None:
         out = np.zeros_like(expected, dtype=outdt)
     else:
         out = None
     result = getattr(rank, filter)(self.image, self.footprint, out=out)
     if filter == "entropy":
         # There may be some arch dependent rounding errors
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # https://github.com/scikit-image/scikit-image/issues/2528
         if outdt is not None:
             # Adjust expected precision
             expected = expected.astype(outdt)
         assert_allclose(expected, result, atol=0, rtol=1E-15)
     elif filter == "otsu":
         # OTSU May also have some optimization dependent failures
         # See the discussions in
         # https://github.com/scikit-image/scikit-image/issues/3091
         # Pixel 3, 5 was found to be problematic. It can take either
         # a value of 41 or 81 depending on the specific optimizations
         # used.
         assert result[3, 5] in [41, 81]
         result[3, 5] = 81
         # Pixel [19, 18] is also found to be problematic for the same
         # reason.
         assert result[19, 18] in [141, 172]
         result[19, 18] = 172
         assert_array_almost_equal(expected, result)
     else:
         if outdt is not None:
             # Avoid rounding issues comparing to expected result
             result = result.astype(expected.dtype)
         assert_array_almost_equal(expected, result)
Exemple #13
0
def test_picture_slice():
    array = _array_2d_to_RGBA(np.arange(0, 10)[np.newaxis, :])
    pic = novice.Picture(array=array)

    x_slice = slice(3, 8)
    subpic = pic[:, x_slice]
    assert_allclose(subpic.array, array[x_slice, :])
Exemple #14
0
def test_scharr_vertical():
    """Scharr on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    j[np.abs(i) == 5] = 10000
    assert_allclose(result[j == 0], 1)
    assert (np.all(result[np.abs(j) > 1] == 0))
Exemple #15
0
def test_sobel_horizontal():
    """Sobel on a horizontal edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.sobel(image) * np.sqrt(2)
    # Check if result match transform direction
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
Exemple #16
0
def test_farid_v_vertical():
    """Vertical Farid on an edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.farid_v(image)
    # Check if result match transform direction
    assert (np.all(result[j == 0] == result[j == 0][0]))
    assert_allclose(result[np.abs(j) > 2], 0, atol=1e-10)
Exemple #17
0
def test_farid_h_horizontal():
    """Horizontal Farid on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.farid_h(image)
    # Check if result match transform direction
    assert np.all(result[i == 0] == result[i == 0][0])
    assert_allclose(result[np.abs(i) > 2], 0, atol=1e-10)
Exemple #18
0
def test_prewitt_vertical():
    """Prewitt on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.prewitt(image) * np.sqrt(2)
    j[np.abs(i) == 5] = 10000
    assert_allclose(result[j == 0], 1)
    assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
Exemple #19
0
def test_prewitt_h_horizontal():
    """Horizontal prewitt on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.prewitt_h(image)
    # Check if result match transform direction
    assert (np.all(result[i == 0] == 1))
    assert_allclose(result[np.abs(i) > 1], 0, atol=1e-10)
Exemple #20
0
def test_prewitt_horizontal():
    """Prewitt on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.prewitt(image) * np.sqrt(2)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert (np.all(result[i == 0] == 1))
    assert_allclose(result[np.abs(i) > 1], 0, atol=1e-10)
Exemple #21
0
def test_prewitt_h_horizontal():
    """Horizontal prewitt on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.prewitt_h(image)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert (np.all(result[i == 0] == 1))
    assert_allclose(result[np.abs(i) > 1], 0, atol=1e-10)
Exemple #22
0
def test_sobel_horizontal():
    """Sobel on a horizontal edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.sobel(image) * np.sqrt(2)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
def test_4d_input_pixel():
    phantom = img_as_float(binary_blobs(length=32, n_dim=4))
    reference_image = np.fft.fftn(phantom)
    shift = (-2., 1., 5., -3)
    shifted_image = fourier_shift(reference_image, shift)
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image,
                                                    space="fourier")
    assert_allclose(result, -np.array(shift), atol=0.05)
Exemple #24
0
def test_prewitt_v_vertical():
    """Vertical prewitt on an edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.prewitt_v(image)
    # Fudge the eroded points
    j[np.abs(i) == 5] = 10000
    assert (np.all(result[j == 0] == 1))
    assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
Exemple #25
0
def test_farid_horizontal():
    """Farid on a horizontal edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.farid(image) * np.sqrt(2)
    # Check if result match transform direction
    i[np.abs(j) == 5] = 10000
    assert (np.all(result[i == 0] == result[i == 0][0]))
    assert_allclose(result[np.abs(i) > 2], 0, atol=1e-10)
Exemple #26
0
def test_sobel_horizontal():
    """Sobel on a horizontal edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.sobel(image) * np.sqrt(2)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
def test_4d_input_pixel():
    phantom = img_as_float(binary_blobs(length=32, n_dim=4))
    reference_image = fft.fftn(phantom)
    shift = (-2., 1., 5., -3)
    shifted_image = fourier_shift(reference_image, shift)
    result, error, diffphase = phase_cross_correlation(reference_image,
                                                       shifted_image,
                                                       space="fourier")
    assert_allclose(result, -np.array(shift), atol=0.05)
Exemple #28
0
def test_scharr_horizontal():
    """Scharr on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    # Check if result match transform direction
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
Exemple #29
0
def test_farid_h_horizontal():
    """Horizontal Farid on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.farid_h(image)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert np.all(result[i == 0] == result[i == 0][0])
    assert_allclose(result[np.abs(i) > 2], 0, atol=1e-10)
Exemple #30
0
def test_farid_v_vertical():
    """Vertical Farid on an edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.farid_v(image)
    # Fudge the eroded points
    j[np.abs(i) == 5] = 10000
    assert (np.all(result[j == 0] == result[j == 0][0]))
    assert_allclose(result[np.abs(j) > 2], 0, atol=1e-10)
Exemple #31
0
def test_prewitt_v_vertical():
    """Vertical prewitt on an edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.prewitt_v(image)
    # Check if result match transform direction
    j[np.abs(i) == 5] = 10000
    assert (np.all(result[j == 0] == 1))
    assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
def test_poisson():
    seed = 42
    data = camera()  # 512x512 grayscale uint8
    cam_noisy = random_noise(data, mode='poisson', seed=seed)
    cam_noisy2 = random_noise(data, mode='poisson', seed=seed, clip=False)

    np.random.seed(seed=seed)
    expected = np.random.poisson(img_as_float(data) * 256) / 256.
    assert_allclose(cam_noisy, np.clip(expected, 0., 1.))
    assert_allclose(cam_noisy2, expected)
def test_poisson():
    seed = 42
    data = camera()  # 512x512 grayscale uint8
    cam_noisy = random_noise(data, mode='poisson', seed=seed)
    cam_noisy2 = random_noise(data, mode='poisson', seed=seed, clip=False)

    np.random.seed(seed=seed)
    expected = np.random.poisson(img_as_float(data) * 256) / 256.
    assert_allclose(cam_noisy, np.clip(expected, 0., 1.))
    assert_allclose(cam_noisy2, expected)
def test_speckle():
    seed = 42
    data = np.zeros((128, 128)) + 0.1
    np.random.seed(seed=seed)
    noise = np.random.normal(0.1, 0.02 ** 0.5, (128, 128))
    expected = np.clip(data + data * noise, 0, 1)

    data_speckle = random_noise(data, mode='speckle', seed=seed, mean=0.1,
                                var=0.02)
    assert_allclose(expected, data_speckle)
def test_correlation():
    reference_image = fft.fftn(camera())
    shift = (-7, 12)
    shifted_image = fourier_shift(reference_image, shift)

    # pixel precision
    result, error, diffphase = phase_cross_correlation(reference_image,
                                                       shifted_image,
                                                       space="fourier")
    assert_allclose(result[:2], -np.array(shift))
Exemple #36
0
def test_real_input():
    reference_image = camera()
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(fft.fftn(reference_image), subpixel_shift)
    shifted_image = fft.ifftn(shifted_image)

    # subpixel precision
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image, 100)
    assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
Exemple #37
0
def test_4d_input_subpixel():
    phantom = img_as_float(binary_blobs(length=32, n_dim=4))
    reference_image = fft.fftn(phantom)
    subpixel_shift = (-2.3, 1.7, 5.4, -3.2)
    shifted_image = fourier_shift(reference_image, subpixel_shift)
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image,
                                                    10,
                                                    space="fourier")
    assert_allclose(result, -np.array(subpixel_shift), atol=0.05)
def test_real_input():
    reference_image = camera()
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(np.fft.fftn(reference_image), subpixel_shift)
    shifted_image = np.fft.ifftn(shifted_image)

    # subpixel precision
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image, 100)
    assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
def test_subpixel_precision():
    reference_image = np.fft.fftn(camera())
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(reference_image, subpixel_shift)

    # subpixel precision
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image, 100,
                                                    space="fourier")
    assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
def test_correlation():
    reference_image = np.fft.fftn(camera())
    shift = (-7, 12)
    shifted_image = fourier_shift(reference_image, shift)

    # pixel precision
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image,
                                                    space="fourier")
    assert_allclose(result[:2], -np.array(shift))
def test_size_one_dimension_input():
    # take a strip of the input image
    reference_image = np.fft.fftn(camera()[:, 15]).reshape((-1, 1))
    subpixel_shift = (-2.4, 4)
    shifted_image = fourier_shift(reference_image, subpixel_shift)

    # subpixel precision
    result, error, diffphase = register_translation(reference_image,
                                                    shifted_image, 100,
                                                    space="fourier")
    assert_allclose(result[:2], -np.array((-2.4, 0)), atol=0.05)
def test_line_profile():
    """ Test a line profile using an ndim=2 image"""
    plugin = setup_line_profile(data.camera())
    line_image, scan_data = plugin.output()
    for inp in [line_image.nonzero()[0].size,
                line_image.sum() / line_image.max(),
                scan_data.size]:
        assert_equal(inp, 172)
    assert_equal(line_image.shape, (512, 512))
    assert_allclose(scan_data.max(), 0.9176, rtol=1e-3)
    assert_allclose(scan_data.mean(), 0.2812, rtol=1e-3)
def test_real_input():
    reference_image = camera()
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(fft.fftn(reference_image), subpixel_shift)
    shifted_image = fft.ifftn(shifted_image)

    # subpixel precision
    result, error, diffphase = phase_cross_correlation(reference_image,
                                                       shifted_image,
                                                       upsample_factor=100)
    assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
Exemple #44
0
def test_line_profile():
    """ Test a line profile using an ndim=2 image"""
    plugin = setup_line_profile(data.camera())
    line_image, scan_data = plugin.output()
    for inp in [line_image.nonzero()[0].size,
                line_image.sum() / line_image.max(),
                scan_data.size]:
        assert_equal(inp, 172)
    assert_equal(line_image.shape, (512, 512))
    assert_allclose(scan_data.max(), 0.9176, rtol=1e-3)
    assert_allclose(scan_data.mean(), 0.2812, rtol=1e-3)
def test_subpixel_precision():
    reference_image = fft.fftn(camera())
    subpixel_shift = (-2.4, 1.32)
    shifted_image = fourier_shift(reference_image, subpixel_shift)

    # subpixel precision
    result, error, diffphase = phase_cross_correlation(reference_image,
                                                       shifted_image,
                                                       upsample_factor=100,
                                                       space="fourier")
    assert_allclose(result[:2], -np.array(subpixel_shift), atol=0.05)
Exemple #46
0
def test_line_profile_rgb():
    """ Test a line profile using an ndim=3 image"""
    plugin = setup_line_profile(data.chelsea(), limits=None)
    for i in range(6):
        plugin.line_tool._thicken_scan_line()
    line_image, scan_data = plugin.output()
    assert_equal(line_image[line_image == 128].size, 750)
    assert_equal(line_image[line_image == 255].size, 151)
    assert_equal(line_image.shape, (300, 451))
    assert_equal(scan_data.shape, (151, 3))
    assert_allclose(scan_data.max(), 0.772, rtol=1e-3)
    assert_allclose(scan_data.mean(), 0.4359, rtol=1e-3)
def test_salt():
    seed = 42
    cam = img_as_float(camera())
    cam_noisy = random_noise(cam, seed=seed, mode='salt', amount=0.15)
    saltmask = cam != cam_noisy

    # Ensure all changes are to 1.0
    assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(saltmask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.15
Exemple #48
0
 def test_check_2d(self):
     arr = np.arange(20).reshape(4, 5).astype(np.float64)
     test = pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0))
     expected = np.array(
         [[0.,   0.,   0.,   0.,   0.,   0.,   0.,    0.,   0.],
          [0.,   0.,   0.,  0.5,   1.,  1.5,   2.,    1.,   0.],
          [0.,   0.,   0.,   1.,   2.,   3.,   4.,    2.,   0.],
          [0.,  2.5,   5.,   6.,   7.,   8.,   9.,   4.5,   0.],
          [0.,   5.,  10.,  11.,  12.,  13.,  14.,    7.,   0.],
          [0.,  7.5,  15.,  16.,  17.,  18.,  19.,   9.5,   0.],
          [0., 3.75,  7.5,   8.,  8.5,   9.,  9.5,  4.75,   0.],
          [0.,   0.,   0.,   0.,   0.,   0.,   0.,    0.,   0.]])
     assert_allclose(test, expected)
    def test_getitem(self):
        num = len(self.images)
        for i in range(-num, num):
            assert type(self.images[i]) is np.ndarray
        assert_allclose(self.images[0],
                        self.images[-num])

        def return_img(n):
            return self.images[n]
        with testing.raises(IndexError):
            return_img(num)
        with testing.raises(IndexError):
            return_img(-num - 1)
Exemple #50
0
def test_vertical_mask_line(grad_func):
    """Vertical edge filters mask pixels surrounding input mask."""
    _, hgrad = np.mgrid[:1:11j, :1:11j]  # horizontal gradient with spacing 0.1
    hgrad[:, 5] = 1                      # bad vertical line

    mask = np.ones_like(hgrad)
    mask[:, 5] = 0                       # mask bad line

    expected = np.zeros_like(hgrad)
    expected[1:-1, 1:-1] = 0.2           # constant gradient for most of image,
    expected[1:-1, 4:7] = 0              # but line and neighbors masked

    result = grad_func(hgrad, mask)
    assert_allclose(result, expected)
    def test_getitem(self):
        for img in self.imgs:
            num = len(img)

            for i in range(-num, num):
                assert type(img[i]) is np.ndarray
            assert_allclose(img[0], img[-num])

            with testing.raises(AssertionError):
                assert_allclose(img[0], img[1])

            with testing.raises(IndexError):
                img[num]
            with testing.raises(IndexError):
                img[-num - 1]
Exemple #52
0
def test_imsave_boolean_input():
    shape = (2, 2)
    image = np.eye(*shape, dtype=np.bool)
    s = BytesIO()

    # save to file-like object
    with expected_warnings(
            ['is a boolean image: setting True to 1 and False to 0']):
        imsave(s, image)

    # read from file-like object
    s.seek(0)
    out = imread(s)
    assert_equal(out.shape, shape)
    assert_allclose(out, image)
Exemple #53
0
def test_imsave_filelike():
    shape = (2, 2)
    image = np.zeros(shape)
    s = BytesIO()

    # save to file-like object
    with expected_warnings(['precision loss',
                            'is a low contrast image']):
        imsave(s, image)

    # read from file-like object
    s.seek(0)
    out = imread(s)
    assert_equal(out.shape, shape)
    assert_allclose(out, image)
Exemple #54
0
def test_inpaint_biharmonic_2d():
    img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    mask = np.zeros_like(img)
    mask[2, 2:] = 1
    mask[1, 3:] = 1
    mask[0, 4:] = 1
    img[np.where(mask)] = 0
    out = inpaint.inpaint_biharmonic(img, mask)
    ref = np.array(
        [[0., 0.0625, 0.25000000, 0.5625000, 0.73925058],
         [0., 0.0625, 0.25000000, 0.5478048, 0.76557821],
         [0., 0.0625, 0.25842878, 0.5623079, 0.85927796],
         [0., 0.0625, 0.25000000, 0.5625000, 1.00000000],
         [0., 0.0625, 0.25000000, 0.5625000, 1.00000000]]
    )
    assert_allclose(ref, out)
Exemple #55
0
def test_laplace_zeros():
    """Laplace on a square image."""
    # Create a synthetic 2D image
    image = np.zeros((9, 9))
    image[3:-3, 3:-3] = 1
    result = filters.laplace(image)
    res_chk = np.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0., -1., -1., -1.,  0.,  0.,  0.],
                        [ 0.,  0., -1.,  2.,  1.,  2., -1.,  0.,  0.],
                        [ 0.,  0., -1.,  1.,  0.,  1., -1.,  0.,  0.],
                        [ 0.,  0., -1.,  2.,  1.,  2., -1.,  0.,  0.],
                        [ 0.,  0.,  0., -1., -1., -1.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
    assert_allclose(result, res_chk)
Exemple #56
0
    def test_check_constant_odd_pad_amount(self):
        arr = np.arange(30).reshape(5, 6)
        test = pad(arr, ((1,), (2,)), mode='constant',
                   constant_values=3)
        expected = np.array(
            [[ 3,  3,  3,  3,  3,  3,  3,  3,  3,  3],

             [ 3,  3,  0,  1,  2,  3,  4,  5,  3,  3],
             [ 3,  3,  6,  7,  8,  9, 10, 11,  3,  3],
             [ 3,  3, 12, 13, 14, 15, 16, 17,  3,  3],
             [ 3,  3, 18, 19, 20, 21, 22, 23,  3,  3],
             [ 3,  3, 24, 25, 26, 27, 28, 29,  3,  3],

             [ 3,  3,  3,  3,  3,  3,  3,  3,  3,  3]]
            )
        assert_allclose(test, expected)
 def test_slicing(self):
     assert type(self.images[:]) is ImageCollection
     assert len(self.images[:]) == 2
     assert len(self.images[:1]) == 1
     assert len(self.images[1:]) == 1
     assert_allclose(self.images[0], self.images[:1][0])
     assert_allclose(self.images[1], self.images[1:][0])
     assert_allclose(self.images[1], self.images[::-1][0])
     assert_allclose(self.images[0], self.images[::-1][1])
 def test_slicing(self):
     img = self.imgs[-1]
     assert type(img[:]) is ImageCollection
     assert len(img[:]) == 26, len(img[:])
     assert len(img[:1]) == 1
     assert len(img[1:]) == 25
     assert_allclose(img[0], img[:1][0])
     assert_allclose(img[1], img[1:][0])
     assert_allclose(img[-1], img[::-1][0])
     assert_allclose(img[0], img[::-1][-1])
def test_salt_and_pepper():
    seed = 42
    cam = img_as_float(camera())
    cam_noisy = random_noise(cam, seed=seed, mode='s&p', amount=0.15,
                             salt_vs_pepper=0.25)
    saltmask = np.logical_and(cam != cam_noisy, cam_noisy == 1.)
    peppermask = np.logical_and(cam != cam_noisy, cam_noisy == 0.)

    # Ensure all changes are to 0. or 1.
    assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))
    assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))

    # Ensure approximately correct amount of noise was added
    proportion = float(
        saltmask.sum() + peppermask.sum()) / (cam.shape[0] * cam.shape[1])
    assert 0.11 < proportion <= 0.18

    # Verify the relative amount of salt vs. pepper is close to expected
    assert 0.18 < saltmask.sum() / float(peppermask.sum()) < 0.33