def test_basic_2d_similarity(): linear_component = np.array([[2, -6], [6, 2]]) translation_component = np.array([7, -8]) h_matrix = np.eye(3, 3) h_matrix[:-1, :-1] = linear_component h_matrix[:-1, -1] = translation_component similarity = SimilarityTransform(h_matrix) x = np.array([[0, 1], [1, 1], [-1, -5], [3, -5]]) # transform x explicitly solution = np.dot(x, linear_component.T) + translation_component # transform x using the affine transform result = similarity.apply(x) # check that both answers are equivalent assert_allclose(solution, result) # create several copies of x x_copies = np.array([x, x, x, x, x, x, x, x]) # transform all of copies at once using the affine transform results = similarity.apply(x_copies) # check that all copies have been transformed correctly for r in results: assert_allclose(solution, r)
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 = SimilarityTransform.identity(2).from_vector(params) assert_equal(sim.h_matrix, h**o)
def test_align_2d_similarity(): linear_component = np.array([[2, -6], [6, 2]]) translation_component = np.array([7, -8]) h_matrix = np.eye(3, 3) h_matrix[:-1, :-1] = linear_component h_matrix[:-1, -1] = translation_component similarity = SimilarityTransform(h_matrix) source = PointCloud(np.array([[0, 1], [1, 1], [-1, -5], [3, -5]])) target = similarity.apply(source) # estimate the transform from source and target estimate = SimilarityTransform.align(source, target) # check the estimates is correct assert_allclose(similarity.h_matrix, estimate.h_matrix)
def test_similarity_jacobian_2d(): params = np.ones(4) t = SimilarityTransform.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 noisy_align(source, target, noise_std=0.05, rotation=False): r""" Constructs and perturbs the optimal similarity transform between source to the target by adding white noise to its parameters. 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.05 rotation: boolean If False the second parameter of the SimilarityTransform, which captures captures inplane rotations, is set to 0. Default:False Returns ------- noisy_transform : :class: `menpo.transform.SimilarityTransform` The noisy Similarity Transform """ transform = SimilarityTransform.align(source, target) parameters = transform.as_vector() if not rotation: parameters[1] = 0 parameter_range = np.hstack((parameters[:2], target.range())) noise = (parameter_range * noise_std * np.random.randn(transform.n_parameters)) parameters += noise return SimilarityTransform.identity(source.n_dims).from_vector_inplace( parameters)
def test_similarity_identity_3d(): assert_allclose(SimilarityTransform.identity(3).h_matrix, np.eye(4))
def test_similarity_2d_points_raises_dimensionalityerror(): params = np.ones(4) t = SimilarityTransform.identity(2).from_vector(params) t.jacobian(np.ones([2, 3]))
def test_similarity_jacobian_3d_raises_dimensionalityerror(): t = SimilarityTransform(np.eye(4)) t.jacobian(np.ones([2, 3]))