def __init__(self, im): # Make Aarray im = Aarray(im) # Create pyramid self._p = ScaleSpacePyramid(im) maxLevel, maxSigma = self._p.calculate() # Init visualization vv.figure(1) vv.clf() self._axes = axes = vv.gca() axes.position.Correct(dy=40, dh=-40) # Make slider self._slider = vv.Slider(axes) self._slider.position = 0, -40, 1, 20 self._slider.fullRange = 0, maxLevel self._slider.value = 0 # Show image self._t = vv.imshow(self._p.get_level(0)) # Bind to handler self._slider.eventSliding.Bind(self.on_sliding) self._slider.eventSliderChanged.Bind(self.on_sliding)
def __init__(self, im, min_scale=None, scale_offset=0): # Make Aarray if True: # not isinstance(im, Aarray): im = Aarray(im) # Create pyramids self._p1 = ScaleSpacePyramid(im, min_scale, scale_offset, level_factor=1.5) self._p2 = ScaleSpacePyramid(im, min_scale, scale_offset, level_factor=2) self._p3 = ScaleSpacePyramid(im, min_scale, scale_offset, level_factor=3) #maxLevel, maxSigma = self._p1.calculate() #self._p2.calculate() #self._p3.calculate() # Init visualization fig = vv.figure(1) vv.clf() self._axes1 = axes1 = vv.subplot(131) vv.title('level factor 1.5') self._axes2 = axes2 = vv.subplot(132) vv.title('level factor 2.0') self._axes3 = axes3 = vv.subplot(133) vv.title('level factor 3.0') axes1.position.Correct(dy=40, dh=-40) axes2.position.Correct(dy=40, dh=-40) axes3.position.Correct(dy=40, dh=-40) # Share camera cam = vv.cameras.TwoDCamera() for ax in [axes1, axes2, axes3]: ax.camera = cam # Make slider self._slider = vv.Slider(fig) self._slider.position = 0.1, 5, 0.8, 20 self._slider.fullRange = 0, 25 self._slider.value = 1 # Show image self._t1 = vv.imshow(self._p1.get_level(0), axes=axes1) self._t2 = vv.imshow(self._p2.get_level(0), axes=axes2) self._t3 = vv.imshow(self._p3.get_level(0), axes=axes3) # Bind to handler self._slider.eventSliding.Bind(self.on_sliding) self._slider.eventSliderChanged.Bind(self.on_sliding)
polypp.append(i, polynom[0]*i**2 + polynom[1]*i + polynom[2]) # Visualize vv.subplot(121) vv.plot([-1,0,1],pp) vv.plot([t_max, t_max], [0,1], lc='r') vv.plot(polypp, lc='r') ## 2D # Input and result im = vv.imread('astronaut.png')[::,::,2].copy() Y,X = np.where(im==im.max()) Y,X = Y[0], X[0] im[Y-1,X] -= 20 # make more interesting :) mat = im[Y-1:Y+2, X-1:X+2] # Get result and scale mat ymin, ymax, surface = fitting.fit_lq2(mat, True) mat = Aarray(mat, sampling=(100, 100), origin=(50, 50)) # Visualize vv.subplot(122) vv.imshow(mat) m = vv.surf(surface) a = vv.gca() a.SetLimits() m.colormap = vv.CM_MAGMA
im1 = self._p1.get_scale(sigma) im2 = self._p2.get_scale(sigma) im3 = self._p3.get_scale(sigma) # Replace textures self._t1.SetData(im1) self._t2.SetData(im2) self._t3.SetData(im3) if __name__ == '__main__': import visvis as vv # Read image im = vv.imread('astronaut.png')[:, :, 1].astype(np.float32) im = Aarray(im)[::2, :] d = Demo2D3(im, 1.5) vv.use().Run() if 0: ## Diffusionkernel vs Gaussiankernel import pirt import visvis as vv sigma = 300 k1, t1 = pirt.diffusionkernel(sigma, returnt=True) k2, t2 = pirt.gaussiankernel(sigma, returnt=True) vv.figure()
which are composed and inverted. """ import numpy as np import visvis as vv from pirt import Aarray, PointSet, DeformationFieldBackward # Parameters, play with these! INJECTIVE = True FREEZE_EDGES = True # Create test image with one block in the corner im0 = np.zeros((100, 100), np.float32) im0 = Aarray(im0, (0.66, 1.0)) im0[30:40, 40:50] = 1.0 # Draw a grid on it grid_step = 10 im0[1::grid_step,:] = 0.3 im0[:,1::grid_step] = 0.3 # Define three locations, to move the block between pp = PointSet(2) pp.append(45, 35) pp.append(45, 85) pp.append(85, 85) pp = pp * PointSet(reversed(im0.sampling)) # Get down-deformation and right-deformation
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_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')