def show_nearest_descriptors(hs, qcx, qfx, fnum=None): if fnum is None: fnum = df2.next_fnum() # Inspect the nearest neighbors of a descriptor dx2_cx = hs.qdat._data_index.ax2_cx dx2_fx = hs.qdat._data_index.ax2_fx K = hs.qdat.cfg.nn_cfg.K Knorm = hs.qdat.cfg.nn_cfg.Knorm checks = hs.qdat.cfg.nn_cfg.checks flann = hs.qdat._data_index.flann qfx2_desc = hs.get_desc(qcx)[qfx:qfx + 1] try: (qfx2_dx, qfx2_dist) = flann.nn_index(qfx2_desc, K + Knorm, checks=checks) qfx2_cx = dx2_cx[qfx2_dx] qfx2_fx = dx2_fx[qfx2_dx] def get_extract_tuple(cx, fx, k=-1): rchip = hs.get_chip(cx) kp = hs.get_kpts(cx)[fx] sift = hs.get_desc(cx)[fx] if k == -1: info = '\nquery %s, fx=%r' % (hs.cidstr(cx), fx) type_ = 'query' elif k < K: type_ = 'match' info = '\nmatch %s, fx=%r k=%r, dist=%r' % (hs.cidstr(cx), fx, k, qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'norm' info = '\nnorm %s, fx=%r k=%r, dist=%r' % (hs.cidstr(cx), fx, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, cx, info, type_) extracted_list = [] extracted_list.append(get_extract_tuple(qcx, qfx, -1)) for k in xrange(K + Knorm): tup = get_extract_tuple(qfx2_cx[0, k], qfx2_fx[0, k], k) extracted_list.append(tup) #print('[viz] K + Knorm = %r' % (K + Knorm)) # Draw the _select_ith_match plot nRows, nCols = len(extracted_list), 3 # Draw selected feature matches prevsift = None df2.figure(fnum=fnum, docla=True, doclf=True) px = 0 # plot offset for (rchip, kp, sift, fx, cx, info, type_) in extracted_list: print('[viz] ' + info.replace('\n', '')) px = draw_feat_row(rchip, fx, kp, sift, fnum, nRows, nCols, px, prevsift=prevsift, cx=cx, info=info, type_=type_) prevsift = sift df2.adjust_subplots_safe(hspace=1) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def _click_chipres_click(event): print_('[inter] clicked chipres') if event is None: return button = event.button is_right_click = button == 3 if is_right_click: return (x, y, ax) = (event.xdata, event.ydata, event.inaxes) # Out of axes click if None in [x, y, ax]: print('... out of axis') _chipmatch_view() viz.draw() return hs_viewtype = ax.__dict__.get('_hs_viewtype', '') print_(' hs_viewtype=%r ' % hs_viewtype) key = '' if event.key is None else event.key print_('key=%r ' % key) ctrl_down = key.find('control') == 0 # Click in match axes if hs_viewtype == 'chipres' and ctrl_down: # Ctrl-Click print('.. control click') return _sv_view(cx) elif hs_viewtype == 'chipres': if len(fm) == 0: print('[inter] no feature matches to click') else: # Normal Click # Select nearest feature match to the click kpts1, kpts2 = hs.get_kpts([qcx, cx]) kpts1_m = kpts1[fm[:, 0]] kpts2_m = kpts2[fm[:, 1]] x2, y2, w2, h2 = xywh2_ptr[0] _mx1, _dist1 = nearest_point(x, y, kpts1_m) _mx2, _dist2 = nearest_point(x - x2, y - y2, kpts2_m) mx = _mx1 if _dist1 < _dist2 else _mx2 print('... clicked mx=%r' % mx) _select_ith_match(mx, qcx, cx) elif hs_viewtype in ['warped', 'unwarped']: hs_cx = ax.__dict__.get('_hs_cx', None) hs_fx = ax.__dict__.get('_hs_fx', None) if hs_cx is not None: interact_chip(hs, hs_cx, fx=hs_fx, fnum=df2.next_fnum()) else: print('...Unknown viewtype') viz.draw()
def query_last_feature(): viz.show_nearest_descriptors(hs, qcx, last_state.last_fx, df2.next_fnum()) fig3 = df2.gca() df2.connect_callback(fig3, 'button_press_event', _click_chipres_click) df2.update()