def bof_image_retrieval(self): # load vocabulary and query feature src = self.image_searcher() q_descr, fp = self.load_query_feature() # RANSAC model for homography fitting model = homography.RansacModel() rank = {} # query match_scores = [ w[0] for w in src.query(self.imlist[q_ind])[:nbr_results] ] res_reg = [w[1] for w in src.query(self.imlist[q_ind])[:nbr_results]] print('top matches:', res_reg) self.plot_results(res_reg[:6], match_scores[:6]) if self.bof_rearrange: # load image features for result for ndx in res_reg[1:]: locs, descr = sift.read_features_from_file(self.featlist[ndx]) # get matches matches = sift.match(q_descr, descr) ind = matches.nonzero()[0] ind2 = matches[ind] locs = np.array(locs) tp = homography.make_homog(locs[:, :2].T) # compute homography, count inliers. try: H, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_theshold=4) except: inliers = [] # store inlier count rank[ndx] = len(inliers) # sort dictionary to get the most inliers first sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res_reg[0]] + [s[0] for s in sorted_rank] # print('top matches (homography):', res_geom) # show results self.plot_results(res_geom[:6], match_scores[:6])
def H_homog_from_PCVSAC(kpts1_m, kpts2_m, xy_thresh_sqrd): from PCV.geometry import homography 'Python Computer Visions Random Sample Consensus' # Get xy points xy1_m = kpts1_m[0:2,:] xy2_m = kpts2_m[0:2,:] # Homogonize points fp = np.vstack([xy1_m, np.ones((1,xy1_m.shape[1]))]) tp = np.vstack([xy2_m, np.ones((1,xy2_m.shape[1]))]) # Get match threshold 10% of image diagonal # Get RANSAC inliers model = homography.RansacModel() try: H, pcv_inliers = homography.H_from_ransac(fp, tp, model, 500, np.sqrt(xy_thresh_sqrd)) except ValueError as ex: printWARN('Warning 221 from H_homog_from_PCVSAC'+repr(ex)) return np.eye(3), [] # Convert to the format I'm expecting inliers = np.zeros(kpts1_m.shape[1], dtype=bool) inliers[pcv_inliers] = True return H, inliers
# function to convert the matches to hom. points def convert_points(j): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j + 1][ndx, :2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2, :2].T) # switch x and y - TODO this should move elsewhere fp = vstack([fp[1], fp[0], fp[2]]) tp = vstack([tp[1], tp[0], tp[2]]) return fp, tp # estimate the homographies model = homography.RansacModel() fp, tp = convert_points(1) H_12 = homography.H_from_ransac(fp, tp, model)[0] # im 1 to 2 fp, tp = convert_points(0) H_01 = homography.H_from_ransac(fp, tp, model)[0] # im 0 to 1 tp, fp = convert_points(2) # NB: reverse order H_32 = homography.H_from_ransac(fp, tp, model)[0] # im 3 to 2 tp, fp = convert_points(3) # NB: reverse order H_43 = homography.H_from_ransac(fp, tp, model)[0] # im 4 to 3 # warp the images delta = 2000 # for padding and translation