def RANSAC(listOfCorrespondences, Niter=1000, epsilon=4, acceptableProbFailure=1e-9): """H_best: the best estimation of homorgraphy (3-by-3 matrix)""" """inliers: A list of booleans that describe whether the element in listOfCorrespondences an inlier or not""" """ 6.815 can bypass acceptableProbFailure""" numInliers = 0 inliers = [False] * len(listOfCorrespondences) H_best = np.zeros((3, 3)) for n in xrange(Niter): correspondences = random.sample(listOfCorrespondences, 4) listOfPairs = A7PairsToA6Pairs(correspondences) H = a6.computeHomography(listOfPairs) currentInliers = [] for c in listOfCorrespondences: estimate = H.dot(A7PointToA6Point(c.pt1)) estimate[0], estimate[1] = (estimate[0] / estimate[2], estimate[1] / estimate[2]) actual = A7PointToA6Point(c.pt2) error = math.sqrt(sum((estimate - actual) ** 2)) currentInliers.append(bool(error < epsilon)) currentNumInliers = sum(bool(x) for x in currentInliers) if currentNumInliers > numInliers: numInliers = currentNumInliers inliers = currentInliers H_best = H.copy() if (1 - (float(numInliers) / len(listOfCorrespondences)) ** 4) ** n < acceptableProbFailure: break return (H_best, inliers)
def RANSAC(listOfCorrespondences, Niter=1000, epsilon=4, acceptableProbFailure=1e-9): """H_best: the best estimation of homorgraphy (3-by-3 matrix) inliers: list of booleans that describe whether the element in listOfCorrespondences an inlier or not""" number_of_correspondences = len(listOfCorrespondences) H_best = None inliers = [] number_of_inliers = 0 for i in range(Niter): feature_pairs = random.sample(listOfCorrespondences, 4) homography = a6.computeHomography(A7PairsToA6Pairs(feature_pairs)) loop_inliers = [] for correspondence in listOfCorrespondences: transformed_point = np.dot(homography, A7PointToA6Point(correspondence.pt1)) transformed_point[0] /= transformed_point[2] transformed_point[1] /= transformed_point[2] target_point = A7PointToA6Point(correspondence.pt2) loop_inliers.append(math.sqrt(np.sum((transformed_point - target_point) ** 2)) < epsilon) loop_inliers_count = loop_inliers.count(True) if loop_inliers_count > number_of_inliers: number_of_inliers = loop_inliers_count inliers = loop_inliers H_best = homography if ((1 - (loop_inliers_count / number_of_correspondences) ** 4) ** i) < acceptableProbFailure: break return (H_best, inliers)
def RANSAC(listOfCorrespondences, Niter=1000, epsilon=4, acceptableProbFailure=1e-9): '''H_best: the best estimation of homorgraphy (3-by-3 matrix)''' '''inliers: A list of booleans that describe whether the element in listOfCorrespondences an inlier or not''' ''' 6.815 can bypass acceptableProbFailure''' numInliers = 0 inliers = [False] * len(listOfCorrespondences) H_best = np.zeros((3, 3)) for n in xrange(Niter): correspondences = random.sample(listOfCorrespondences, 4) listOfPairs = A7PairsToA6Pairs(correspondences) H = a6.computeHomography(listOfPairs) currentInliers = [] for c in listOfCorrespondences: estimate = H.dot(A7PointToA6Point(c.pt1)) estimate[0], estimate[1] = (estimate[0] / estimate[2], estimate[1] / estimate[2]) actual = A7PointToA6Point(c.pt2) error = math.sqrt(sum((estimate - actual)**2)) currentInliers.append(bool(error < epsilon)) currentNumInliers = sum(bool(x) for x in currentInliers) if currentNumInliers > numInliers: numInliers = currentNumInliers inliers = currentInliers H_best = H.copy() if (1 - (float(numInliers) / len(listOfCorrespondences))** 4)**n < acceptableProbFailure: break return (H_best, inliers)
def RANSAC(listOfCorrespondences, Niter=1000, epsilon=4, acceptableProbFailure=1e-9): '''H_best: the best estimation of homorgraphy (3-by-3 matrix)''' '''inliers: A list of booleans that describe whether the element in listOfCorrespondences an inlier or not''' ''' 6.815 can bypass acceptableProbFailure''' cLen = len(listOfCorrespondences) maxInliers = 0 H_best = np.zeros((3,3)) for i in xrange(Niter): samples = rnd.sample( listOfCorrespondences, 4 ) H = a6.computeHomography( A7PairsToA6Pairs(samples) ) areInliers = map( mapH, [H]*cLen, listOfCorrespondences, [epsilon]*cLen ) numInliers = areInliers.count(True) if(maxInliers < numInliers): H_best = H maxInliers = numInliers x = float(maxInliers)/cLen probFailure = pow( (1-pow(x,4)), i+1 ) if (probFailure<acceptableProbFailure): break inliers = map( mapH, [H_best]*cLen, listOfCorrespondences, [epsilon]*cLen ) return (H_best, inliers)
def testComputeNHomographies(): pointList1 = [np.array([209, 218, 1]), np.array([425, 300, 1]), np.array([209, 337, 1]), np.array([396, 336, 1])] pointList2 = [np.array([232, 4, 1]), np.array([465, 62, 1]), np.array([247, 125, 1]), np.array([433, 102, 1])] listOfPairs = zip(pointList1, pointList2) h1 = a6.computeHomography(listOfPairs) h2 = a6.computeNHomographies([listOfPairs], 1) print h1, h2
def testComputeTransformedBBox(): im1=io.imread('stata/stata-1.png') im2=io.imread('stata/stata-2.png') pointList1=[np.array([209, 218, 1]), np.array([425, 300, 1]), np.array([209, 337, 1]), np.array([396, 336, 1])] pointList2=[np.array([232, 4, 1]), np.array([465, 62, 1]), np.array([247, 125, 1]), np.array([433, 102, 1])] listOfPairsS=zip(pointList1, pointList2) HS=a6.computeHomography(listOfPairsS) shape = np.shape(im2) print a6.computeTransformedBBox(shape, HS)
def testComputeAndApplyHomographyStata(): im1=io.imread('stata/stata-1.png') im2=io.imread('stata/stata-2.png') pointList1=[np.array([209, 218, 1]), np.array([425, 300, 1]), np.array([209, 337, 1]), np.array([396, 336, 1])] pointList2=[np.array([232, 4, 1]), np.array([465, 62, 1]), np.array([247, 125, 1]), np.array([433, 102, 1])] listOfPairsS=zip(pointList1, pointList2) HS=a6.computeHomography(listOfPairsS) #multiply by 0.2 to better show the transition out=im2*0.5 a6.applyHomography(im1, out, HS, True) io.imwrite(out, "stata_computeAndApplyHomography.png")
def testComputeAndApplyHomographyFun(): im1=io.imread('fun/room1.png') im2=io.imread('fun/room2.png') pointList1=[np.array([327, 258, 1], dtype=np.float64), np.array([75, 437, 1], dtype=np.float64), np.array([224, 364, 1], dtype=np.float64), np.array([423, 449, 1], dtype=np.float64)] pointList2=[np.array([294, 50, 1], dtype=np.float64), np.array([50, 227, 1], dtype=np.float64), np.array([190, 161, 1], dtype=np.float64), np.array([366, 240, 1], dtype=np.float64)] listOfPairsS=zip(pointList1, pointList2) HS=a6.computeHomography(listOfPairsS) #multiply by 0.2 to better show the transition out=im2*0.5 a6.applyHomography(im1, out, HS, True) io.imwrite(out, "fun.png")
def testComputeAndApplyHomographyPoster(): green = io.getImage("green.png") poster = io.getImage("poster.png") h, w = poster.shape[0] - 1, poster.shape[1] - 1 pointListPoster = [np.array([0, 0, 1]), np.array([0, w, 1]), np.array([h, w, 1]), np.array([h, 0, 1])] pointListT = [np.array([170, 95, 1]), np.array([171, 238, 1]), np.array([233, 235, 1]), np.array([239, 94, 1])] listOfPairs = zip(pointListPoster, pointListT) H = a6.computeHomography(listOfPairs) # print H a6.applyHomography(poster, green, H, True) io.imwrite(green, "HWDueAt9pm_computeHomography.png")
def makeStreetSign(): sign = io.imread("highway.png") people = io.imread("coverphoto.png") h, w = people.shape[0] - 1, people.shape[1] - 1 peoplecorners = [np.array([0, 0, 1]), np.array([0, w, 1]), np.array([h, w, 1]), np.array([h, 0, 1])] pointList1 = [ np.array([105, 94, 1], dtype=np.float64), np.array([110, 200, 1], dtype=np.float64), np.array([162, 200, 1], dtype=np.float64), np.array([159, 92, 1], dtype=np.float64), ] listOfPairs = zip(peoplecorners, pointList1) H = a6.computeHomography(listOfPairs) a6.applyHomography(people, sign, H, True) io.imwrite(sign, "Fun.png")
def testComputeAndApplyHomographyPrudential(): pru = io.getImage("pruSkyline.png") poster = io.getImage("andrewTag.png") h, w = poster.shape[0]-1, poster.shape[1]-1 pointListPoster=[np.array([0, 0, 1]), np.array([0, w, 1]), np.array([h, w, 1]), np.array([h, 0, 1])] #pointListPru=[np.array([78, 120, 1]), np.array([99, 160, 1]), np.array([150, 159, 1]), np.array([132, 118, 1])] pointListPru=[np.array([78, 120, 1], dtype=np.float64), np.array([99, 160, 1], dtype=np.float64), np.array([207, 156, 1], dtype=np.float64), np.array([194, 115, 1], dtype=np.float64)] listOfPairs=zip(pointListPoster, pointListPru) H = a6.computeHomography(listOfPairs) #print H a6.applyHomography(poster, pru, H, True) io.imwrite(pru, "Fun.png")