Exemple #1
0
    def export_states(self, states, output_dir):
        Z = map(lambda i: [], xrange(self.N)) 

        for n, s in enumerate(states):
            s = Bunch(s)
            for i in xrange(self.N):
                V = s.Vb[0]
                if s.y[i].size > 0:
                    V = V + reduce(add, map(mul, s.y[i], s.Vb[1:]))

                q = quaternion.quat(s.Xg[i].ravel())
                R = quaternion.rotationMatrix(q)
                Rt = np.transpose(R)
                V = s.s[i][0][0] * np.dot(V, Rt) + s.Vd[i][0]
                    
                Q = geometry.path2pos(V, self.T, s.L[i], s.U[i])

                d = dict(T=self.T, V=V,
                         C=self.C[i], P=self.P[i],
                         Q=Q, S=self.S[i],
                         image=self.frames[i],
                         s=s.s[i], Xg=s.Xg[i].ravel())

                Z[i].append(d)

        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        for i, z in enumerate(Z):
            output_path = os.path.join(output_dir, '%d.dat' % i)
            print '-> %s' % output_path
            dump(output_path, dict(has_states=True, states=z))
Exemple #2
0
def test_visualise(V, T, U=None, L=None, S=None):
    vis = VisualiseMesh()
    vis.add_mesh(V, T, L)
    vis.add_image('data/segmentations/circle/0-INV_S.png')

    if U is not None:
        Q = geometry.path2pos(V, T, L, U)
        N = Q.shape[0]
        vis.add_silhouette(Q, np.arange(N), [0, N - 1], S)

    vis.camera_actions(('SetParallelProjection', True))
    vis.execute()
Exemple #3
0
    def solve_iteration():
        status = solve_single_lap_proj_sil_spil(
            V1, T, U, L, C, P, S, SN, Rx, Ry,
            lm_lambdas, lm_preconditioners, narrowBand,
            **solver_options)

        print 'LM Status (%d): ' % status[0], status[1]

        if True:
            Q = geometry.path2pos(V1, T, L, U)
            N = Q.shape[0]

            vis = VisualiseMesh()
            vis.add_mesh(V1, T, L)
            vis.add_silhouette(Q, np.arange(N), [0, N-1], S)
            vis.add_image(frame_path())
            vis.camera_actions(('SetParallelProjection',(True,)))

            vis.execute()

        return status
def main_solve_single_silhouette():
    args = parse_args()
    pprint(args.__dict__)

    arap_solver = np.load(args.solver)

    T = arap_solver.T
    V = arap_solver._s.V1[args.i]
    Vb = [V.copy(),
          np.zeros_like(V)]

    s = np.r_[1.].reshape(-1,1)
    xg = np.r_[0., 0., 0.].reshape(-1,3)
    vd = np.r_[0., 0., 0.].reshape(-1,3)

    # y = np.array(tuple(), dtype=np.float64).reshape(0,1)
    y = np.r_[1.0].reshape(-1,1)

    C = arap_solver.C[args.i]
    P = arap_solver.P[args.i]
    S = arap_solver.S[args.i]
    SN = arap_solver.SN[args.i]

    U = arap_solver._s.U[args.i].copy()
    L = arap_solver._s.L[args.i].copy()

    for i in xrange(args.max_restarts):
        status = solve_single_silhouette(
            T, Vb, s, xg, vd, y, U, L, 
            C, P, S, SN, args.lambdas, args.preconditioners,
            args.narrowband,
            debug=False,
            **args.solver_options)
        print status[1]

        print 'y:'
        print y

        if status[0] not in (0, 4):
            break

    if args.output is not None:
        print '-> %s' % args.output
        dump(args.output, dict(Vb=Vb, y=y, T=T))
        return
        
    V = Vb[0]
    if y.size > 0:
        V = V + reduce(add, map(mul, y, Vb[1:]))

    q = quaternion.quat(xg[0])
    R = quaternion.rotationMatrix(q)
    Rt = np.transpose(R)
    V = s[0][0] * np.dot(V, Rt) + vd[0]

    vis = VisualiseMesh()
    vis.add_mesh(V, T)
    vis.add_image(arap_solver.frames[args.i])

    Q = geometry.path2pos(V, T, L, U)
    vis.add_projection(C, P)
    vis.add_quick_silhouette(Q, S)

    vis.camera_actions(('SetParallelProjection', (True,)))
    vis.execute()
Exemple #5
0
 def silhouette_preimages(self, i):
     return geometry.path2pos(self._s.V1[i], self.T, self._s.L[i],
                              self._s.U[i])
Exemple #6
0
def main_fit_joint_lap_silhouette():
    # use '0b' for initial core geometry
    V = load_projection('0b')[0]
    T = load_triangles()

    # information for silhouette specific only to the model (approx)
    silhouette_info = load_silhouette_info()

    # weighting lambdas
    global_solve_lambdas = np.array(
        [
            1e-3,  # geodesic between preimage
            1e0,  # silhouette projection
            1e3
        ],  # silhouette normal
        dtype=np.float64)

    lm_lambdas = np.asarray(
        np.r_[1.0,  # as-rigid-as-possible
              global_solve_lambdas[1:], 1.0,  # spillage
              1e1],  # laplacian
        dtype=np.float64)

    # preconditioning for the joint minimisation
    lm_preconditioners = np.array([1.0, 1.0, 100.0], dtype=np.float64)

    # other solver options
    solver_options = dict(narrowBand=2,
                          uniformWeights=True,
                          maxIterations=20,
                          gradientThreshold=1e-5,
                          updateThreshold=1e-5,
                          improvementThreshold=1e-5,
                          verbosenessLevel=1)

    # construct lists for minimisation
    (multiX, multiV, multiU, multiL, multiS, multiSN, multiRx,
     multiRy) = [list() for i in range(8)]

    for index, user_constraints in INPUT_SELECTION:
        print 'index:', index

        # load geometry from initial projection
        z = np.load(
            os.path.join(
                OUTPUT_ROOT,
                'chihuahua_single_projection_%s.npz' % user_constraints))
        X = z['X']
        V1 = z['V1']

        # get the silhouette information for the frame
        S, SN = load_silhouette(index)

        # get the spillage information for the frame
        Rx, Ry = load_spillage(index)

        # solve for the initial silhouette positions
        U, L = shortest_path_solve(V1,
                                   T,
                                   S,
                                   SN,
                                   lambdas=global_solve_lambdas,
                                   isCircular=False,
                                   **silhouette_info)

        multiX.append(X)
        multiV.append(V1)
        multiU.append(U)
        multiL.append(L)
        multiS.append(S)
        multiSN.append(SN)
        multiRx.append(Rx)
        multiRy.append(Ry)

    # solve_iteration
    def solve_iteration():
        status = solve_multiview_lap_silhouette(T, V, multiX, multiV, multiU,
                                                multiL, multiS, multiSN,
                                                multiRx, multiRy, lm_lambdas,
                                                lm_preconditioners,
                                                **solver_options)

        print 'LM Status (%d): ' % status[0], status[1]

        return status

    # solve
    t1 = clock()

    count = 0
    status = solve_iteration()
    while status[0] in (0, 4):
        status = solve_iteration()
        count += 1

    t2 = clock()
    print 'Time taken: %.3fs' % (t2 - t1)

    np.savez_compressed(os.path.join(OUTPUT_ROOT,
                                     'chihuahua_lap_silhouette.npz'),
                        T=T,
                        V=V,
                        multiV=multiV,
                        multiS=multiS,
                        multiSN=multiSN,
                        lm_lambdas=lm_lambdas,
                        lm_preconditioners=lm_preconditioners,
                        solver_options=solver_options)

    # visualise ?
    if True:
        vis = visualise.VisualiseMesh(V, T)
        vis.execute()

        for i in xrange(len(multiV)):
            V1 = multiV[i]
            U = multiU[i]
            L = multiL[i]

            Q = geometry.path2pos(V1, T, L, U)
            N = Q.shape[0]

            vis = visualise.VisualiseMesh(V1, T, L)
            vis.add_silhouette(Q, np.arange(N), [0, N - 1], multiS[i])
            vis.add_image(get_frame_path(INPUT_SELECTION[i][0]))

            vis.execute()
Exemple #7
0
def main_fit_joint_arap_silhouette():
    # use '0b' for initial core geometry
    V = load_projection('0b')[0]
    T = load_triangles()

    # information for silhouette specific only to the model (approx)
    silhouette_info = load_silhouette_info()

    global_solve_lambdas = np.array(
        [
            1e-3,  # geodesic between preimage
            1e0,  # silhouette projection
            1e3
        ],  # silhouette normal
        dtype=np.float64)

    lm_lambdas = np.asarray(
        np.r_[1.0,  # as-rigid-as-possible
              global_solve_lambdas[1:]],
        dtype=np.float64)

    lm_preconditioners = np.array([1.0, 1.0, 100.0], dtype=np.float64)

    # construct lists for minimisation
    multiX, multiV, multiU, multiL, multiS, multiSN = [
        list() for i in range(6)
    ]

    for index, user_constraints in INPUT_SELECTION:
        print 'index:', index

        # load geometry from initial projection
        z = np.load(
            os.path.join(
                OUTPUT_ROOT,
                'chihuahua_single_projection_%s.npz' % user_constraints))
        X = z['X']
        V1 = z['V1']

        # get the silhouette information for the frame
        S, SN = load_silhouette(index)

        # solve for the initial silhouette positions
        U, L = shortest_path_solve(V1,
                                   T,
                                   S,
                                   SN,
                                   lambdas=global_solve_lambdas,
                                   isCircular=False,
                                   **silhouette_info)

        multiX.append(X)
        multiV.append(V1)
        multiU.append(U)
        multiL.append(L)
        multiS.append(S)
        multiSN.append(SN)

    # solve_iteration
    def solve_iteration():
        status = solve_multiview_arap_silhouette(T,
                                                 V,
                                                 multiX,
                                                 multiV,
                                                 multiU,
                                                 multiL,
                                                 multiS,
                                                 multiSN,
                                                 lm_lambdas,
                                                 lm_preconditioners,
                                                 narrowBand=2,
                                                 maxIterations=50,
                                                 gradientThreshold=1e-6,
                                                 updateThreshold=1e-6,
                                                 improvementThreshold=1e-6)

        print 'LM Status (%d): ' % status[0], status[1]

        return status

    solve_iteration()

    # visualise ?
    if True:
        vis = visualise.VisualiseMesh(V, T)
        vis.execute()

        for i in xrange(len(multiV)):
            V1 = multiV[i]
            U = multiU[i]
            L = multiL[i]

            Q = geometry.path2pos(V1, T, L, U)
            N = Q.shape[0]

            vis = visualise.VisualiseMesh(V1, T, L)
            vis.add_silhouette(Q, np.arange(N), [0, N - 1], multiS[i])
            vis.add_image(get_frame_path(INPUT_SELECTION[i][0]))

            vis.execute()
Exemple #8
0
def main_fit_single_silhouette():
    # use '0b' for initial core geometry
    V = load_projection('0b')[0]
    T = load_triangles()

    # information for silhouette specific only to the model (approx)
    silhouette_info = load_silhouette_info()

    global_solve_lambdas = np.array(
        [
            1e-3,  # geodesic between preimage
            1e0,  # silhouette projection
            1e3
        ],  # silhouette normal
        dtype=np.float64)

    lm_lambdas = np.r_[2.0,  # laplacian regularisation
                       global_solve_lambdas[1:],  # silhouette
                       1.0]  # spillage

    lm_lambdas = np.asarray(lm_lambdas, dtype=np.float64)
    lm_preconditioners = np.array([1.0, 5.0], dtype=np.float64)

    for i, (index, user_constraints) in enumerate(INPUT_SELECTION):
        if i > 0:
            break
        print 'index:', index

        # load geometry from initial projection
        z = np.load(
            os.path.join(
                OUTPUT_ROOT,
                'chihuahua_single_projection_%s.npz' % user_constraints))
        V = z['V1']

        # get the silhouette information for the frame
        S, SN = load_silhouette(index)

        # get the spillage information for the frame
        Rx, Ry = load_spillage(index)

        # solve for the initial silhouette positions
        U, L = shortest_path_solve(V,
                                   T,
                                   S,
                                   SN,
                                   lambdas=global_solve_lambdas,
                                   isCircular=False,
                                   **silhouette_info)

        # fit under laplacian
        def solve_iteration():
            status = solve_single_lap_silhouette(
                V,
                T,
                U,
                L,
                S,
                SN,
                Rx,
                Ry,
                lm_lambdas,
                lm_preconditioners,
                narrowBand=3,
                maxIterations=20,
                gradientThreshold=1e-5,
                updateThreshold=1e-5,
                improvementThreshold=1e-5,
            )
            print 'LM Status (%d): ' % status[0], status[1]

            return status

        status = solve_iteration()
        while status[0] in (0, 4):
            status = solve_iteration()

        print 'Final Status (%d): ' % status[0], status[1]

        # visualise ?
        if True:
            Q = geometry.path2pos(V, T, L, U)
            N = Q.shape[0]

            vis = visualise.VisualiseMesh(V, T, L)
            vis.add_silhouette(Q, np.arange(N), [0, N - 1], S)
            vis.add_image(get_frame_path(index))

            vis.execute()
Exemple #9
0
def main():
    T = wsp.load_triangles()
    #V, C, P = wsp.load_projection('0f')
    V, C, P = wsp.load_projection('0b')

    single_arap_lambdas = np.array(
        [
            1.0,  # as-rigid-as-possible
            1e1
        ],  # projection
        dtype=np.float64)

    # solve for projection
    X = np.zeros_like(V)
    V1 = V.copy()

    print 'solve_single_arap_proj'
    status = solve_single_arap_proj(V,
                                    T,
                                    X,
                                    V1,
                                    C,
                                    P,
                                    single_arap_lambdas,
                                    maxIterations=20,
                                    gradientThreshold=1e-6,
                                    updateThreshold=1e-6,
                                    improvementThreshold=1e-6)
    print 'Status (%d): ' % status[0], status[1]

    # show
    vis = visualise.VisualiseMesh(V1, T)
    vis.add_image(wsp.get_frame_path(0))
    vis.add_projection(C, P)
    vis.execute()

    # solve for Laplacian
    global_sil_lambdas = np.array(
        [
            1e0,  # geodesic between preimage points
            1e0,  # silhouette projection
            1e3
        ],  # silhouette normal
        dtype=np.float64)

    #lm_lambdas = np.r_[1e1, 0.0, global_sil_lambdas[1:]]
    lm_lambdas = np.r_[1e1, global_sil_lambdas[1:]]
    lm_lambdas = np.asarray(lm_lambdas, dtype=np.float64)
    print 'lm_lambdas:', lm_lambdas

    lm_preconditioners = np.array([1.0, 100.0], dtype=np.float64)
    print 'lm_preconditioners :', lm_preconditioners

    # use as initialisation for next stage
    V = V1.copy()

    # solve for silhouette
    silhouette_info = wsp.load_silhouette_info()
    S, SN = wsp.get_silhouette(0)

    print 'shortest_path_solve'
    U, L = shortest_path_solve(V,
                               T,
                               S,
                               SN,
                               lambdas=global_sil_lambdas,
                               isCircular=True,
                               **silhouette_info)
    # show
    vis = visualise.VisualiseMesh(V, T, L)
    vis.add_image(wsp.get_frame_path(0))

    Q = geometry.path2pos(V, T, L, U)
    N = Q.shape[0]

    vis.add_silhouette(Q, np.arange(N), [0, N - 1], S)
    vis.add_projection(C, P)
    vis.execute()

    # fit under laplacian
    def solve_iteration():
        status = solve_single_lap_silhouette(
            V,
            T,
            U,
            L,
            S,
            SN,
            lm_lambdas,
            lm_preconditioners,
            narrowBand=3,
            maxIterations=50,
            gradientThreshold=1e-6,
            updateThreshold=1e-6,
            improvementThreshold=1e-6,
            verbosenessLevel=1,
            useAsymmetricLambda=True,
        )
        print 'LM Status (%d): ' % status[0], status[1]
        #status = solve_single_lap_proj_silhouette(V, T, U, L, C, P, S, SN,
        #    lm_lambdas,
        #    lm_preconditioners,
        #    narrowBand=2,
        #    maxIterations=50,
        #    gradientThreshold=1e-6,
        #    updateThreshold=1e-6,
        #    improvementThreshold=1e-6,
        #    verbosenessLevel=1,
        #    useAsymmetricLambda=True,
        #    )
        #print 'LM Status (%d): ' % status[0], status[1]

        return status

    #for i in xrange(10):
    #    status = solve_iteration()
    count = 0
    while status[0] in (0, 4) and count < 5:
        status = solve_iteration()
        count += 1

    # show
    vis = visualise.VisualiseMesh(V, T, L)
    vis.add_image(wsp.get_frame_path(0))

    Q = geometry.path2pos(V, T, L, U)
    N = Q.shape[0]

    vis.add_silhouette(Q, np.arange(N), [0, N - 1], S)
    vis.add_projection(C, P)
    vis.execute()