def test_slow_warp_nonint_oshape(): image = cp.random.rand(5, 5) with pytest.raises(ValueError): warp(image, lambda xy: xy, output_shape=(13.1, 19.5)) warp(image, lambda xy: xy, output_shape=(13.0001, 19.9999))
def test_warp_matrix(): x = cp.zeros((5, 5), dtype=np.double) x[2, 2] = 1 refx = cp.zeros((5, 5), dtype=np.double) refx[1, 1] = 1 matrix = cp.asarray([[1, 0, 1], [0, 1, 1], [0, 0, 1]]) # _warp_fast outx = warp(x, matrix, order=1) assert_array_almost_equal(outx, refx) # check for ndimage.map_coordinates outx = warp(x, matrix, order=5)
def test_warp_identity(): img = img_as_float(cp.array(rgb2gray(astronaut()))) assert len(img.shape) == 2 assert cp.allclose(img, warp(img, AffineTransform(rotation=0))) assert not cp.allclose(img, warp(img, AffineTransform(rotation=0.1))) img = cp.asnumpy(img) rgb_img = cp.transpose( cp.asarray(np.array([img, np.zeros_like(img), img])), (1, 2, 0)) warped_rgb_img = warp(rgb_img, AffineTransform(rotation=0.1)) assert cp.allclose(rgb_img, warp(rgb_img, AffineTransform(rotation=0))) assert not cp.allclose(rgb_img, warped_rgb_img) # assert no cross-talk between bands assert cp.all(0 == warped_rgb_img[:, :, 1])
def test_warp_tform(): x = cp.zeros((5, 5), dtype=np.double) x[2, 2] = 1 theta = -np.pi / 2 tform = SimilarityTransform(scale=1, rotation=theta, translation=(0, 4), xp=cp) x90 = warp(x, tform, order=1) assert_array_almost_equal(x90, cp.rot90(x)) x90 = warp(x, tform.inverse, order=1) assert_array_almost_equal(x90, cp.rot90(x))
def test_zero_image_size(): with pytest.raises(ValueError): warp(cp.zeros(0), SimilarityTransform()) with pytest.raises(ValueError): warp(cp.zeros((0, 10)), SimilarityTransform()) with pytest.raises(ValueError): warp(cp.zeros((10, 0)), SimilarityTransform()) with pytest.raises(ValueError): warp(cp.zeros((10, 10, 0)), SimilarityTransform())
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)
def test_warp_callable(): x = cp.zeros((5, 5), dtype=np.double) x[2, 2] = 1 refx = cp.zeros((5, 5), dtype=np.double) refx[1, 1] = 1 def shift(xy): return xy + 1 outx = warp(x, shift, order=1) assert_array_almost_equal(outx, refx)
def test_homography(): x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 theta = -np.pi / 2 # fmt: off M = cp.array([[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 4], [0, 0, 1]]) # fmt: on x90 = warp(x, inverse_map=ProjectiveTransform(M).inverse, order=1) assert_array_almost_equal(x90, cp.rot90(x))
def test_warp_nd(): for dim in range(2, 8): shape = dim * (5, ) x = cp.zeros(shape, dtype=np.double) x_c = dim * (2, ) x[x_c] = 1 refx = cp.zeros(shape, dtype=np.double) refx_c = dim * (1, ) refx[refx_c] = 1 coord_grid = dim * (slice(0, 5, 1), ) coords = cp.array(cp.mgrid[coord_grid]) + 1 outx = warp(x, coords, order=0, cval=0) assert_array_almost_equal(outx, refx)
def test_inverse(): tform = SimilarityTransform(scale=0.5, rotation=0.1) inverse_tform = SimilarityTransform(matrix=cp.linalg.inv(tform.params)) image = cp.arange(10 * 10).reshape(10, 10).astype(cp.double) assert_array_equal(warp(image, inverse_tform), warp(image, tform.inverse))
def test_invalid(): with pytest.raises(ValueError): warp(cp.ones((4, 3, 3, 3)), SimilarityTransform())
def test_const_cval_out_of_range(): img = cp.random.randn(100, 100) cval = -10 warped = warp(img, AffineTransform(translation=(10, 10)), cval=cval) assert cp.sum(warped == cval) == (2 * 100 * 10 - 10 * 10)