Beispiel #1
0
def RANSACFit(p1,
              p2,
              match,
              maxIter=200,
              seedSetSize=None,
              maxInlierError=30,
              goodFitThresh=None):
    '''
    RANSACFit Use RANSAC to find a robust affine transformation
    Input:
    p1: N1 * 2 matrix, each row is a point
    p2: N2 * 2 matrix, each row is a point
    match: M * 2 matrix, each row represents a match [index of p1, index of p2]
    maxIter: the number of iterations RANSAC will run
    seedNum: The number of randomly-chosen seed points that we'll use to fit
    our initial circle
    maxInlierError: A match not in the seed set is considered an inlier if
                its error is less than maxInlierError. Error is
                measured as sum of Euclidean distance between transformed 
                point1 and point2. You need to implement the
                ComputeCost function.
                
    goodFitThresh: The threshold for deciding whether or not a model is
               good; for a model to be good, at least goodFitThresh
               non-seed points must be declared inliers.
               
    Output:
        H: a robust estimation of affine transformation from p1 to p2
    '''

    # Check parameters
    N = len(match)
    if N < 3:
        sys.exit('not enough matches to produce a transformation matrix')
    if not seedSetSize:
        seedSetSize = np.max([np.ceil(0.2 * N), 3])
    if not goodFitThresh:
        goodFitThresh = np.floor(0.7 * N)
    '''
    below is an obfuscated version of RANSAC. You don't need to
    edit any of this code, just the ComputeError() function below
    '''
    H = np.eye(3)
    iota = np.inf
    kappa = 0
    lamb = iota
    alpha = int(seedSetSize)

    for i in range(maxIter):
        [beta, gamma] = part(match, alpha)
        eta = ComputeAffineMatrix(p1[beta[:, 0], :], p2[beta[:, 1], :])
        delta = ComputeError(eta, p1, p2, gamma)
        epsilon = delta <= maxInlierError

        if np.sum(epsilon) + alpha >= goodFitThresh:
            zeta = np.concatenate([beta, gamma[epsilon, :]], axis=0)

            eta = ComputeAffineMatrix(p1[zeta[:, 0], :], p2[zeta[:, 1], :])
            theta = np.sum(ComputeError(eta, p1, p2, zeta))
            if theta < iota:
                H = eta
                kappa = lamb
                iota = theta

    if np.sum(np.square(H - np.eye(3))) == 0:
        print('No RANSAC fit was found.')

    return H