Ejemplo n.º 1
0
def test_rescale():
    # same scale factor
    x = cp.zeros((5, 5), dtype=np.double)
    x[1, 1] = 1
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=False,
                     anti_aliasing=False,
                     mode='constant')
    ref = cp.zeros((10, 10))
    ref[2:4, 2:4] = 1
    assert_array_almost_equal(scaled, ref)

    # different scale factors
    x = cp.zeros((5, 5), dtype=np.double)
    x[1, 1] = 1

    scaled = rescale(x, (2, 1),
                     order=0,
                     multichannel=False,
                     anti_aliasing=False,
                     mode='constant')
    ref = cp.zeros((10, 5))
    ref[2:4, 1] = 1
    assert_array_almost_equal(scaled, ref)
Ejemplo n.º 2
0
def test_keep_range():
    image = cp.linspace(0, 2, 25).reshape(5, 5)
    out = rescale(image,
                  2,
                  preserve_range=False,
                  clip=True,
                  order=0,
                  mode='constant',
                  multichannel=False,
                  anti_aliasing=False)
    assert out.min() == 0
    assert out.max() == 2

    out = rescale(image,
                  2,
                  preserve_range=True,
                  clip=True,
                  order=0,
                  mode='constant',
                  multichannel=False,
                  anti_aliasing=False)
    assert out.min() == 0
    assert out.max() == 2

    out = rescale(image.astype(np.uint8),
                  2,
                  preserve_range=False,
                  mode='constant',
                  multichannel=False,
                  anti_aliasing=False,
                  clip=True,
                  order=0)
    assert out.min() == 0
    assert out.max() == 2 / 255.0
Ejemplo n.º 3
0
def test_rescale_multichannel_defaults():
    x = cp.zeros((8, 3), dtype=np.double)
    scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant')
    assert_equal(scaled.shape, (16, 6))

    x = cp.zeros((8, 8, 3), dtype=np.double)
    scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant')
    assert_equal(scaled.shape, (16, 16, 6))
Ejemplo n.º 4
0
def test_rescale_invalid_scale():
    x = cp.zeros((10, 10, 3))
    with pytest.raises(ValueError):
        rescale(x, (2, 2),
                multichannel=False,
                anti_aliasing=False,
                mode='constant')
    with pytest.raises(ValueError):
        rescale(x, (2, 2, 2),
                multichannel=True,
                anti_aliasing=False,
                mode='constant')
Ejemplo n.º 5
0
def test_bool_array_warnings():
    img = cp.zeros((10, 10), dtype=bool)

    with expected_warnings(['Input image dtype is bool']):
        rescale(img, 0.5, anti_aliasing=True)

    with expected_warnings(['Input image dtype is bool']):
        resize(img, (5, 5), anti_aliasing=True)

    with expected_warnings(['Input image dtype is bool']):
        rescale(img, 0.5, order=1)

    with expected_warnings(['Input image dtype is bool']):
        resize(img, (5, 5), order=1)

    with expected_warnings(['Input image dtype is bool']):
        warp(img, cp.eye(3), order=1)
Ejemplo n.º 6
0
def test_rescale_multichannel_multiscale():
    x = cp.zeros((5, 5, 3), dtype=np.double)
    scaled = rescale(x, (2, 1),
                     order=0,
                     multichannel=True,
                     anti_aliasing=False,
                     mode='constant')
    assert_equal(scaled.shape, (10, 5, 3))
Ejemplo n.º 7
0
def test_bool_img_rescale():
    img = cp.ones((12, 18), dtype=bool)
    img[2:-2, 4:-4] = False
    res = rescale(img, 0.5)

    expected = cp.ones((6, 9))
    expected[1:-1, 2:-2] = False

    assert_array_equal(res, expected)
Ejemplo n.º 8
0
def test_downscale_anti_aliasing():
    x = cp.zeros((10, 10), dtype=np.double)
    x[2, 2] = 1
    scaled = rescale(x,
                     0.5,
                     order=1,
                     anti_aliasing=True,
                     multichannel=False,
                     mode='constant')
    assert_equal(scaled.shape, (5, 5))
    assert np.all(scaled[:3, :3] > 0)
    assert_equal(float(scaled[3:, :].sum()), 0)
    assert_equal(float(scaled[:, 3:].sum()), 0)
Ejemplo n.º 9
0
def test_downscale():
    x = cp.zeros((10, 10), dtype=np.double)
    x[2:4, 2:4] = 1
    scaled = rescale(x,
                     0.5,
                     order=0,
                     anti_aliasing=False,
                     multichannel=False,
                     mode='constant')
    assert_equal(scaled.shape, (5, 5))
    assert_equal(float(scaled[1, 1]), 1)
    assert_equal(float(scaled[2:, :].sum()), 0)
    assert_equal(float(scaled[:, 2:].sum()), 0)
Ejemplo n.º 10
0
def test_warp_clip():
    x = cp.zeros((5, 5), dtype=np.double)
    x[2, 2] = 1

    outx = rescale(x,
                   3,
                   order=3,
                   clip=False,
                   multichannel=False,
                   anti_aliasing=False,
                   mode='constant')
    assert outx.min() < 0

    outx = rescale(x,
                   3,
                   order=3,
                   clip=True,
                   multichannel=False,
                   anti_aliasing=False,
                   mode='constant')
    assert_almost_equal(float(outx.min()), 0)
    assert_almost_equal(float(outx.max()), 1)
Ejemplo n.º 11
0
def test_rescale_multichannel():
    # 1D + channels
    x = cp.zeros((8, 3), dtype=np.double)
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=True,
                     anti_aliasing=False,
                     mode='constant')
    assert_equal(scaled.shape, (16, 3))
    # 2D
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=False,
                     anti_aliasing=False,
                     mode='constant')
    assert_array_equal(scaled.shape, (16, 6))

    # 2D + channels
    x = cp.zeros((8, 8, 3), dtype=np.double)
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=True,
                     anti_aliasing=False,
                     mode='constant')
    assert_equal(scaled.shape, (16, 16, 3))
    # 3D
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=False,
                     anti_aliasing=False,
                     mode='constant')
    assert_equal(scaled.shape, (16, 16, 6))

    # 3D + channels
    x = cp.zeros((8, 8, 8, 3), dtype=np.double)
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=True,
                     anti_aliasing=False,
                     mode='constant')
    assert_array_equal(scaled.shape, (16, 16, 16, 3))
    # 4D
    scaled = rescale(x,
                     2,
                     order=0,
                     multichannel=False,
                     anti_aliasing=False,
                     mode='constant')
    assert_equal(scaled.shape, (16, 16, 16, 6))