def test_recover_Gk_no_noise(): # Generate some synthetic data. n_frames = 30 n_basis = 2 n_points = 15 gt_model = model.generate_synthetic_model(n_frames, n_basis, n_points) # Factor the matrix. from rigid import factor_matrix W = gt_model.W.copy() W -= W.mean(axis=-1)[:, np.newaxis] M_hat, B_hat = factor_matrix(W, J=n_basis * 3) # Try to find a G_k G_k = shape.find_G_k(M_hat) # Get the k'th column triple of M_k M_k = np.dot(M_hat, G_k) M_kx = M_k[0::2] M_ky = M_k[1::2] # Ensure they are orthogonal. npt.assert_allclose((M_kx * M_ky).sum(), 0, atol=1e-3) # And that the length of x and y are equal. npt.assert_allclose((M_kx * M_kx).sum() - (M_ky * M_ky).sum(), 0, atol=1e-3)
def factor(W, n_basis=2, use_method_1=False): """Factor's W using Dai, Y., Li, H. and He, M. "A Simple Prior-free Method for Non-Rigid Structure-from-Motion Factorization". CVPR, 2012 Note that we use the letter M in place of PI from this paper. """ n_frames = W.shape[0] / 2 # Estimate 2D translation. T = W.mean(axis=-1) Ts = np.zeros((n_frames, 3)) Ts[:, :2] = T.reshape(n_frames, 2) # Center observation matrix W_cent = W.copy() - T[:, np.newaxis] # Factor M_hat, B_hat = rigid.factor_matrix(W_cent, n_basis * 3) # Recover G_k G_k = find_G_k(M_hat) # Recover M_k M_k = np.dot(M_hat, G_k) # Recover R Rs = Rs_from_M_k(M_k) if use_method_1: # Recover S S = S_from_Rs_method_1(W_cent, Rs) # Build the scene. scene = Scene(S=S, Rs=Rs, Ts=Ts) else: # Recover S_sharp S_sharp = S_sharp_from_Rs(W_cent, Rs) # Factor to C and B matrices. C, B = factor_S_sharp_to_C_and_B(S_sharp, n_basis) # Build linear model. scene = model.BasisShapeModel(Rs, Bs=B.reshape(n_basis, 3, B.shape[1]), C=C, Ts=Ts) return scene
def factor(W, n_basis=2, use_method_1 = False): """Factor's W using Dai, Y., Li, H. and He, M. "A Simple Prior-free Method for Non-Rigid Structure-from-Motion Factorization". CVPR, 2012 Note that we use the letter M in place of PI from this paper. """ n_frames = W.shape[0]/2 # Estimate 2D translation. T = W.mean(axis=-1) Ts = np.zeros((n_frames,3)) Ts[:,:2] = T.reshape(n_frames,2) # Center observation matrix W_cent = W.copy() - T[:,np.newaxis] # Factor M_hat, B_hat = rigid.factor_matrix(W_cent, n_basis*3) # Recover G_k G_k = find_G_k(M_hat) # Recover M_k M_k = np.dot(M_hat, G_k) # Recover R Rs = Rs_from_M_k(M_k) if use_method_1: # Recover S S = S_from_Rs_method_1(W_cent, Rs) # Build the scene. scene = Scene(S = S, Rs = Rs, Ts = Ts) else: # Recover S_sharp S_sharp = S_sharp_from_Rs(W_cent, Rs) # Factor to C and B matrices. C, B = factor_S_sharp_to_C_and_B(S_sharp, n_basis) # Build linear model. scene = model.BasisShapeModel(Rs, Bs = B.reshape(n_basis, 3, B.shape[1]), C=C, Ts=Ts) return scene