Esempio n. 1
0
def find_labels(x2ds, model, x2ds_indices, model_indices, threshold,
                labels_out):
    A, B = fit_points(x2ds[x2ds_indices], model[model_indices])
    print 'cf', np.dot(x2ds[x2ds_indices], A) + B - model[model_indices]
    cloud = ISCV.HashCloud2D(model, threshold)
    L = np.dot(x2ds, A) + B
    scores, matches, matches_splits = cloud.score(L)
    sc = ISCV.min_assignment_sparse(scores, matches, matches_splits,
                                    threshold**2, labels_out)
    ms = np.sum(labels_out != -1)
    return ((sc - (len(x2ds) - len(model)) * (threshold**2)) /
            len(model))**0.5, ms
Esempio n. 2
0
def getMapping(hi_geo, triangles, lo_geo, threshold=20.0):
    '''given a hi-res geometry and topology, and a lo-res geometry, find the triangles and barycentric weights that
	when applied to the hi-res geometry, best fit the lo-res geometry.
	The mapping is returned as a list of weight triples and a list of index triples, per vertex.
	The output vertex is the weighted sum of the extracted indicated source vertices.'''
    is3D = (hi_geo.shape[1] == 3)
    lunterp = lunterp3D if is3D else lunterp2D
    numVertsHi = hi_geo.shape[0]
    numVertsLo = lo_geo.shape[0]
    weights = np.zeros((numVertsLo, 3), dtype=np.float32)
    indices = -np.ones((numVertsLo, 3), dtype=np.int32)
    import ISCV
    cloud = ISCV.HashCloud3D(hi_geo, threshold) if is3D else ISCV.HashCloud2D(
        hi_geo, threshold)
    scores, matches, matches_splits = cloud.score(lo_geo.copy())
    # the indices of the closest 3 hi verts to each lo vert
    D = [
        matches[m0 + np.argsort(scores[m0:m1])[:3]] if m0 != m1 else []
        for m0, m1 in zip(matches_splits[:-1], matches_splits[1:])
    ]
    # for speed-up, compute all the triangles involving each hi vertex.
    T = [[] for x in xrange(numVertsHi)]
    for ti, tri in enumerate(triangles):
        for tj in tri:
            T[tj].append(tri)
    bads = []
    for vi, (lo_x, nearIndices, ws,
             xis) in enumerate(zip(lo_geo, D, weights, indices)):
        best = -10
        for ni in nearIndices:
            for tri in T[ni]:
                xws = lunterp(hi_geo[tri], lo_x)
                sc = np.min(xws)
                if sc > best:  # pick the best triangle (the one that it's closest to being inside)
                    best = sc
                    xis[:] = tri
                    ws[:] = xws
                    if best >= 0: break
            if best >= 0: break
        # the vertex *might* not be inside any of these triangles
        if best < -0.1:
            bads.append(vi)
            ws[:] = 0.0  # ensure there's no weight
            xis[:] = -1  # and no label
    if len(bads):
        print 'vertices outside', len(bads)
        print bads[:10], '...'
    which = np.where(indices[:, 0] != -1)[0]
    print len(which), 'vertices inside'
    return which, weights[which], indices[which]