def _recursive_procrustes(self): r""" Recursively calculates a procrustes alignment. """ from menpo.shape import mean_pointcloud from menpo.transform import Similarity if self.n_iterations > self.max_iterations: return False new_tgt = mean_pointcloud([t.aligned_source.points for t in self.transforms]) # rescale the new_target to be the same size as the original about # it's centre rescale = Similarity.identity(new_tgt.n_dims) s = UniformScale(self.initial_target_scale / new_tgt.norm(), self.n_dims, skip_checks=True) t = Translation(-new_tgt.centre, skip_checks=True) rescale.compose_before_inplace(t) rescale.compose_before_inplace(s) rescale.compose_before_inplace(t.pseudoinverse) rescale.apply_inplace(new_tgt) # check to see if we have converged yet delta_target = np.linalg.norm(self.target.points - new_tgt.points) if delta_target < 1e-6: return True else: self.n_iterations += 1 for t in self.transforms: t.set_target(new_tgt) self.target = new_tgt return self._recursive_procrustes()
def noisy_align(source, target, noise_std=0.04, rotation=False): r""" Constructs and perturbs the optimal similarity transform between source to the target by adding white noise to its weights. Parameters ---------- source: :class:`menpo.shape.PointCloud` The source pointcloud instance used in the alignment target: :class:`menpo.shape.PointCloud` The target pointcloud instance used in the alignment noise_std: float The standard deviation of the white noise Default: 0.04 rotation: boolean If False the second parameter of the Similarity, which captures captures inplane rotations, is set to 0. Default:False Returns ------- noisy_transform : :class: `menpo.transform.Similarity` The noisy Similarity Transform """ transform = AlignmentSimilarity(source, target, rotation=rotation) parameters = transform.as_vector() parameter_range = np.hstack((parameters[:2], target.range())) noise = (parameter_range * noise_std * np.random.randn(transform.n_parameters)) return Similarity.identity(source.n_dims).from_vector(parameters + noise)
def _recursive_procrustes(self): r""" Recursively calculates a procrustes alignment. """ from menpo.shape import mean_pointcloud from menpo.transform import Similarity if self.n_iterations > self.max_iterations: return False new_tgt = mean_pointcloud( [t.aligned_source.points for t in self.transforms]) # rescale the new_target to be the same size as the original about # it's centre rescale = Similarity.identity(new_tgt.n_dims) s = UniformScale(self.initial_target_scale / new_tgt.norm(), self.n_dims, skip_checks=True) t = Translation(-new_tgt.centre, skip_checks=True) rescale.compose_before_inplace(t) rescale.compose_before_inplace(s) rescale.compose_before_inplace(t.pseudoinverse) rescale.apply_inplace(new_tgt) # check to see if we have converged yet delta_target = np.linalg.norm(self.target.points - new_tgt.points) if delta_target < 1e-6: return True else: self.n_iterations += 1 for t in self.transforms: t.set_target(new_tgt) self.target = new_tgt return self._recursive_procrustes()
def test_similarity_jacobian_2d(): params = np.ones(4) t = Similarity.identity(2).from_vector(params) explicit_pixel_locations = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]) dW_dp = t.jacobian(explicit_pixel_locations) assert_equal(dW_dp, sim_jac_solution2d)
def test_similarity_2d_from_vector(): params = np.array([0.2, 0.1, 1, 2]) h**o = np.array([[params[0] + 1, -params[1], params[2]], [params[1], params[0] + 1, params[3]], [0, 0, 1]]) sim = Similarity.identity(2).from_vector(params) assert_equal(sim.h_matrix, h**o)
def test_similarity_jacobian_2d(): params = np.ones(4) t = Similarity.identity(2).from_vector(params) explicit_pixel_locations = np.array( [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]) dW_dp = t.d_dp(explicit_pixel_locations) assert_equal(dW_dp, sim_jac_solution2d)
def test_similarity_2d_points_raises_dimensionalityerror(): params = np.ones(4) t = Similarity.identity(2).from_vector(params) t.d_dp(np.ones([2, 3]))
def test_similarity_identity_3d(): assert_allclose(Similarity.identity(3).h_matrix, np.eye(4))