def test_spline_grid_3D_anisotropic(): sg = SplineGrid(FD((90, 90, 90), (2.0, 3.0, 0.5)), 4) # field and sampling # Test basic params assert sg.ndim == 3 assert sg.field_shape == (90, 90, 90) assert sg.field_sampling == (2.0, 3.0, 0.5) assert sg.grid_sampling == 4 assert sg.grid_sampling_in_pixels == (4 / 2.0, 4 / 3.0, 4 / 0.5) assert sg.knots.shape == (48, 70, 15 ) # ceil(90/4) == 23. Add 3 (2 left, 1 right) # Get field, should be empty field = sg.get_field() assert field.shape == sg.field_shape assert np.all(field == 0) # From a field im = 4 * np.ones((20, 20, 20), np.float32) im[4, 5, 5] = 8 im[8, 9, 9] = 7 im = Aarray(im, (2.0, 3.0, 0.5)) sg = SplineGrid.from_field(im, 2) field1 = sg.get_field() assert field1.ndim == 3 assert field1.max() > 5.5 assert field1.max() < 7.5 assert field1.min() > 4 assert field1.min() < 4.7 # From a weighted field im = 4 * np.ones((20, 20, 20), np.float32) im[4, 5, 5] = 8 im[8, 9, 9] = 7 ww = np.zeros((20, 20, 20), np.float32) ww[4, 5, 5] = 1 ww[8, 9, 9] = 1 im = Aarray(im, (2.0, 3.0, 0.5)) sg = SplineGrid.from_field(im, 2, ww) field2 = sg.get_field() assert field2.ndim == 3 assert field2.max() > 7.5 assert field2.max() < 8.5 assert field2.min() >= 0 assert field2.min() < 0.5 # From a weighted field, multiscale im = 4 * np.ones((20, 20, 20), np.float32) im[4, 5, 5] = 8 im[8, 9, 9] = 7 ww = np.zeros((20, 20, 20), np.float32) ww[4, 5, 5] = 1 ww[8, 9, 9] = 1 im = Aarray(im, (2.0, 3.0, 0.5)) sg = SplineGrid.from_field_multiscale(im, 2, ww) field3 = sg.get_field() assert field3.ndim == 3 assert field3.max() > 7.5 assert field3.max() < 8.5 assert field3.min() > 4.5 assert field3.min() < 7 # From points pp = PointSet(3) pp.append(5, 5, 4) pp.append(9, 9, 8) pp2 = pp * PointSet((0.5, 3.0, 2.0)) sg2 = SplineGrid.from_points(FD((20, 20, 20), (2.0, 3.0, 0.5)), 2, pp2, [8, 7]) field5 = sg2.get_field() assert np.all(field5 == field2) # From points, multiscale pp = PointSet(3) pp.append(5, 5, 4) pp.append(9, 9, 8) pp2 = pp * PointSet((0.5, 3.0, 2.0)) sg = SplineGrid.from_points_multiscale(FD((20, 20, 20), (2.0, 3.0, 0.5)), 2, pp2, [8, 7]) field6 = sg.get_field() assert np.all(field6 == field3) # Get field in points values = sg.get_field_in_points(pp2) assert list(values) == [ field6[int(p[0, 2]), int(p[0, 1]), int(p[0, 0])] for p in pp ] # Get field in samples, x-y-z order values2 = sg.get_field_in_samples((pp[:, 2], pp[:, 1], pp[:, 0])) assert list(values) != list(values2) values2 = sg.get_field_in_samples((pp[:, 0], pp[:, 1], pp[:, 2])) assert list(values) == list(values2) # Test copy sg2 = sg.copy() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling assert sg2.grid_sampling_in_pixels == sg.grid_sampling_in_pixels assert sg2.knots is not sg.knots # Test refine sg2 = sg.refine() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling / 2 assert sg2.grid_sampling_in_pixels == tuple( [i / 2 for i in sg.grid_sampling_in_pixels]) # Test resize_field sg2 = sg.resize_field(FD((50, 50, 50))) assert sg2.get_field().shape == (50, 50, 50) # Test addition sg2 = sg.add(sg.add(sg)) field = sg2.get_field() assert np.allclose(field, field6 * 3) print('test_spline_grid_3D_anisotropic ok')
def test_spline_grid_1D(): sg = SplineGrid(FD((90, )), 4) # field and sampling # Test basic params assert sg.ndim == 1 assert sg.field_shape == (90, ) assert sg.field_sampling == (1, ) assert sg.grid_sampling == 4 assert sg.grid_sampling_in_pixels == (4, ) assert sg.knots.shape == (26, ) # ceil(90/4) == 23. Add 3 (2 left, 1 right) # Get field, should be empty field = sg.get_field() assert field.shape == sg.field_shape assert np.all(field == 0) # From a field im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) sg = SplineGrid.from_field(im, 2) field1 = sg.get_field() assert field1.max() > 6 assert field1.max() < 7 assert field1.min() > 4 assert field1.min() < 4.2 # From a weighted field im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) ww = np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], np.float32) sg = SplineGrid.from_field(im, 2, ww) field2 = sg.get_field() assert field2.max() > 7.5 assert field2.max() < 8.5 assert field2.min() > 0 assert field2.min() < 0.5 # From a weighted field, multiscale im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) ww = np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], np.float32) sg = SplineGrid.from_field_multiscale(im, 2, ww) field3 = sg.get_field() assert field3.max() > 7.5 assert field3.max() < 8.5 assert field3.min() > 6 assert field3.min() < 7 # From points pp = PointSet(1) pp.append(4) pp.append(9) sg = SplineGrid.from_points(FD((14, )), 2, pp, [8, 7]) field5 = sg.get_field() assert all(field5 == field2) # From points, multiscale pp = PointSet(1) pp.append(4) pp.append(9) sg = SplineGrid.from_points_multiscale(FD((14, )), 2, pp, [8, 7]) field6 = sg.get_field() assert all(field6 == field3) # Get field in points values = sg.get_field_in_points(pp) assert list(values) == [field6[int(p[0])] for p in pp] # Test copy sg2 = sg.copy() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling assert sg2.grid_sampling_in_pixels == sg.grid_sampling_in_pixels assert sg2.knots is not sg.knots # Test refine sg2 = sg.refine() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling / 2 assert sg2.grid_sampling_in_pixels == tuple( [i / 2 for i in sg.grid_sampling_in_pixels]) # Test resize_field sg2 = sg.resize_field(FD((50, ))) assert sg2.get_field().shape == (50, ) # Test addition sg2 = sg.add(sg.add(sg)) field = sg2.get_field() assert np.allclose(field, field6 * 3) print('test_spline_grid_1D ok')
def test_spline_grid_2D(): sg = SplineGrid(FD((90, 90)), 4) # field and sampling # Test basic params assert sg.ndim == 2 assert sg.field_shape == (90, 90) assert sg.field_sampling == (1, 1) assert sg.grid_sampling == 4 assert sg.grid_sampling_in_pixels == (4, 4) assert sg.knots.shape == (26, 26 ) # ceil(90/4) == 23. Add 3 (2 left, 1 right) # Get field, should be empty field = sg.get_field() assert field.shape == sg.field_shape assert np.all(field == 0) # From a field im = 4 * np.ones((20, 20), np.float32) im[4, 5] = 8 im[8, 9] = 7 sg = SplineGrid.from_field(im, 2) field1 = sg.get_field() assert field1.ndim == 2 assert field1.max() > 5.5 assert field1.max() < 6.5 assert field1.min() > 4 assert field1.min() < 4.5 # From a weighted field im = 4 * np.ones((20, 20), np.float32) im[4, 5] = 8 im[8, 9] = 7 ww = np.zeros((20, 20), np.float32) ww[4, 5] = 1 ww[8, 9] = 1 sg = SplineGrid.from_field(im, 2, ww) field2 = sg.get_field() assert field2.ndim == 2 assert field2.max() > 7.5 assert field2.max() < 8.5 assert field2.min() >= 0 assert field2.min() < 0.5 # From a weighted field, multiscale im = 4 * np.ones((20, 20), np.float32) im[4, 5] = 8 im[8, 9] = 7 ww = np.zeros((20, 20), np.float32) ww[4, 5] = 1 ww[8, 9] = 1 sg = SplineGrid.from_field_multiscale(im, 2, ww) field3 = sg.get_field() assert field3.ndim == 2 assert field3.max() > 7.5 assert field3.max() < 8.5 assert field3.min() > 5 assert field3.min() < 7 # From points pp = PointSet(2) pp.append(5, 4) pp.append(9, 8) sg2 = SplineGrid.from_points(FD((20, 20)), 2, pp, [8, 7]) field5 = sg2.get_field() assert np.all(field5 == field2) # From points, multiscale pp = PointSet(2) pp.append(5, 4) pp.append(9, 8) sg = SplineGrid.from_points_multiscale(FD((20, 20)), 2, pp, [8, 7]) field6 = sg.get_field() assert np.all(field6 == field3) # Get field in points values = sg.get_field_in_points(pp) assert list(values) == [field6[int(p[0, 1]), int(p[0, 0])] for p in pp] # Get field in points beyond field pp = PointSet(2) pp.append(100, 103) pp.append(-100, -108) values = sg.get_field_in_points(pp) assert list(values) == [field6[int(p[0, 1]), int(p[0, 0])] for p in pp] # Test copy sg2 = sg.copy() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling assert sg2.grid_sampling_in_pixels == sg.grid_sampling_in_pixels assert sg2.knots is not sg.knots # Test refine sg2 = sg.refine() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling / 2 assert sg2.grid_sampling_in_pixels == tuple( [i / 2 for i in sg.grid_sampling_in_pixels]) # Test resize_field sg2 = sg.resize_field(FD((50, 50))) assert sg2.get_field().shape == (50, 50) # Test addition sg2 = sg.add(sg.add(sg)) field = sg2.get_field() assert np.allclose(field, field6 * 3) print('test_spline_grid_2D ok')
def test_spline_grid_1D_anisotropic(): sg = SplineGrid(FD((90, ), (0.5, )), 4) # field and sampling # Test basic params assert sg.ndim == 1 assert sg.field_shape == (90, ) assert sg.field_sampling == (0.5, ) assert sg.grid_sampling == 4 assert sg.grid_sampling_in_pixels == (8, ) assert sg.knots.shape == (15, ) # Get field, should be empty field = sg.get_field() assert field.shape == sg.field_shape assert np.all(field == 0) # From a field im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) im = Aarray(im, (0.5, )) sg = SplineGrid.from_field(im, 2) field1 = sg.get_field() assert field1.max() > 5.5 assert field1.max() < 7 assert field1.min() > 4 assert field1.min() < 4.2 # From a weighted field im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) ww = np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], np.float32) im = Aarray(im, (0.5, )) sg = SplineGrid.from_field(im, 2, ww) field2 = sg.get_field() assert field2.max() > 7.5 assert field2.max() < 9.5 assert field2.min() > 0 assert field2.min() < 3.5 # From a weighted field, multiscale im = np.array([4, 4, 4, 4, 8, 4, 4, 4, 4, 7, 4, 4, 4, 4], np.float32) ww = np.array([0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], np.float32) im = Aarray(im, (0.5, )) sg = SplineGrid.from_field_multiscale(im, 2, ww) field3 = sg.get_field() assert field3.max() > 7.5 assert field3.max() < 8.5 assert field3.min() > 6 assert field3.min() < 7 # From points pp = PointSet(1) pp.append(4) pp.append(9) pp2 = pp * PointSet((0.5, )) sg = SplineGrid.from_points(FD((14, ), (0.5, )), 2, pp2, [8, 7]) field5 = sg.get_field() assert all(field5 == field2) # From points, multiscale pp = PointSet(1) pp.append(4) pp.append(9) pp2 = pp * PointSet((0.5, )) sg = SplineGrid.from_points_multiscale(FD((14, ), (0.5, )), 2, pp2, [8, 7]) field6 = sg.get_field() assert all(field6 == field3) # Get field in points, note pp2, which is in world coords values = sg.get_field_in_points(pp2) assert list(values) == [field6[int(p[0])] for p in pp] # Get field in samples values2 = sg.get_field_in_samples((pp[:, 0], )) assert list(values) == list(values2) # Test copy sg2 = sg.copy() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling assert sg2.grid_sampling_in_pixels == sg.grid_sampling_in_pixels assert sg2.knots is not sg.knots # Test refine sg2 = sg.refine() assert sg2.field_shape == sg.field_shape assert sg2.field_sampling == sg.field_sampling assert sg2.grid_sampling == sg.grid_sampling / 2 assert sg2.grid_sampling_in_pixels == tuple( [i / 2 for i in sg.grid_sampling_in_pixels]) # Test resize_field sg2 = sg.resize_field(FD((50, ))) assert sg2.get_field().shape == (50, ) # Test addition sg2 = sg.add(sg.add(sg)) field = sg2.get_field() assert np.allclose(field, field6 * 3) print('test_spline_grid_1D_anisotropic ok')
R.append(im[y, x, 0]) G.append(im[y, x, 1]) B.append(im[y, x, 2]) ims[y, x] = im[y, x] # Make three grids spacing = 10 grid1 = SplineGrid(im_r, spacing) grid2 = SplineGrid(im_r, spacing) grid3 = SplineGrid(im_r, spacing) # Put data in grid1._set_using_points(pp, R) grid2._set_using_points(pp, G) grid3._set_using_points(pp, B) # Obtain interpolated image imi[:, :, 0] = grid1.get_field() imi[:, :, 1] = grid2.get_field() imi[:, :, 2] = grid3.get_field() # Show vv.figure(1) vv.clf() vv.subplot(131) vv.imshow(im) vv.subplot(132) vv.imshow(ims) vv.subplot(133) vv.imshow(imi)