def test_copy(): x = cp.array([1], dtype=cp.float64) y = img_as_float(x) z = img_as_float(x, force_copy=True) assert y is x assert z is not x
def test_rotated_img(): """ The harris filter should yield the same results with an image and it's rotation. """ im = img_as_float(cp.asarray(data.astronaut().mean(axis=2))) im_rotated = im.T # # Moravec # results = peak_local_max(corner_moravec(im), # min_distance=10, threshold_rel=0) # results_rotated = peak_local_max(corner_moravec(im_rotated), # min_distance=10, threshold_rel=0) # assert (cp.sort(results[:, 0]) == cp.sort(results_rotated[:, 1])).all() # assert (cp.sort(results[:, 1]) == cp.sort(results_rotated[:, 0])).all() # Harris results = cp.nonzero(corner_harris(im)) results_rotated = cp.nonzero(corner_harris(im_rotated)) assert (cp.sort(results[0]) == cp.sort(results_rotated[1])).all() assert (cp.sort(results[1]) == cp.sort(results_rotated[0])).all() # Shi-Tomasi results = cp.nonzero(corner_shi_tomasi(im)) results_rotated = cp.nonzero(corner_shi_tomasi(im_rotated)) assert (cp.sort(results[0]) == cp.sort(results_rotated[1])).all() assert (cp.sort(results[1]) == cp.sort(results_rotated[0])).all()
def test_daisy_normalization(): img = img_as_float(data.astronaut()[:64, :64].mean(axis=2)) descs = daisy(img, normalization="l1") for i in range(descs.shape[0]): for j in range(descs.shape[1]): assert_array_almost_equal(cp.sum(descs[i, j, :]), 1) descs_ = daisy(img) assert_array_almost_equal(descs, descs_) descs = daisy(img, normalization="l2") for i in range(descs.shape[0]): for j in range(descs.shape[1]): dtmp = descs[i, j, :] assert_array_almost_equal(cp.sqrt(cp.sum(dtmp * dtmp)), 1) orientations = 8 descs = daisy(img, orientations=orientations, normalization="daisy") desc_dims = descs.shape[2] for i in range(descs.shape[0]): for j in range(descs.shape[1]): for k in range(0, desc_dims, orientations): dtmp = descs[i, j, k:k + orientations] assert_array_almost_equal(cp.sqrt(cp.sum(dtmp * dtmp)), 1) img = cp.zeros((50, 50)) descs = daisy(img, normalization="off") for i in range(descs.shape[0]): for j in range(descs.shape[1]): assert_array_almost_equal(cp.sum(descs[i, j, :]), 0) with pytest.raises(ValueError): daisy(img, normalization="does_not_exist")
def test_bounding_values(): image = img_as_float(cp.asarray(data.page())) template = cp.zeros((3, 3)) template[1, 1] = 1 result = match_template(image, template) print(result.max()) assert result.max() < 1 + 1e-7 assert result.min() > -1 - 1e-7
def test_4d_input_pixel(): phantom = img_as_float(binary_blobs(length=32, n_dim=4)) reference_image = fft.fftn(phantom) shift = (-2.0, 1.0, 5.0, -3) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = phase_cross_correlation(reference_image, shifted_image, space="fourier") assert_allclose(result, -cp.array(shift), atol=0.05)
def test_descs_shape(): img = img_as_float(data.astronaut()[:256, :256].mean(axis=2)) radius = 20 step = 8 descs = daisy(img, radius=radius, step=step) assert descs.shape[0] == math.ceil( (img.shape[0] - radius * 2) / float(step)) assert descs.shape[1] == math.ceil( (img.shape[1] - radius * 2) / float(step)) img = img[:-1, :-2] radius = 5 step = 3 descs = daisy(img, radius=radius, step=step) assert descs.shape[0] == math.ceil( (img.shape[0] - radius * 2) / float(step)) assert descs.shape[1] == math.ceil( (img.shape[1] - radius * 2) / float(step))
def test_daisy_desc_dims(): img = img_as_float(cp.asarray(data.astronaut()[:128, :128].mean(axis=2))) rings = 2 histograms = 4 orientations = 3 descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations) assert descs.shape[2] == (rings * histograms + 1) * orientations rings = 4 histograms = 5 orientations = 13 descs = daisy(img, rings=rings, histograms=histograms, orientations=orientations) assert descs.shape[2] == (rings * histograms + 1) * orientations
def test_3d_input(): phantom = img_as_float(binary_blobs(length=32, n_dim=3)) reference_image = fft.fftn(phantom) shift = (-2.0, 1.0, 5.0) shifted_image = fourier_shift(reference_image, shift) result, error, diffphase = phase_cross_correlation(reference_image, shifted_image, space="fourier") assert_allclose(result, -cp.array(shift), atol=0.05) # subpixel precision now available for 3-D data subpixel_shift = (-2.3, 1.7, 5.4) shifted_image = fourier_shift(reference_image, subpixel_shift) result, error, diffphase = phase_cross_correlation(reference_image, shifted_image, upsample_factor=100, space="fourier") assert_allclose(result, -cp.array(subpixel_shift), atol=0.05)
def test_squared_dot(): im = cp.zeros((50, 50)) im[4:8, 4:8] = 1 im = img_as_float(im) # Moravec fails # Harris results = peak_local_max(corner_harris(im), min_distance=10, threshold_rel=0) assert (results == cp.asarray([[6, 6]])).all() # Shi-Tomasi results = peak_local_max(corner_shi_tomasi(im), min_distance=10, threshold_rel=0) assert (results == cp.asarray([[6, 6]])).all()
def test_float_float_all_ranges(): arr_in = cp.array([[-10.0, 10.0, 1e20]], dtype=cp.float32) cp.testing.assert_array_equal(img_as_float(arr_in), arr_in)
def test_float32_passthrough(): x = cp.array([-1, 1], dtype=cp.float32) y = img_as_float(x) assert y.dtype == x.dtype
def test_daisy_incompatible_sigmas_and_radii(): img = img_as_float(data.astronaut()[:64, :64].mean(axis=2)) sigmas = [1, 2] radii = [1, 2] with pytest.raises(ValueError): daisy(img, sigmas=sigmas, ring_radii=radii)
def test_daisy_sigmas_and_radii(): img = img_as_float(data.astronaut()[:64, :64].mean(axis=2)) sigmas = [1, 2, 3] radii = [1, 2] daisy(img, sigmas=sigmas, ring_radii=radii)
def test_daisy_visualization(): img = img_as_float(data.astronaut()[:32, :32].mean(axis=2)) descs, descs_img = daisy(img, visualize=True) assert descs_img.shape == (32, 32, 3)
def test_each_channel(): filtered = edges_each(COLOR_IMAGE) for i, channel in enumerate(cp.rollaxis(filtered, axis=-1)): expected = img_as_float(filters.sobel(COLOR_IMAGE[:, :, i])) assert_allclose(channel, expected)