def TEST_figure2(imgBGR, kpts, desc, sel, fnum=2):
    #df2.imshow(wpatch, fnum=2)
    sift = desc[sel]
    viz_kwargs = dict(ell=True,
                      eig=False,
                      rect=True,
                      ori_color=df2.DEEP_PINK,
                      ell_alpha=1,
                      fnum=fnum,
                      pnum=(2, 1, 1))
    show_keypoints(imgBGR,
                   kpts,
                   sifts=None,
                   sel_fx=sel,
                   ori=False,
                   **viz_kwargs)
    _annotate_kpts(kpts, sel, ori=True, **viz_kwargs)
    draw_feat_row(imgBGR,
                  sel,
                  kpts[sel],
                  sift,
                  fnum=fnum,
                  nRows=2,
                  nCols=3,
                  px=3)
def TEST_figure2(imgBGR, kpts, desc, sel, fnum=2):
    #df2.imshow(wpatch, fnum=2)
    sift = desc[sel]
    viz_kwargs = dict(ell=True, eig=False,
                      rect=True, ori_color=df2.DEEP_PINK, ell_alpha=1, fnum=fnum, pnum=(2, 1, 1))
    show_keypoints(imgBGR, kpts, sifts=None, sel_fx=sel, ori=False, **viz_kwargs)
    _annotate_kpts(kpts, sel, ori=True, **viz_kwargs)
    draw_feat_row(imgBGR, sel, kpts[sel], sift, fnum=fnum, nRows=2, nCols=3, px=3)
Example #3
0
 def _select_ith_kpt(self, fx):
     print('[interact] viewing ith=%r keypoint' % fx)
     # Get the fx-th keypiont
     kp, sift = self.kpts[fx], self.vecs[fx]
     # Draw the image with keypoint fx highlighted
     self.plot(self.fnum, (2, 1, 1), sel_fx=fx)
     # Draw the selected feature
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(self.chip, fx, kp, sift, self.fnum, nRows, nCols, px, None)
Example #4
0
 def _select_ith_kpt(fx):
     print('[interact] viewing ith=%r keypoint' % fx)
     # Get the fx-th keypiont
     kp, sift = kpts[fx], vecs[fx]
     # Draw the image with keypoint fx highlighted
     _viz_keypoints(fnum, (2, 1, 1), sel_fx=fx, **kwargs)  # MAYBE: remove kwargs
     # Draw the selected feature
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(chip, fx, kp, sift, fnum, nRows, nCols, px, None)
 def _select_ith_kpt(self, fx):
     print('[interact] viewing ith=%r keypoint' % fx)
     # Get the fx-th keypiont
     kp, sift = self.kpts[fx], self.vecs[fx]
     # Draw the image with keypoint fx highlighted
     self.plot(self.fnum, (2, 1, 1), sel_fx=fx)
     # Draw the selected feature
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(self.chip, fx, kp, sift, self.fnum, nRows, nCols, px,
                   None)
 def _select_ith_kpt(fx):
     print('[interact] viewing ith=%r keypoint' % fx)
     # Get the fx-th keypiont
     kp, sift = kpts[fx], vecs[fx]
     # Draw the image with keypoint fx highlighted
     _viz_keypoints(fnum, (2, 1, 1), sel_fx=fx,
                    **kwargs)  # MAYBE: remove kwargs
     # Draw the selected feature
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(chip, fx, kp, sift, fnum, nRows, nCols, px, None)
Example #7
0
 def _select_fxth_kpt(fx):
     # Get the fx-th keypiont
     chip = ibs.get_annot_chips(aid, config2_=config2_)
     kp = ibs.get_annot_kpts(aid, config2_=config2_)[fx]
     sift = ibs.get_annot_vecs(aid, config2_=config2_)[fx]
     # Draw chip + keypoints + highlighted plots
     _chip_view(pnum=(2, 1, 1), sel_fx=fx)
     #ishow_chip(ibs, aid, fnum=None, fx=fx, config2_=config2_, **kwargs)
     # Draw the selected feature plots
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(chip, fx, kp, sift, fnum, nRows, nCols, px, None)
Example #8
0
 def _select_fxth_kpt(fx):
     # Get the fx-th keypiont
     chip = ibs.get_annot_chips(aid, config2_=config2_)
     kp = ibs.get_annot_kpts(aid, config2_=config2_)[fx]
     sift = ibs.get_annot_vecs(aid, config2_=config2_)[fx]
     # Draw chip + keypoints + highlighted plots
     _chip_view(pnum=(2, 1, 1), sel_fx=fx)
     #ishow_chip(ibs, aid, fnum=None, fx=fx, config2_=config2_, **kwargs)
     # Draw the selected feature plots
     nRows, nCols, px = (2, 3, 3)
     draw_feat_row(chip, fx, kp, sift, fnum, nRows, nCols, px, None)
Example #9
0
    def _select_ith_match(mx, qaid, aid):
        #----------------------
        # Get info for the _select_ith_match plot
        annote_ptr[0] = 1
        # Get the mx-th feature match
        aid1, aid2 = qaid, aid
        fx1, fx2 = fm[mx]
        fscore2  = qres.aid2_fs[aid2][mx]
        fk2      = qres.aid2_fk[aid2][mx]
        kpts1, kpts2 = ibs.get_annot_kpts([aid1, aid2])
        desc1, desc2 = ibs.get_annot_desc([aid1, aid2])
        kp1, kp2     = kpts1[fx1], kpts2[fx2]
        sift1, sift2 = desc1[fx1], desc2[fx2]
        info1 = '\nquery'
        info2 = '\nk=%r fscore=%r' % (fk2, fscore2)
        last_state.last_fx = fx1

        # Extracted keypoints to draw
        extracted_list = [(rchip1, kp1, sift1, fx1, aid1, info1),
                          (rchip2, kp2, sift2, fx2, aid2, info2)]
        # Normalizng Keypoint
        if hasattr(qres, 'filt2_meta') and 'lnbnn' in qres.filt2_meta:
            qfx2_norm = qres.filt2_meta['lnbnn']
            # Normalizing chip and feature
            (aid3, fx3, normk) = qfx2_norm[fx1]
            rchip3 = ibs.get_annot_chips(aid3)
            kp3 = ibs.get_annot_kpts(aid3)[fx3]
            sift3 = ibs.get_annot_desc(aid3)[fx3]
            info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk)
            extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3))
        else:
            print('WARNING: meta doesnt exist')

        #----------------------
        # Draw the _select_ith_match plot
        nRows, nCols = len(extracted_list) + same_fig, 3
        # Draw matching chips and features
        sel_fm = np.array([(fx1, fx2)])
        pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1)
        _chipmatch_view(pnum1, vert=False, ell_alpha=.4, ell_linewidth=1.8,
                        colors=df2.BLUE, sel_fm=sel_fm, **kwargs)
        # Draw selected feature matches
        px = nCols * same_fig  # plot offset
        prevsift = None
        if not same_fig:
            fnum2 = fnum + len(viz.FNUMS)
            fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True)
        else:
            fnum2 = fnum
        for (rchip, kp, sift, fx, aid, info) in extracted_list:
            px = draw_feat_row(rchip, fx, kp, sift, fnum2, nRows, nCols, px,
                               prevsift=prevsift, aid=aid, info=info)
            prevsift = sift
        if not same_fig:
            ih.connect_callback(fig2, 'button_press_event', _click_matches_click)
            df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
Example #10
0
    def select_ith_match(self, mx):
        """
        Selects the ith match and visualizes and prints information concerning
        features weights, keypoint details, and sift descriptions
        """
        import plottool as pt
        from plottool import viz_featrow
        from plottool import interact_helpers as ih

        # <CLOSURE VARS>
        fnum = self.fnum
        same_fig = self.same_fig
        rchip1 = self.rchip1
        rchip2 = self.rchip2
        # </CLOSURE VARS>
        self.mx = mx
        print("+--- SELECT --- ")
        print("... selecting mx-th=%r feature match" % mx)
        fsv = self.fsv
        fs = self.fs
        print("score stats:")
        print(ut.get_stats_str(fsv, axis=0, newlines=True))
        print("fsv[mx] = %r" % (fsv[mx],))
        print("fs[mx] = %r" % (fs[mx],))
        # ----------------------
        # Get info for the select_ith_match plot
        self.mode = 1
        # Get the mx-th feature match
        fx1, fx2 = self.fm[mx]

        # Older info
        fscore2 = self.fs[mx]
        fk2 = None
        kp1, kp2 = self.kpts1[fx1], self.kpts2[fx2]
        vecs1, vecs2 = self.vecs1[fx1], self.vecs2[fx2]
        info1 = "\nquery"
        info2 = "\nk=%r fscore=%r" % (fk2, fscore2)
        # self.last_fx = fx1
        self.last_fx = fx1

        # Extracted keypoints to draw
        extracted_list = [(rchip1, kp1, vecs1, fx1, "aid1", info1), (rchip2, kp2, vecs2, fx2, "aid2", info2)]
        # Normalizng Keypoint
        # if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta:
        #    qfx2_norm = cm.filt2_meta['lnbnn']
        #    # Normalizing chip and feature
        #    (aid3, fx3, normk) = qfx2_norm[fx1]
        #    rchip3 = ibs.get_annot_chips(aid3)
        #    kp3 = ibs.get_annot_kpts(aid3)[fx3]
        #    sift3 = ibs.get_annot_vecs(aid3)[fx3]
        #    info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk)
        #    extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3))
        # else:
        #    pass
        #    #print('WARNING: meta doesnt exist')

        # ----------------------
        # Draw the select_ith_match plot
        nRows, nCols = len(extracted_list) + same_fig, 3
        # Draw matching chips and features
        sel_fm = np.array([(fx1, fx2)])
        pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1)
        vert = self.vert if self.vert is not None else False
        self.chipmatch_view(pnum=pnum1, ell_alpha=0.4, ell_linewidth=1.8, colors=pt.BLUE, sel_fm=sel_fm, vert=vert)
        # Draw selected feature matches
        px = nCols * same_fig  # plot offset
        prevsift = None
        if not same_fig:
            # fnum2 = fnum + len(viz.FNUMS)
            fnum2 = self.fnum2
            fig2 = pt.figure(fnum=fnum2, docla=True, doclf=True)
        else:
            fnum2 = fnum

        for (rchip, kp, sift, fx, aid, info) in extracted_list:
            px = viz_featrow.draw_feat_row(
                rchip, fx, kp, sift, fnum2, nRows, nCols, px, prevsift=prevsift, aid=aid, info=info
            )
            prevsift = sift
        if not same_fig:
            ih.connect_callback(fig2, "button_press_event", self.on_click)
Example #11
0
    def select_ith_match(self, mx):
        """
        Selects the ith match and visualizes and prints information concerning
        features weights, keypoint details, and sift descriptions

        Args:
            mx (int) - the ith match to visualize
            qaid (int) - query annotation id
            aid (int) - database annotation id

        CommandLine:
            python -m ibeis.viz.interact.interact_matches --test-select_ith_match --show

        Example:
            >>> # DISABLE_DOCTEST
            >>> from ibeis.viz.interact.interact_matches import *  # NOQA
            >>> self = testdata_match_interact(mx=1)
            >>> pt.show_if_requested()
        """
        ibs = self.ibs
        qaid = self.qaid
        aid = self.daid
        fnum = self.fnum
        figtitle = self.figtitle
        rchip1 = self.rchip1
        rchip2 = self.rchip2
        aid = self.daid
        same_fig = self.same_fig
        self.mx = mx
        print('+--- SELECT --- ')
        print('qaid=%r, daid=%r' % (qaid, aid))
        print('... selecting mx-th=%r feature match' % mx)
        if False:
            print('score stats:')
            print(ut.get_stats_str(self.fsv, axis=0, newlines=True))
            print('fsv[mx] = %r' % (self.fsv[mx], ))
            print('fs[mx] = %r' % (self.fs[mx], ))
        """
        # test feature weights of actual chips
        fx1, fx2 = fm[mx]
        daid = aid
        ibs.get_annot_fgweights([daid])[0][fx2]
        ibs.get_annot_fgweights([qaid])[0][fx1]
        """
        #----------------------
        # Get info for the select_ith_match plot
        self.mode = 1
        # Get the mx-th feature match
        fx1, fx2 = self.fm[mx]
        fscore2 = self.fs[mx]
        fk2 = self.fk[mx]
        kpts1 = ibs.get_annot_kpts([self.qaid],
                                   config2_=self.query_config2_)[0]
        kpts2 = ibs.get_annot_kpts([self.daid], config2_=self.data_config2_)[0]
        desc1 = ibs.get_annot_vecs([self.qaid],
                                   config2_=self.query_config2_)[0]
        desc2 = ibs.get_annot_vecs([self.daid], config2_=self.data_config2_)[0]
        kp1, kp2 = kpts1[fx1], kpts2[fx2]
        sift1, sift2 = desc1[fx1], desc2[fx2]
        info1 = '\nquery'
        info2 = '\nk=%r fscore=%r' % (fk2, fscore2)
        #last_state.last_fx = fx1
        self.last_fx = fx1
        # Extracted keypoints to draw
        extracted_list = [(rchip1, kp1, sift1, fx1, self.qaid, info1),
                          (rchip2, kp2, sift2, fx2, self.daid, info2)]
        # Normalizng Keypoint
        #if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta:
        #    qfx2_norm = cm.filt2_meta['lnbnn']
        #    # Normalizing chip and feature
        #    (aid3, fx3, normk) = qfx2_norm[fx1]
        #    rchip3 = ibs.get_annot_chips(aid3)
        #    kp3 = ibs.get_annot_kpts(aid3)[fx3]
        #    sift3 = ibs.get_annot_vecs(aid3)[fx3]
        #    info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk)
        #    extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3))
        #else:
        #    pass
        # print('WARNING: meta doesnt exist')

        #----------------------
        # Draw the select_ith_match plot
        nRows, nCols = len(extracted_list) + same_fig, 3
        # Draw matching chips and features
        sel_fm = np.array([(fx1, fx2)])
        pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1)
        vert = self.vert if self.vert is not None else False
        self.chipmatch_view(pnum1,
                            ell_alpha=.4,
                            ell_linewidth=1.8,
                            colors=df2.BLUE,
                            sel_fm=sel_fm,
                            vert=vert)
        # Draw selected feature matches
        px = nCols * same_fig  # plot offset
        prevsift = None
        if not same_fig:
            #fnum2 = fnum + len(viz.FNUMS)
            fnum2 = self.fnum2
            fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True)
        else:
            fnum2 = fnum
        for (rchip, kp, sift, fx, aid, info) in extracted_list:
            px = viz_featrow.draw_feat_row(rchip,
                                           fx,
                                           kp,
                                           sift,
                                           fnum2,
                                           nRows,
                                           nCols,
                                           px,
                                           prevsift=prevsift,
                                           aid=aid,
                                           info=info)
            prevsift = sift
        if not same_fig:
            ih.connect_callback(fig2, 'button_press_event', self.on_click)
            df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5,
                             qreq_=None, **kwargs):
    r"""
    Args:
        ibs (ibeis.IBEISController): image analysis api
        qaid (int):  query annotation id
        qfx (int): query feature index
        fnum (int):  figure number
        stride (int):
        consecutive_distance_compare (bool):

    CommandLine:
        # Find a good match to inspect
        python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3

        # Now inspect it
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256

    SeeAlso:
        plottool.viz_featrow
        ~/code/plottool/plottool/viz_featrow.py

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_nearest_descriptors import *  # NOQA
        >>> import ibeis
        >>> # build test data
        >>> qreq_ = ibeis.testdata_qreq_()
        >>> ibs = ibeis.opendb('PZ_MTEST')
        >>> qaid = qreq_.qaids[0]
        >>> qfx = ut.get_argval('--qfx', type_=int, default=879)
        >>> fnum = None
        >>> stride = 5
        >>> # execute function
        >>> skip = False
        >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride,
        >>>                                   draw_chip=True,
        >>>                                   draw_warped=True,
        >>>                                   draw_unwarped=False,
        >>>                                   draw_desc=False, qreq_=qreq_)
        >>> # verify results
        >>> print(result)
        >>> pt.show_if_requested()
    """
    consecutive_distance_compare = True
    draw_chip     = kwargs.get('draw_chip', False)
    draw_desc     = kwargs.get('draw_desc', True)
    draw_warped   = kwargs.get('draw_warped', True)
    draw_unwarped = kwargs.get('draw_unwarped', True)
    #skip = kwargs.get('skip', True)
    # Plots the nearest neighbors of a given feature (qaid, qfx)
    if fnum is None:
        fnum = df2.next_fnum()
    try:
        # Flann NN query
        (qfx2_daid, qfx2_dfx, qfx2_dist, K, Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_)

        # Adds metadata to a feature match
        def get_extract_tuple(aid, fx, k=-1):
            rchip = ibs.get_annot_chips(aid)
            kp    = ibs.get_annot_kpts(aid)[fx]
            sift  = ibs.get_annot_vecs(aid)[fx]
            if not ut.get_argflag('--texknormplot'):
                aidstr = vh.get_aidstrs(aid)
                nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid))
                id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx,)
            else:
                id_str = nidstr = aidstr = ''
            info = ''
            if k == -1:
                if pt.is_texmode():
                    info = '\\vspace{1cm}'
                    info += 'Query $\\mathbf{d}_i$'
                    info += '\n\\_'
                    info += '\n\\_'
                else:
                    if len(id_str) > '':
                        info = 'Query: %s' % (id_str,)
                    else:
                        info = 'Query'
                type_ = 'Query'
            elif k < K:
                type_ = 'Match'
                if ut.get_argflag('--texknormplot') and  pt.is_texmode():
                    #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k])
                    info = '\\vspace{1cm}'
                    info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k])
                    info += '\n$s_{\\tt{LNBNN}}=%.3f$' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k])
                else:
                    info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k, qfx2_dist[0, k])
                    info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k])
            elif k < Knorm + K:
                type_ = 'Norm'
                if ut.get_argflag('--texknormplot') and  pt.is_texmode():
                    #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k])
                    info = '\\vspace{1cm}'
                    info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k])
                    info += '\n\\_'
                else:
                    info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k, qfx2_dist[0, k])
            else:
                raise Exception('[viz] problem k=%r')
            return (rchip, kp, sift, fx, aid, info, type_)

        extracted_list = []
        # Remember the query sift feature
        extracted_list.append(get_extract_tuple(qaid, qfx, -1))
        origsift = extracted_list[0][2]
        skipped = 0
        for k in range(K + Knorm):
            #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx:
            if qfx2_daid[0, k] == qaid:
                skipped += 1
                continue
            tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k)
            extracted_list.append(tup)
        # Draw the _select_ith_match plot
        nRows = len(extracted_list)
        if stride is None:
            stride = nRows
        # Draw selected feature matches
        prevsift = None
        px = 0  # plot offset
        px_shift = 0  # plot stride shift
        nExtracted = len(extracted_list)
        featrow_kw = dict(
            draw_chip=draw_chip, draw_desc=draw_desc, draw_warped=draw_warped,
            draw_unwarped=draw_unwarped,
        )
        if ut.get_argflag('--texknormplot'):
            featrow_kw['ell_color'] = pt.ORANGE
            pass
        for listx, tup in enumerate(extracted_list):
            (rchip, kp, sift, fx, aid, info, type_) = tup
            if listx % stride == 0:
                # Create a temporary nRows and fnum in case we are splitting
                # up nearest neighbors into separate figures with stride
                _fnum = fnum + listx
                _nRows = min(nExtracted - listx, stride)
                px_shift = px
                df2.figure(fnum=_fnum, docla=True, doclf=True)
            px_ = px - px_shift
            px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, px=px_,
                               prevsift=prevsift, origsift=origsift, aid=aid,
                               info=info, type_=type_, **featrow_kw)

            px += px_shift
            if prevsift is None or consecutive_distance_compare:
                prevsift = sift

        df2.adjust_subplots_safe(hspace=.85, wspace=0, top=.95, bottom=.087, left=.05, right=.95)

    except Exception as ex:
        print('[viz] Error in show nearest descriptors')
        print(ex)
        raise
Example #13
0
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5,
                             consecutive_distance_compare=False):
    # Plots the nearest neighbors of a given feature (qaid, qfx)
    if fnum is None:
        fnum = df2.next_fnum()
    try:
        # Flann NN query
        (qfx2_aid, qfx2_fx, qfx2_dist, K, Knorm) = query_helpers.get_annotfeat_nn_index(ibs, qaid, qfx)

        # Adds metadata to a feature match
        def get_extract_tuple(aid, fx, k=-1):
            rchip = ibs.get_annot_chips(aid)
            kp    = ibs.get_annot_kpts(aid)[fx]
            sift  = ibs.get_annot_desc(aid)[fx]
            if k == -1:
                info = '\nquery %s, fx=%r' % (vh.get_aidstrs(aid), fx)
                type_ = 'query'
            elif k < K:
                type_ = 'match'
                info = '\nmatch %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k])
            elif k < Knorm + K:
                type_ = 'norm'
                info = '\nnorm  %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k])
            else:
                raise Exception('[viz] problem k=%r')
            return (rchip, kp, sift, fx, aid, info, type_)

        extracted_list = []
        extracted_list.append(get_extract_tuple(qaid, qfx, -1))
        skipped = 0
        for k in xrange(K + Knorm):
            if qfx2_aid[0, k] == qaid and qfx2_fx[0, k] == qfx:
                skipped += 1
                continue
            tup = get_extract_tuple(qfx2_aid[0, k], qfx2_fx[0, k], k)
            extracted_list.append(tup)
        # Draw the _select_ith_match plot
        nRows, nCols = len(extracted_list), 3
        if stride is None:
            stride = nRows
        # Draw selected feature matches
        prevsift = None
        px = 0  # plot offset
        px_shift = 0  # plot stride shift
        nExtracted = len(extracted_list)
        for listx, tup in enumerate(extracted_list):
            (rchip, kp, sift, fx, aid, info, type_) = tup
            if listx % stride == 0:
                # Create a temporary nRows and fnum in case we are splitting
                # up nearest neighbors into separate figures with stride
                _fnum = fnum + listx
                _nRows = min(nExtracted - listx, stride)
                px_shift = px
                df2.figure(fnum=_fnum, docla=True, doclf=True)
            printDBG('[viz] ' + info.replace('\n', ''))
            px_ = px - px_shift
            px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, nCols, px_,
                               prevsift=prevsift, aid=aid, info=info, type_=type_) + px_shift
            if prevsift is None or consecutive_distance_compare:
                prevsift = sift

        df2.adjust_subplots_safe(hspace=1)

    except Exception as ex:
        print('[viz] Error in show nearest descriptors')
        print(ex)
        raise
Example #14
0
    def select_ith_match(self, mx):
        """
        Selects the ith match and visualizes and prints information concerning
        features weights, keypoint details, and sift descriptions

        Args:
            mx (int) - the ith match to visualize
            qaid (int) - query annotation id
            aid (int) - database annotation id

        CommandLine:
            python -m ibeis.viz.interact.interact_matches --test-select_ith_match --show

        Example:
            >>> # DISABLE_DOCTEST
            >>> from ibeis.viz.interact.interact_matches import *  # NOQA
            >>> self = testdata_match_interact(mx=1)
            >>> pt.show_if_requested()
        """
        ibs        = self.ibs
        qaid       = self.qaid
        aid        = self.daid
        fnum       = self.fnum
        figtitle   = self.figtitle
        rchip1     = self.rchip1
        rchip2     = self.rchip2
        aid        = self.daid
        same_fig   = self.same_fig
        self.mx    = mx
        print('+--- SELECT --- ')
        print('qaid=%r, daid=%r' % (qaid, aid))
        print('... selecting mx-th=%r feature match' % mx)
        if False:
            print('score stats:')
            print(ut.get_stats_str(self.fsv, axis=0, newlines=True))
            print('fsv[mx] = %r' % (self.fsv[mx],))
            print('fs[mx] = %r' % (self.fs[mx],))
        """
        # test feature weights of actual chips
        fx1, fx2 = fm[mx]
        daid = aid
        ibs.get_annot_fgweights([daid])[0][fx2]
        ibs.get_annot_fgweights([qaid])[0][fx1]
        """
        #----------------------
        # Get info for the select_ith_match plot
        self.mode = 1
        # Get the mx-th feature match
        fx1, fx2 = self.fm[mx]
        fscore2  = self.fs[mx]
        fk2      = self.fk[mx]
        kpts1 = ibs.get_annot_kpts([self.qaid], config2_=self.query_config2_)[0]
        kpts2 = ibs.get_annot_kpts([self.daid], config2_=self.data_config2_)[0]
        desc1 = ibs.get_annot_vecs([self.qaid], config2_=self.query_config2_)[0]
        desc2 = ibs.get_annot_vecs([self.daid], config2_=self.data_config2_)[0]
        kp1, kp2     = kpts1[fx1], kpts2[fx2]
        sift1, sift2 = desc1[fx1], desc2[fx2]
        info1 = '\nquery'
        info2 = '\nk=%r fscore=%r' % (fk2, fscore2)
        #last_state.last_fx = fx1
        self.last_fx = fx1
        # Extracted keypoints to draw
        extracted_list = [(rchip1, kp1, sift1, fx1, self.qaid, info1),
                          (rchip2, kp2, sift2, fx2, self.daid, info2)]
        # Normalizng Keypoint
        #if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta:
        #    qfx2_norm = cm.filt2_meta['lnbnn']
        #    # Normalizing chip and feature
        #    (aid3, fx3, normk) = qfx2_norm[fx1]
        #    rchip3 = ibs.get_annot_chips(aid3)
        #    kp3 = ibs.get_annot_kpts(aid3)[fx3]
        #    sift3 = ibs.get_annot_vecs(aid3)[fx3]
        #    info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk)
        #    extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3))
        #else:
        #    pass
        # print('WARNING: meta doesnt exist')

        #----------------------
        # Draw the select_ith_match plot
        nRows, nCols = len(extracted_list) + same_fig, 3
        # Draw matching chips and features
        sel_fm = np.array([(fx1, fx2)])
        pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1)
        vert = self.vert if self.vert is not None else False
        self.chipmatch_view(pnum1, ell_alpha=.4, ell_linewidth=1.8,
                            colors=df2.BLUE, sel_fm=sel_fm, vert=vert)
        # Draw selected feature matches
        px = nCols * same_fig  # plot offset
        prevsift = None
        if not same_fig:
            #fnum2 = fnum + len(viz.FNUMS)
            fnum2 = self.fnum2
            fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True)
        else:
            fnum2 = fnum
        for (rchip, kp, sift, fx, aid, info) in extracted_list:
            px = viz_featrow.draw_feat_row(rchip, fx, kp, sift, fnum2, nRows, nCols, px,
                                           prevsift=prevsift, aid=aid, info=info)
            prevsift = sift
        if not same_fig:
            ih.connect_callback(fig2, 'button_press_event', self.on_click)
            df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
Example #15
0
    def select_ith_match(self, mx):
        """
        Selects the ith match and visualizes and prints information concerning
        features weights, keypoint details, and sift descriptions
        """
        import plottool as pt
        from plottool import viz_featrow
        from plottool import interact_helpers as ih
        # <CLOSURE VARS>
        fnum       = self.fnum
        same_fig   = self.same_fig
        rchip1     = self.rchip1
        rchip2     = self.rchip2
        # </CLOSURE VARS>
        self.mx    = mx
        print('+--- SELECT --- ')
        print('... selecting mx-th=%r feature match' % mx)
        fsv = self.fsv
        fs  = self.fs
        print('score stats:')
        print(ut.get_stats_str(fsv, axis=0, newlines=True))
        print('fsv[mx] = %r' % (fsv[mx],))
        print('fs[mx] = %r' % (fs[mx],))
        #----------------------
        # Get info for the select_ith_match plot
        self.mode = 1
        # Get the mx-th feature match
        fx1, fx2 = self.fm[mx]

        # Older info
        fscore2  = self.fs[mx]
        fk2      = None
        kp1, kp2     = self.kpts1[fx1], self.kpts2[fx2]
        vecs1, vecs2 = self.vecs1[fx1], self.vecs2[fx2]
        info1 = '\nquery'
        info2 = '\nk=%r fscore=%r' % (fk2, fscore2)
        #self.last_fx = fx1
        self.last_fx = fx1

        # Extracted keypoints to draw
        extracted_list = [(rchip1, kp1, vecs1, fx1, 'aid1', info1),
                          (rchip2, kp2, vecs2, fx2, 'aid2', info2)]
        # Normalizng Keypoint
        #if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta:
        #    qfx2_norm = cm.filt2_meta['lnbnn']
        #    # Normalizing chip and feature
        #    (aid3, fx3, normk) = qfx2_norm[fx1]
        #    rchip3 = ibs.get_annot_chips(aid3)
        #    kp3 = ibs.get_annot_kpts(aid3)[fx3]
        #    sift3 = ibs.get_annot_vecs(aid3)[fx3]
        #    info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk)
        #    extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3))
        #else:
        #    pass
        #    #print('WARNING: meta doesnt exist')

        #----------------------
        # Draw the select_ith_match plot
        nRows, nCols = len(extracted_list) + same_fig, 3
        # Draw matching chips and features
        sel_fm = np.array([(fx1, fx2)])
        pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1)
        vert = self.vert if self.vert is not None else False
        self.chipmatch_view(pnum=pnum1, ell_alpha=.4, ell_linewidth=1.8,
                            colors=pt.BLUE, sel_fm=sel_fm, vert=vert)
        # Draw selected feature matches
        px = nCols * same_fig  # plot offset
        prevsift = None
        if not same_fig:
            #fnum2 = fnum + len(viz.FNUMS)
            fnum2 = self.fnum2
            fig2 = pt.figure(fnum=fnum2, docla=True, doclf=True)
        else:
            fnum2 = fnum

        for (rchip, kp, sift, fx, aid, info) in extracted_list:
            px = viz_featrow.draw_feat_row(
                rchip, fx, kp, sift, fnum2, nRows, nCols, px,
                prevsift=prevsift, aid=aid, info=info)
            prevsift = sift
        if not same_fig:
            ih.connect_callback(fig2, 'button_press_event', self.on_click)
Example #16
0
def show_nearest_descriptors(ibs,
                             qaid,
                             qfx,
                             fnum=None,
                             stride=5,
                             qreq_=None,
                             **kwargs):
    r"""
    Args:
        ibs (ibeis.IBEISController): image analysis api
        qaid (int):  query annotation id
        qfx (int): query feature index
        fnum (int):  figure number
        stride (int):
        consecutive_distance_compare (bool):

    CommandLine:
        # Find a good match to inspect
        python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3

        # Now inspect it
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show
        python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256

    SeeAlso:
        plottool.viz_featrow
        ~/code/plottool/plottool/viz_featrow.py

    Example:
        >>> # DISABLE_DOCTEST
        >>> from ibeis.viz.viz_nearest_descriptors import *  # NOQA
        >>> import ibeis
        >>> # build test data
        >>> qreq_ = ibeis.testdata_qreq_()
        >>> ibs = ibeis.opendb('PZ_MTEST')
        >>> qaid = qreq_.qaids[0]
        >>> qfx = ut.get_argval('--qfx', type_=None, default=879)
        >>> fnum = None
        >>> stride = 5
        >>> # execute function
        >>> skip = False
        >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride,
        >>>                                   draw_chip=True,
        >>>                                   draw_warped=True,
        >>>                                   draw_unwarped=False,
        >>>                                   draw_desc=False, qreq_=qreq_)
        >>> # verify results
        >>> print(result)
        >>> pt.show_if_requested()
    """
    import plottool as pt  # NOQA
    consecutive_distance_compare = True
    draw_chip = kwargs.get('draw_chip', False)
    draw_desc = kwargs.get('draw_desc', True)
    draw_warped = kwargs.get('draw_warped', True)
    draw_unwarped = kwargs.get('draw_unwarped', True)
    #skip = kwargs.get('skip', True)
    # Plots the nearest neighbors of a given feature (qaid, qfx)
    if fnum is None:
        fnum = df2.next_fnum()
    try:
        # Flann NN query
        (qfx, qfx2_daid, qfx2_dfx, qfx2_dist, K,
         Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_)

        # Adds metadata to a feature match
        def get_extract_tuple(aid, fx, k=-1):
            rchip = ibs.get_annot_chips(aid)
            kp = ibs.get_annot_kpts(aid)[fx]
            sift = ibs.get_annot_vecs(aid)[fx]
            if not ut.get_argflag('--texknormplot'):
                aidstr = vh.get_aidstrs(aid)
                nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid))
                id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx, )
            else:
                id_str = nidstr = aidstr = ''
            info = ''
            if k == -1:
                if pt.is_texmode():
                    info = '\\vspace{1cm}'
                    info += 'Query $\\mathbf{d}_i$'
                    info += '\n\\_'
                    info += '\n\\_'
                else:
                    if len(id_str) > '':
                        info = 'Query: %s' % (id_str, )
                    else:
                        info = 'Query'
                type_ = 'Query'
            elif k < K:
                type_ = 'Match'
                if ut.get_argflag('--texknormplot') and pt.is_texmode():
                    #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k])
                    info = '\\vspace{1cm}'
                    info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % (
                        k, qfx2_dist[0, k])
                    info += '\n$s_{\\tt{LNBNN}}=%.3f$' % (
                        qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k])
                else:
                    info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k,
                                                          qfx2_dist[0, k])
                    info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] -
                                              qfx2_dist[0, k])
            elif k < Knorm + K:
                type_ = 'Norm'
                if ut.get_argflag('--texknormplot') and pt.is_texmode():
                    #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k])
                    info = '\\vspace{1cm}'
                    info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % (
                        k, qfx2_dist[0, k])
                    info += '\n\\_'
                else:
                    info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k,
                                                              qfx2_dist[0, k])
            else:
                raise Exception('[viz] problem k=%r')
            return (rchip, kp, sift, fx, aid, info, type_)

        extracted_list = []
        # Remember the query sift feature
        extracted_list.append(get_extract_tuple(qaid, qfx, -1))
        origsift = extracted_list[0][2]
        skipped = 0
        for k in range(K + Knorm):
            #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx:
            if qfx2_daid[0, k] == qaid:
                skipped += 1
                continue
            tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k)
            extracted_list.append(tup)
        # Draw the _select_ith_match plot
        nRows = len(extracted_list)
        if stride is None:
            stride = nRows
        # Draw selected feature matches
        prevsift = None
        px = 0  # plot offset
        px_shift = 0  # plot stride shift
        nExtracted = len(extracted_list)
        featrow_kw = dict(
            draw_chip=draw_chip,
            draw_desc=draw_desc,
            draw_warped=draw_warped,
            draw_unwarped=draw_unwarped,
        )
        if ut.get_argflag('--texknormplot'):
            featrow_kw['ell_color'] = pt.ORANGE
            pass
        for listx, tup in enumerate(extracted_list):
            (rchip, kp, sift, fx, aid, info, type_) = tup
            if listx % stride == 0:
                # Create a temporary nRows and fnum in case we are splitting
                # up nearest neighbors into separate figures with stride
                _fnum = fnum + listx
                _nRows = min(nExtracted - listx, stride)
                px_shift = px
                df2.figure(fnum=_fnum, docla=True, doclf=True)
            px_ = px - px_shift
            px = draw_feat_row(rchip,
                               fx,
                               kp,
                               sift,
                               _fnum,
                               _nRows,
                               px=px_,
                               prevsift=prevsift,
                               origsift=origsift,
                               aid=aid,
                               info=info,
                               type_=type_,
                               **featrow_kw)

            px += px_shift
            if prevsift is None or consecutive_distance_compare:
                prevsift = sift

        df2.adjust_subplots_safe(hspace=.85,
                                 wspace=0,
                                 top=.95,
                                 bottom=.087,
                                 left=.05,
                                 right=.95)

    except Exception as ex:
        print('[viz] Error in show nearest descriptors')
        print(ex)
        raise