def baseline_vsone_ratio_matcher_(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, cfgdict={}): r""" Args: vecs1 (ndarray[uint8_t, ndim=2]): SIFT descriptors vecs2 (ndarray[uint8_t, ndim=2]): SIFT descriptors kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints Ignore: %pylab qt4 import plottool as pt pt.imshow(rchip1) pt.draw_kpts2(kpts1) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) """ #import vtool as vt sver_xy_thresh = cfgdict.get('sver_xy_thresh', .01) ratio_thresh = cfgdict.get('ratio_thresh', .625) #ratio_thresh = .99 # GET NEAREST NEIGHBORS fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=2) assigntup = matching.assign_unconstrained_matches(fx2_to_fx1, fx2_to_dist) fx2_match, fx1_match, fx1_norm, match_dist, norm_dist = assigntup fm_ORIG = np.vstack((fx1_match, fx2_match)).T fs_ORIG = 1 - np.divide(match_dist, norm_dist) # APPLY RATIO TEST fm_RAT, fs_RAT, fm_norm_RAT = matching.ratio_test(fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh) # SPATIAL VERIFICATION FILTER #with ut.EmbedOnException(): match_weights = np.ones(len(fm_RAT)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_RAT, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights) if svtup is not None: (homog_inliers, homog_errors, H_RAT) = svtup[0:3] else: H_RAT = np.eye(3) homog_inliers = [] fm_SV = fm_RAT[homog_inliers] fs_SV = fs_RAT[homog_inliers] fm_norm_SV = fm_norm_RAT[homog_inliers] base_tup = (fm_ORIG, fs_ORIG, fm_RAT, fs_RAT, fm_SV, fs_SV, H_RAT) base_meta = (fm_norm_RAT, fm_norm_SV) return base_tup, base_meta
def spatially_constrianed_matcher_(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, H_RAT, cfgdict={}): # import vtool as vt # match_xy_thresh = .1 # sver_xy_thresh = .01 # ratio_thresh2 = .8 # Observation, scores don't change above K=7 # on easy test case # search_K = 7 # 3 search_K = cfgdict.get("search_K", 7) ratio_thresh2 = cfgdict.get("ratio_thresh2", 0.8) sver_xy_thresh2 = cfgdict.get("sver_xy_thresh2", 0.01) normalizer_mode = cfgdict.get("normalizer_mode", "far") match_xy_thresh = cfgdict.get("match_xy_thresh", 0.1) # ASSIGN CANDIDATES # Get candidate nearest neighbors fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=search_K) # COMPUTE CONSTRAINTS # normalizer_mode = 'far' constrain_tup = spatially_constrain_matches( dlen_sqrd2, kpts1, kpts2, H_RAT, fx2_to_fx1, fx2_to_dist, match_xy_thresh, normalizer_mode=normalizer_mode ) (fm_SC, fm_norm_SC, match_dist, norm_dist) = constrain_tup fx2_match = fm_SC.T[1] fx1_match = fm_SC.T[1] fx1_norm = fm_norm_SC.T[1] fm_SCR, fs_SCR, fm_norm_SCR = matching.ratio_test( fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh2 ) fs_SC = 1 - np.divide(match_dist, norm_dist) # NOQA # fm_SCR, fs_SCR, fm_norm_SCR = ratio_test2(match_dist, norm_dist, fm_SC, # fm_norm_SC, ratio_thresh2) # Another round of verification match_weights = np.ones(len(fm_SCR)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_SCR, sver_xy_thresh2, dlen_sqrd2, match_weights=match_weights) if svtup is not None: (homog_inliers, homog_errors, H_SCR) = svtup[0:3] else: H_SCR = np.eye(3) homog_inliers = [] fm_SCRSV = fm_SCR[homog_inliers] fs_SCRSV = fs_SCR[homog_inliers] fm_norm_SVSCR = fm_norm_SCR[homog_inliers] nexttup = (fm_SC, fs_SC, fm_SCR, fs_SCR, fm_SCRSV, fs_SCRSV, H_SCR) next_meta = (fm_norm_SC, fm_norm_SCR, fm_norm_SVSCR) return nexttup, next_meta
def baseline_vsone_ratio_matcher_(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, cfgdict={}): r""" Args: vecs1 (ndarray[uint8_t, ndim=2]): SIFT descriptors vecs2 (ndarray[uint8_t, ndim=2]): SIFT descriptors kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints Ignore: %pylab qt4 import plottool as pt pt.imshow(rchip1) pt.draw_kpts2(kpts1) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) """ # import vtool as vt sver_xy_thresh = cfgdict.get("sver_xy_thresh", 0.01) ratio_thresh = cfgdict.get("ratio_thresh", 0.625) # ratio_thresh = .99 # GET NEAREST NEIGHBORS fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=2) assigntup = matching.assign_unconstrained_matches(fx2_to_fx1, fx2_to_dist) fx2_match, fx1_match, fx1_norm, match_dist, norm_dist = assigntup fm_ORIG = np.vstack((fx1_match, fx2_match)).T fs_ORIG = 1 - np.divide(match_dist, norm_dist) # APPLY RATIO TEST fm_RAT, fs_RAT, fm_norm_RAT = matching.ratio_test( fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh ) # SPATIAL VERIFICATION FILTER # with ut.EmbedOnException(): match_weights = np.ones(len(fm_RAT)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_RAT, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights) if svtup is not None: (homog_inliers, homog_errors, H_RAT) = svtup[0:3] else: H_RAT = np.eye(3) homog_inliers = [] fm_SV = fm_RAT[homog_inliers] fs_SV = fs_RAT[homog_inliers] fm_norm_SV = fm_norm_RAT[homog_inliers] base_tup = (fm_ORIG, fs_ORIG, fm_RAT, fs_RAT, fm_SV, fs_SV, H_RAT) base_meta = (fm_norm_RAT, fm_norm_SV) return base_tup, base_meta
def spatially_constrianed_matcher_(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, H_RAT, cfgdict={}): #import vtool as vt #match_xy_thresh = .1 #sver_xy_thresh = .01 #ratio_thresh2 = .8 # Observation, scores don't change above K=7 # on easy test case #search_K = 7 # 3 search_K = cfgdict.get('search_K', 7) ratio_thresh2 = cfgdict.get('ratio_thresh2', .8) sver_xy_thresh2 = cfgdict.get('sver_xy_thresh2', .01) normalizer_mode = cfgdict.get('normalizer_mode', 'far') match_xy_thresh = cfgdict.get('match_xy_thresh', .1) # ASSIGN CANDIDATES # Get candidate nearest neighbors fx2_to_fx1, fx2_to_dist = assign_nearest_neighbors(vecs1, vecs2, K=search_K) # COMPUTE CONSTRAINTS #normalizer_mode = 'far' constrain_tup = spatially_constrain_matches( dlen_sqrd2, kpts1, kpts2, H_RAT, fx2_to_fx1, fx2_to_dist, match_xy_thresh, normalizer_mode=normalizer_mode) (fm_SC, fm_norm_SC, match_dist, norm_dist) = constrain_tup fx2_match = fm_SC.T[1] fx1_match = fm_SC.T[1] fx1_norm = fm_norm_SC.T[1] fm_SCR, fs_SCR, fm_norm_SCR = matching.ratio_test(fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh2) fs_SC = 1 - np.divide(match_dist, norm_dist) # NOQA #fm_SCR, fs_SCR, fm_norm_SCR = ratio_test2(match_dist, norm_dist, fm_SC, # fm_norm_SC, ratio_thresh2) # Another round of verification match_weights = np.ones(len(fm_SCR)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_SCR, sver_xy_thresh2, dlen_sqrd2, match_weights=match_weights) if svtup is not None: (homog_inliers, homog_errors, H_SCR) = svtup[0:3] else: H_SCR = np.eye(3) homog_inliers = [] fm_SCRSV = fm_SCR[homog_inliers] fs_SCRSV = fs_SCR[homog_inliers] fm_norm_SVSCR = fm_norm_SCR[homog_inliers] nexttup = (fm_SC, fs_SC, fm_SCR, fs_SCR, fm_SCRSV, fs_SCRSV, H_SCR) next_meta = (fm_norm_SC, fm_norm_SCR, fm_norm_SVSCR) return nexttup, next_meta
def vsone_feature_matching(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, cfgdict={}, flann1=None, flann2=None, verbose=None): r""" Actual logic for matching Args: vecs1 (ndarray[uint8_t, ndim=2]): SIFT descriptors vecs2 (ndarray[uint8_t, ndim=2]): SIFT descriptors kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints Ignore: >>> from vtool.matching import * # NOQA %pylab qt4 import plottool as pt pt.imshow(rchip1) pt.draw_kpts2(kpts1) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) """ import vtool as vt import pyflann from vtool import spatial_verification as sver #import vtool as vt sver_xy_thresh = cfgdict.get('sver_xy_thresh', .01) ratio_thresh = cfgdict.get('ratio_thresh', .625) refine_method = cfgdict.get('refine_method', 'homog') symmetric = cfgdict.get('symmetric', False) K = cfgdict.get('K', 1) Knorm = cfgdict.get('Knorm', 1) #ratio_thresh = .99 # GET NEAREST NEIGHBORS checks = 800 #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 #pseudo_max_dist_sqrd = 2 * (512 ** 2) if verbose is None: verbose = True flann_params = {'algorithm': 'kdtree', 'trees': 8} if flann1 is None: flann1 = vt.flann_cache(vecs1, flann_params=flann_params, verbose=verbose) #print('symmetric = %r' % (symmetric,)) if symmetric: if flann2 is None: flann2 = vt.flann_cache(vecs2, flann_params=flann_params, verbose=verbose) try: try: num_neighbors = K + Knorm fx2_to_fx1, fx2_to_dist = normalized_nearest_neighbors(flann1, vecs2, num_neighbors, checks) #fx2_to_fx1, _fx2_to_dist = flann1.nn_index(vecs2, num_neighbors=K, checks=checks) if symmetric: fx1_to_fx2, fx1_to_dist = normalized_nearest_neighbors(flann2, vecs1, K, checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape,)) print('vecs2.shape = %r' % (vecs2.shape,)) print('vecs1.dtype = %r' % (vecs1.dtype,)) print('vecs2.dtype = %r' % (vecs2.dtype,)) raise if symmetric: is_symmetric = flag_symmetric_matches(fx2_to_fx1, fx1_to_fx2) fx2_to_fx1 = fx2_to_fx1.compress(is_symmetric, axis=0) fx2_to_dist = fx2_to_dist.compress(is_symmetric, axis=0) assigntup = assign_unconstrained_matches(fx2_to_fx1, fx2_to_dist) fx2_match, fx1_match, fx1_norm, match_dist, norm_dist = assigntup fm_ORIG = np.vstack((fx1_match, fx2_match)).T fs_ORIG = 1 - np.divide(match_dist, norm_dist) # APPLY RATIO TEST fm_RAT, fs_RAT, fm_norm_RAT = ratio_test(fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh) # SPATIAL VERIFICATION FILTER #with ut.EmbedOnException(): match_weights = np.ones(len(fm_RAT)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_RAT, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights, refine_method=refine_method) if svtup is not None: (homog_inliers, homog_errors, H_RAT) = svtup[0:3] else: H_RAT = np.eye(3) homog_inliers = [] fm_RAT_SV = fm_RAT.take(homog_inliers, axis=0) fs_RAT_SV = fs_RAT.take(homog_inliers, axis=0) fm_norm_RAT_SV = fm_norm_RAT[homog_inliers] top_percent = .5 top_idx = ut.take_percentile(fx2_to_dist.T[0].argsort(), top_percent) fm_TOP = fm_ORIG.take(top_idx, axis=0) fs_TOP = fx2_to_dist.T[0].take(top_idx) #match_weights = np.ones(len(fm_TOP)) #match_weights = (np.exp(fs_TOP) / np.sqrt(np.pi * 2)) match_weights = 1 - fs_TOP #match_weights = np.ones(len(fm_TOP)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_TOP, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights, refine_method=refine_method) if svtup is not None: (homog_inliers, homog_errors, H_TOP) = svtup[0:3] np.sqrt(homog_errors[0] / dlen_sqrd2) else: H_TOP = np.eye(3) homog_inliers = [] fm_TOP_SV = fm_TOP.take(homog_inliers, axis=0) fs_TOP_SV = fs_TOP.take(homog_inliers, axis=0) matches = { 'ORIG' : MatchTup2(fm_ORIG, fs_ORIG), 'RAT' : MatchTup3(fm_RAT, fs_RAT, fm_norm_RAT), 'RAT+SV' : MatchTup3(fm_RAT_SV, fs_RAT_SV, fm_norm_RAT_SV), 'TOP' : MatchTup2(fm_TOP, fs_TOP), 'TOP+SV' : MatchTup2(fm_TOP_SV, fs_TOP_SV), } output_metdata = { 'H_RAT': H_RAT, 'H_TOP': H_TOP, } except MatchingError: fm_ERR = np.empty((0, 2), dtype=np.int32) fs_ERR = np.empty((0, 1), dtype=np.float32) H_ERR = np.eye(3) matches = { 'ORIG' : MatchTup2(fm_ERR, fs_ERR), 'RAT' : MatchTup3(fm_ERR, fs_ERR, fm_ERR), 'RAT+SV' : MatchTup3(fm_ERR, fs_ERR, fm_ERR), 'TOP' : MatchTup2(fm_ERR, fs_ERR), 'TOP+SV' : MatchTup2(fm_ERR, fs_ERR), } output_metdata = { 'H_RAT': H_ERR, 'H_TOP': H_ERR, } return matches, output_metdata
def vsone_feature_matching(kpts1, vecs1, kpts2, vecs2, dlen_sqrd2, cfgdict={}, flann1=None, flann2=None, verbose=None): r""" Actual logic for matching Args: vecs1 (ndarray[uint8_t, ndim=2]): SIFT descriptors vecs2 (ndarray[uint8_t, ndim=2]): SIFT descriptors kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints Ignore: >>> from vtool.matching import * # NOQA %pylab qt4 import plottool as pt pt.imshow(rchip1) pt.draw_kpts2(kpts1) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm=fm, fs=fs) """ import vtool as vt import pyflann from vtool import spatial_verification as sver #import vtool as vt sver_xy_thresh = cfgdict.get('sver_xy_thresh', .01) ratio_thresh = cfgdict.get('ratio_thresh', .625) refine_method = cfgdict.get('refine_method', 'homog') symmetric = cfgdict.get('symmetric', False) K = cfgdict.get('K', 1) Knorm = cfgdict.get('Knorm', 1) #ratio_thresh = .99 # GET NEAREST NEIGHBORS checks = 800 #pseudo_max_dist_sqrd = (np.sqrt(2) * 512) ** 2 #pseudo_max_dist_sqrd = 2 * (512 ** 2) if verbose is None: verbose = True flann_params = {'algorithm': 'kdtree', 'trees': 8} if flann1 is None: flann1 = vt.flann_cache(vecs1, flann_params=flann_params, verbose=verbose) #print('symmetric = %r' % (symmetric,)) if symmetric: if flann2 is None: flann2 = vt.flann_cache(vecs2, flann_params=flann_params, verbose=verbose) try: try: num_neighbors = K + Knorm fx2_to_fx1, fx2_to_dist = normalized_nearest_neighbors( flann1, vecs2, num_neighbors, checks) #fx2_to_fx1, _fx2_to_dist = flann1.nn_index(vecs2, num_neighbors=K, checks=checks) if symmetric: fx1_to_fx2, fx1_to_dist = normalized_nearest_neighbors( flann2, vecs1, K, checks) except pyflann.FLANNException: print('vecs1.shape = %r' % (vecs1.shape, )) print('vecs2.shape = %r' % (vecs2.shape, )) print('vecs1.dtype = %r' % (vecs1.dtype, )) print('vecs2.dtype = %r' % (vecs2.dtype, )) raise if symmetric: is_symmetric = flag_symmetric_matches(fx2_to_fx1, fx1_to_fx2) fx2_to_fx1 = fx2_to_fx1.compress(is_symmetric, axis=0) fx2_to_dist = fx2_to_dist.compress(is_symmetric, axis=0) assigntup = assign_unconstrained_matches(fx2_to_fx1, fx2_to_dist) fx2_match, fx1_match, fx1_norm, match_dist, norm_dist = assigntup fm_ORIG = np.vstack((fx1_match, fx2_match)).T fs_ORIG = 1 - np.divide(match_dist, norm_dist) # APPLY RATIO TEST fm_RAT, fs_RAT, fm_norm_RAT = ratio_test(fx2_match, fx1_match, fx1_norm, match_dist, norm_dist, ratio_thresh) # SPATIAL VERIFICATION FILTER #with ut.EmbedOnException(): match_weights = np.ones(len(fm_RAT)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_RAT, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights, refine_method=refine_method) if svtup is not None: (homog_inliers, homog_errors, H_RAT) = svtup[0:3] else: H_RAT = np.eye(3) homog_inliers = [] fm_RAT_SV = fm_RAT.take(homog_inliers, axis=0) fs_RAT_SV = fs_RAT.take(homog_inliers, axis=0) fm_norm_RAT_SV = fm_norm_RAT[homog_inliers] top_percent = .5 top_idx = ut.take_percentile(fx2_to_dist.T[0].argsort(), top_percent) fm_TOP = fm_ORIG.take(top_idx, axis=0) fs_TOP = fx2_to_dist.T[0].take(top_idx) #match_weights = np.ones(len(fm_TOP)) #match_weights = (np.exp(fs_TOP) / np.sqrt(np.pi * 2)) match_weights = 1 - fs_TOP #match_weights = np.ones(len(fm_TOP)) svtup = sver.spatially_verify_kpts(kpts1, kpts2, fm_TOP, sver_xy_thresh, dlen_sqrd2, match_weights=match_weights, refine_method=refine_method) if svtup is not None: (homog_inliers, homog_errors, H_TOP) = svtup[0:3] np.sqrt(homog_errors[0] / dlen_sqrd2) else: H_TOP = np.eye(3) homog_inliers = [] fm_TOP_SV = fm_TOP.take(homog_inliers, axis=0) fs_TOP_SV = fs_TOP.take(homog_inliers, axis=0) matches = { 'ORIG': MatchTup2(fm_ORIG, fs_ORIG), 'RAT': MatchTup3(fm_RAT, fs_RAT, fm_norm_RAT), 'RAT+SV': MatchTup3(fm_RAT_SV, fs_RAT_SV, fm_norm_RAT_SV), 'TOP': MatchTup2(fm_TOP, fs_TOP), 'TOP+SV': MatchTup2(fm_TOP_SV, fs_TOP_SV), } output_metdata = { 'H_RAT': H_RAT, 'H_TOP': H_TOP, } except MatchingError: fm_ERR = np.empty((0, 2), dtype=np.int32) fs_ERR = np.empty((0, 1), dtype=np.float32) H_ERR = np.eye(3) matches = { 'ORIG': MatchTup2(fm_ERR, fs_ERR), 'RAT': MatchTup3(fm_ERR, fs_ERR, fm_ERR), 'RAT+SV': MatchTup3(fm_ERR, fs_ERR, fm_ERR), 'TOP': MatchTup2(fm_ERR, fs_ERR), 'TOP+SV': MatchTup2(fm_ERR, fs_ERR), } output_metdata = { 'H_RAT': H_ERR, 'H_TOP': H_ERR, } return matches, output_metdata