def _build_reference_frame(landmarks, boundary=3, group='source'): # translate landmarks to the origin minimum = landmarks.bounds(boundary=boundary)[0] landmarks = Translation(-minimum).apply(landmarks) resolution = landmarks.range(boundary=boundary) reference_frame = MaskedImage.blank(resolution) reference_frame.landmarks[group] = landmarks return reference_frame
def test_align_2d_translation(): t_vec = np.array([1, 2]) translation = Translation(t_vec) source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]])) target = translation.apply(source) # estimate the transform from source and target estimate = Translation.align(source, target) # check the estimates is correct assert_allclose(translation.h_matrix, estimate.h_matrix)
def chain_compose_before_tps_test(): a = PointCloud(np.random.random([10, 2])) b = PointCloud(np.random.random([10, 2])) tps = TPS(a, b) t = Translation([3, 4]) s = Scale([4, 2]) chain = TransformChain([t, s]) chain_mod = chain.compose_before(tps) points = PointCloud(np.random.random([10, 2])) manual_res = tps.apply(s.apply(t.apply(points))) chain_res = chain_mod.apply(points) assert(np.all(manual_res.points == chain_res.points))
def test_translation_2d_from_vector(): params = np.array([1, 2]) h**o = np.array([[1, 0, params[0]], [0, 1, params[1]], [0, 0, 1]]) tr = Translation.identity(2).from_vector(params) assert_equal(tr.h_matrix, h**o)
def test_translation_identity_3d(): assert_allclose(Translation.identity(3).h_matrix, np.eye(4))
def test_translation(): t_vec = np.array([1, 2, 3]) starting_vector = np.random.rand(10, 3) transform = Translation(t_vec) transformed = transform.apply(starting_vector) assert_allclose(starting_vector + t_vec, transformed)
def crop(self, min_indices, max_indices, constrain_to_boundary=True): r""" Crops this image using the given minimum and maximum indices. Landmarks are correctly adjusted so they maintain their position relative to the newly cropped image. Parameters ----------- min_indices: (n_dims, ) ndarray The minimum index over each dimension max_indices: (n_dims, ) ndarray The maximum index over each dimension constrain_to_boundary: boolean, optional If True the crop will be snapped to not go beyond this images boundary. If False, an ImageBoundaryError will be raised if an attempt is made to go beyond the edge of the image. Default: True Returns ------- cropped_image : :class:`type(self)` This image, but cropped. Raises ------ ValueError min_indices and max_indices both have to be of length n_dims. All max_indices must be greater than min_indices. ImageBoundaryError Raised if constrain_to_boundary is False, and an attempt is made to crop the image in a way that violates the image bounds. """ min_indices = np.floor(min_indices) max_indices = np.ceil(max_indices) if not (min_indices.size == max_indices.size == self.n_dims): raise ValueError( "Both min and max indices should be 1D numpy arrays of" " length n_dims ({})".format(self.n_dims)) elif not np.all(max_indices > min_indices): raise ValueError("All max indices must be greater that the min " "indices") min_bounded = self.constrain_points_to_bounds(min_indices) max_bounded = self.constrain_points_to_bounds(max_indices) if not constrain_to_boundary and not ( np.all(min_bounded == min_indices) or np.all(max_bounded == max_indices)): # points have been constrained and the user didn't want this - raise ImageBoundaryError(min_indices, max_indices, min_bounded, max_bounded) slices = [slice(min_i, max_i) for min_i, max_i in zip(list(min_bounded), list(max_bounded))] self.pixels = self.pixels[slices].copy() # update all our landmarks lm_translation = Translation(-min_bounded) lm_translation.apply_inplace(self.landmarks) return self