def _vote_ransac(self, queryset, dataset, mapping, results, dists): """ Applies RANSAC to database features and then votes with them. Note that normally we RANSAC *after* all the cells are combined, not on the cells individually. Experimentally this approach works poorly, but it might be useful as an example. """ if self.params['num_neighbors'] > 1: sorted_counts = self._vote_top_n(queryset, dataset, mapping, results, dists) else: sorted_counts = self._vote_matchonce(queryset, dataset, mapping, results, dists) # filters out outliers from counts until # filtered_votes(ith image) > votes(jth image) for all j != i # and returns top 10 filtered results filtered = {} bound = -1 num_filt = 0 for siftfile, matches in sorted_counts: if len(matches) < bound or num_filt > 10: INFO('stopped after filtering %d' % num_filt) break num_filt += 1 F, inliers = corr.find_corr(matches) bound = max(sum(inliers), bound) pts = np.ndarray(len(matches), np.object) pts[0:len(matches)] = matches if any(inliers): filtered[siftfile] = list(np.compress(inliers, pts)) rsorted_counts = sorted(filtered.iteritems(), key=lambda x: len(x[1]), reverse=True) if not rsorted_counts: INFO('W: ransac rejected everything, not filtering') return sorted_counts return rsorted_counts
def rerank_ransac(counts, C, Q): sorted_counts = sorted(counts.iteritems(), key=lambda x: len(x[1]), reverse=True) if C.amb_cutoff: assert C.amb_cutoff > 1 old = sorted_counts sorted_counts = amb_cutoff(C, Q, sorted_counts) print "amb cutoff: %d -> %d" % (len(old), len(sorted_counts)) if not sorted_counts: sorted_counts = old # tuples of (len, siftfile, matches) ordered [(1..), (2..), (3..)] reranked = [] num_filt = 0 for siftfile, matches in sorted_counts: # the bottom g items in 'reranked' are in their final order g = len(reranked) - bisect.bisect(reranked, (len(matches), None, None)) if g >= C.ranking_min_consistent or num_filt >= C.ranking_max_considered: if C.verbosity > 0: INFO('stopped after filtering %d' % num_filt) break num_filt += 1 # perform ransac F, inliers = corr.find_corr(matches) if any(inliers): m = np.compress(inliers, matches) bisect.insort(reranked, (len(m), siftfile, m)) if not reranked: INFO('W: no db matches passed ransac, not filtering') return condense2(sorted_counts), False else: reranked.reverse() return condense3(reranked), True
def compute_hom(C, Q, ranked_matches, comb_matches): match1 = any(check_img(C, Q, ranked_matches[0])) if not C.compute_hom: if match1: return if not C.log_failures: return if C.put_into_dirs: udir = os.path.join(C.resultsdir, Q.name) else: udir = C.resultsdir if not os.path.exists(udir): os.makedirs(udir) i = 0 data = {} for matchedimg, score, pairs in ranked_matches[:C.max_matches_to_analyze]: i += 1 if C.stop_on_homTrue and data.get('success'): break # we are done (found good homography) clat, clon = getlatlonfromdbimagename(C, matchedimg) matchimgpath = None # XXX this sucks, since we don't have a db image abstraction for ext in ['.jpg', '.JPG', '.png', '.PNG']: p = os.path.join(C.dbdump, '%s%s' % (matchedimg, ext)) if os.path.exists(p): matchimgpath = p assert matchimgpath match = any(check_img(C, Q, ranked_matches[i-1])) # matches = db_matches # rematch for precise fit db_matches = comb_matches[matchedimg + 'sift.txt'] matchsiftpath = os.path.join(C.dbdump, matchedimg + 'sift.txt') matches = corr.rematch(C, Q, matchsiftpath) # matches1 = matches # rp_matches = corr.rematch(C, Q, matchsiftpath) # concat db matches matches.extend(db_matches) # find homography rsc_matches, H, inliers = corr.find_corr(matches, hom=True, ransac_pass=True, data=data) rsc_inliers = np.compress(inliers, rsc_matches).tolist() u = corr.count_unique_matches(rsc_inliers) if C.drawtopcorr or (not match and C.log_failures): # draw picture matchoutpath = os.path.join(udir, Q.name + ';match' + str(i) + ';gt' + str(match) + ';hom' + str(data.get('success')) + ';uniq=' + str(u) + ';inliers=' + str(float(sum(inliers))/len(matches)) + ';' + matchedimg + '.jpg') # try: corr.draw_matches(C, Q, matches, rsc_matches, H, inliers, matchimgpath, matchoutpath, matchsiftpath, C.show_feature_pairs) # except IOError, e: # INFO(e) if C.dump_hom: if C.put_into_dirs: identifier = str(i) else: identifier = Q.name + ':' + str(i) H = np.matrix(np.asarray(H)) with open(os.path.join(udir, 'homography%s.txt' % identifier), 'w') as f: print >> f, H np.save(os.path.join(udir, 'inliers%s.npy' % identifier), rsc_inliers) ### POSIT ### if C.do_posit: posit.do_posit(C, Q, rsc_inliers, matchsiftpath, matchimgpath)