Ejemplo n.º 1
0
    def testRemapPointsToSegments01(self):
        points  = np.array([[1, 2], [3, 4], [5, 6]])
        indices = np.array([[0, 1], [2, 0]])

        res     = contourloss.remapPointsToSegements(points, indices)
        ans     = np.array([
            [[1, 2], [3, 4]],
            [[5, 6], [1, 2]]
        ])
        self.assertEqual(res.shape, ans.shape)
        self.assertTrue(np.allclose(res, ans))
Ejemplo n.º 2
0
def mkErrorFunction(linePoints, segsInds, targetPoints):
    segments = contourloss.remapPointsToSegements(linePoints, segsInds)
    targetPoints2SegmentsMap = searchnearestsegment.findNearestSegmentForPoints(targetPoints, segments)
    targetPoints2SegmentsWeights = np.ones(len(targetPoints), dtype=np.float32)

    eqWeight = 0.1 * len(targetPoints) * float(len(segsInds))
    print 'eqWeight', eqWeight

    def loss(linePoints):
        return contourloss.contourFittingLoss(
            linePoints, segsInds, 
            targetPoints, targetPoints2SegmentsMap, targetPoints2SegmentsWeights,
            [], [], eqWeight)
    return loss
Ejemplo n.º 3
0
def main2():
    targetPoints, _ = segmentgeneration.mkSphericalyDistributedClosedSegments(600)
    targetPoints *= np.array([2.0, 1.2])

    targetPoints += np.random.normal(size=targetPoints.shape, scale=0.02)

    nSegments = 8
    linePoints, segsInds = segmentgeneration.mkSphericalyDistributedClosedSegments(nSegments)

    drawScene(linePoints, segsInds, targetPoints)
    nSubdivisions = 5
    nICPIters = 10
    nSolverIters = 20
    for subdivisionIter in range(nSubdivisions):
        if subdivisionIter > 0:
            segments = contourloss.remapPointsToSegements(linePoints, segsInds)
            eqErr = contourloss.segmentsLengthEquallityAll2AllLoss(segments)
            print 'eqErr before subdiv', eqErr
            linePoints, segsInds = segmentgeneration.splitExistingSegmentsByAverage(linePoints, segsInds)
            segments = contourloss.remapPointsToSegements(linePoints, segsInds)
            eqErr = contourloss.segmentsLengthEquallityAll2AllLoss(segments)
            print 'eqErr after subdiv', eqErr
            drawScene(linePoints, segsInds, targetPoints)
        for icpIter in range(nICPIters):
            f = mkErrorFunction(linePoints, segsInds, targetPoints)
            #df = None
            df = grad(f)
            res = minimize(f, linePoints.reshape(-1), jac=df, method='bfgs', options={'maxiter': nSolverIters})
            print icpIter, '/', nICPIters
            print res['nfev'], res['nit'], res['njev'], res['fun']

            linePoints = res['x'].reshape((-1, 2))
            drawScene(linePoints, segsInds, targetPoints)
            segments = contourloss.remapPointsToSegements(linePoints, segsInds)
            eqErr = contourloss.segmentsLengthEquallityAll2AllLoss(segments)
            print 'eqErr', eqErr
    cv2.waitKey()
Ejemplo n.º 4
0
def drawScene(linePoints, segsInds, targetPoints):
    canvasSize = (800, 800)
    scale = 170.0
    canvas = drawtools.mkCanvas(canvasSize)
    drawtools.drawPoints(canvas, targetPoints, scale=scale, circleRadius=2, circleThickness=2, color=(0, 0, 255))
    drawtools.drawContour(canvas, linePoints, segsInds, scale=scale, circleRadius=2, circleThickness=2)

    segments = contourloss.remapPointsToSegements(linePoints, segsInds)
    targetPoints2SegmentsMap = searchnearestsegment.findNearestSegmentForPoints(
            targetPoints, segments)
    corrsLines = correspondeceLines(segments, targetPoints, targetPoints2SegmentsMap)
    drawtools.drawSegments(canvas, corrsLines, scale, color=(255, 0, 0))

    cv2.imshow('target', canvas)
    cv2.waitKey(50)
Ejemplo n.º 5
0
def main1():
    targetPoints, _ = segmentgeneration.mkSphericalyDistributedClosedSegments(300)
    targetPoints *= np.array([2.0, 1.2])

    nSegments = 64
    linePoints, segsInds = segmentgeneration.mkSphericalyDistributedClosedSegments(nSegments)

    f = mkErrorFunction(linePoints, segsInds, targetPoints)
    #df = None
    df = grad(f)
    res = minimize(f, linePoints.reshape(-1), jac=df, method='bfgs')
    print res

    drawScene(linePoints, segsInds, targetPoints)
    linePoints = res['x'].reshape((-1, 2))
    drawScene(linePoints, segsInds, targetPoints)
    segments = contourloss.remapPointsToSegements(linePoints, segsInds)
Ejemplo n.º 6
0
def drawContour(canvas,
                points,
                segmentsInds,
                scale=1.0,
                offset=None,
                circleRadius=2,
                circleThickness=2,
                lineColor=(0, 255, 0),
                drawCenterOfCoords=False):
    if offset is None:
        offset = canvasOffsetAsHalfOfCanvas(canvas)
    points = points * scale + offset

    segments = contourloss.remapPointsToSegements(points, segmentsInds)

    for p1, p2 in segments:
        p1 = vert2Point(p1)
        p2 = vert2Point(p2)
        cv2.line(canvas, p1, p2, lineColor)

    for p in points.tolist() + ([offset] if drawCenterOfCoords else []):
        p = vert2Point(p)
        cv2.circle(canvas, p, circleRadius, (0, 255, 0), circleThickness)
    return canvas
Ejemplo n.º 7
0
        newSegmentIndices.append((s2, s3))
    return np.array(points.tolist() + newPoints), np.array(newSegmentIndices)

if __name__ == '__main__':
    import cv2
    import contourloss
    import drawtools
    nSegs = 50

    points, segmentsInds = mkSphericalyDistributedClosedSegments(nSegs)
    print points
    print segmentsInds

    canvasSize = (512, 512)
    scale = 200
    canvas = np.ones(canvasSize + (3,), dtype=np.uint8) * 255
    segments = contourloss.remapPointsToSegements(points, segmentsInds)
    drawtools.drawContour(canvas, points, segmentsInds, scale, drawCenterOfCoords=True)
    cv2.imshow('', canvas)
    cv2.waitKey()

    points, segmentsInds = splitExistingSegmentsByAverage(points, segmentsInds)
    drawtools.drawContour(canvas, points, segmentsInds, scale, drawCenterOfCoords=True)

    #drawtools.drawSegments(canvas, segments, scale=scale)
    #drawtools.drawPoints(canvas, points, scale=scale, drawCenterOfCoords=True)

    cv2.imshow('', canvas)
    cv2.waitKey()