示例#1
0
def show_name(hs, nx, nx2_cxs=None, fnum=0, sel_cxs=[], subtitle='',
              annote=False, **kwargs):
    print('[viz] show_name nx=%r' % nx)
    nx2_name = hs.tables.nx2_name
    cx2_nx   = hs.tables.cx2_nx
    name = nx2_name[nx]
    if not nx2_cxs is None:
        cxs = nx2_cxs[nx]
    else:
        cxs = np.where(cx2_nx == nx)[0]
    print('[viz] show_name %r' % hs.cidstr(cxs))
    nRows, nCols = get_square_row_cols(len(cxs))
    print('[viz*] r=%r, c=%r' % (nRows, nCols))
    #gs2 = gridspec.GridSpec(nRows, nCols)
    pnum = lambda px: (nRows, nCols, px + 1)
    fig = df2.figure(fnum=fnum, pnum=pnum(0), **kwargs)
    fig.clf()
    # Trigger computation of all chips in parallel
    hs.refresh_features(cxs)
    for px, cx in enumerate(cxs):
        show_chip(hs, cx=cx, pnum=pnum(px), draw_ell=annote, kpts_alpha=.2)
        if cx in sel_cxs:
            ax = df2.gca()
            df2.draw_border(ax, df2.GREEN, 4)
        #plot_cx3(hs, cx)
    if isinstance(nx, np.ndarray):
        nx = nx[0]
    if isinstance(name, np.ndarray):
        name = name[0]

    figtitle = 'Name View nx=%r name=%r' % (nx, name)
    df2.set_figtitle(figtitle)
示例#2
0
文件: viz.py 项目: obaiga/hotspotter
def draw_feat_row(rchip,
                  fx,
                  kp,
                  sift,
                  fnum,
                  nRows,
                  nCols,
                  px,
                  prevsift=None,
                  cx=None,
                  info='',
                  type_=None):
    pnum_ = lambda px: (nRows, nCols, px)

    def _draw_patch(**kwargs):
        return extract_patch.draw_keypoint_patch(rchip, kp, sift, **kwargs)

    # Feature strings
    xy_str, acd_str, scale = kp_info(kp)

    # Draw the unwarped selected feature
    ax = _draw_patch(fnum=fnum, pnum=pnum_(px + 1))
    ax._hs_viewtype = 'unwarped'
    ax._hs_cx = cx
    ax._hs_fx = fx
    unwarped_lbl = 'affine feature inv(A) =\n' + acd_str
    df2.set_xlabel(unwarped_lbl, ax)

    # Draw the warped selected feature
    ax = _draw_patch(fnum=fnum, pnum=pnum_(px + 2), warped=True)
    ax._hs_viewtype = 'warped'
    ax._hs_cx = cx
    ax._hs_fx = fx
    warped_lbl = ('warped feature\n' + 'fx=%r scale=%.1f\n' + '%s' +
                  info) % (fx, scale, xy_str)
    df2.set_xlabel(warped_lbl, ax)

    border_color = {
        None: None,
        'query': None,
        'match': df2.BLUE,
        'norm': df2.ORANGE
    }[type_]
    if border_color is not None:
        df2.draw_border(ax, color=border_color)

    # Draw the SIFT representation
    sigtitle = '' if px != 3 else 'sift histogram'
    ax = df2.plot_sift_signature(sift, sigtitle, fnum=fnum, pnum=pnum_(px + 3))
    ax._hs_viewtype = 'histogram'
    if prevsift is not None:
        from hotspotter import algos
        dist_list = ['L1', 'L2', 'hist_isect', 'emd']
        distmap = algos.compute_distances(sift, prevsift, dist_list)
        dist_str = ', '.join(
            ['(%s, %.1E)' % (key, val) for key, val in distmap.iteritems()])
        df2.set_xlabel(dist_str)
    return px + nCols
示例#3
0
def draw_feat_row(rchip, fx, kp, sift, fnum, nRows, nCols, px, prevsift=None,
                  cx=None, info='', type_=None):
    pnum_ = lambda px: (nRows, nCols, px)

    def _draw_patch(**kwargs):
        return extract_patch.draw_keypoint_patch(rchip, kp, sift, **kwargs)

    # Feature strings
    xy_str, acd_str, scale = kp_info(kp)

    # Draw the unwarped selected feature
    ax = _draw_patch(fnum=fnum, pnum=pnum_(px + 1))
    ax._hs_viewtype = 'unwarped'
    ax._hs_cx = cx
    ax._hs_fx = fx
    unwarped_lbl = 'affine feature inv(A) =\n' + acd_str
    df2.set_xlabel(unwarped_lbl, ax)

    # Draw the warped selected feature
    ax = _draw_patch(fnum=fnum, pnum=pnum_(px + 2), warped=True)
    ax._hs_viewtype = 'warped'
    ax._hs_cx = cx
    ax._hs_fx = fx
    warped_lbl = ('warped feature\n' +
                  'fx=%r scale=%.1f\n' +
                  '%s' + info) % (fx, scale, xy_str)
    df2.set_xlabel(warped_lbl, ax)

    border_color = {None: None,
                    'query': None,
                    'match': df2.BLUE,
                    'norm': df2.ORANGE}[type_]
    if border_color is not None:
        df2.draw_border(ax, color=border_color)

    # Draw the SIFT representation
    sigtitle = '' if px != 3 else 'sift histogram'
    ax = df2.plot_sift_signature(sift, sigtitle, fnum=fnum, pnum=pnum_(px + 3))
    ax._hs_viewtype = 'histogram'
    if prevsift is not None:
        from hotspotter import algos
        dist_list = ['L1', 'L2', 'hist_isect', 'emd']
        distmap = algos.compute_distances(sift, prevsift, dist_list)
        dist_str = ', '.join(['(%s, %.1E)' % (key, val) for key, val in distmap.iteritems()])
        df2.set_xlabel(dist_str)
    return px + nCols
示例#4
0
文件: viz.py 项目: obaiga/hotspotter
def show_name(hs,
              nx,
              nx2_cxs=None,
              fnum=0,
              sel_cxs=[],
              subtitle='',
              annote=False,
              **kwargs):
    print('[viz] show_name nx=%r' % nx)
    nx2_name = hs.tables.nx2_name
    cx2_nx = hs.tables.cx2_nx
    name = nx2_name[nx]
    if not nx2_cxs is None:
        cxs = nx2_cxs[nx]
    else:
        cxs = np.where(cx2_nx == nx)[0]
    print('[viz] show_name %r' % hs.cidstr(cxs))
    nRows, nCols = get_square_row_cols(len(cxs))
    print('[viz*] r=%r, c=%r' % (nRows, nCols))
    #gs2 = gridspec.GridSpec(nRows, nCols)
    pnum = lambda px: (nRows, nCols, px + 1)
    fig = df2.figure(fnum=fnum, pnum=pnum(0), **kwargs)
    fig.clf()
    # Trigger computation of all chips in parallel
    hs.refresh_features(cxs)
    for px, cx in enumerate(cxs):
        show_chip(hs, cx=cx, pnum=pnum(px), draw_ell=annote, kpts_alpha=.2)
        if cx in sel_cxs:
            ax = df2.gca()
            df2.draw_border(ax, df2.GREEN, 4)
        #plot_cx3(hs, cx)
    if isinstance(nx, np.ndarray):
        nx = nx[0]
    if isinstance(name, np.ndarray):
        name = name[0]

    figtitle = 'Name View nx=%r name=%r' % (nx, name)
    df2.set_figtitle(figtitle)
示例#5
0
文件: viz.py 项目: obaiga/hotspotter
def annotate_chipres(hs,
                     res,
                     cx,
                     showTF=True,
                     showScore=True,
                     title_pref='',
                     title_suff='',
                     show_2nd_gname=True,
                     show_2nd_name=True,
                     show_1st=True,
                     time_appart=True,
                     in_image=False,
                     offset1=(0, 0),
                     offset2=(0, 0),
                     show_query=True,
                     xywh2=None,
                     **kwargs):

    printDBG('[viz] annotate_chipres()')
    #print('Did not expect args: %r' % (kwargs.keys(),))
    qcx = res.qcx
    score = res.cx2_score[cx]
    matched_kpts = np.float32(len(res.cx2_fs[cx]))
    #    print('matched_kpts= %r'%str(matched_kpts))
    # TODO Use this function when you clean show_chipres
    (truestr, falsestr, nonamestr) = ('TRUE', 'FALSE', '???')
    is_true, is_unknown = hs.is_true_match(qcx, cx)
    isgt_str = nonamestr if is_unknown else (truestr if is_true else falsestr)
    match_color = {
        nonamestr: df2.UNKNOWN_PURP,
        truestr: df2.TRUE_GREEN,
        falsestr: df2.FALSE_RED
    }[isgt_str]
    # Build title
    title = '*%s*' % isgt_str if showTF else ''
    if showScore:
        score_str = (' score=' + helpers.num_fmt(score)) % (score)
        score_str += ('   matched_kpts=' +
                      helpers.num_fmt(matched_kpts)) % (matched_kpts)
        title += score_str
    title = title_pref + str(title) + title_suff
    # Build xlabel
    xlabel_ = []

    if 'show_1st':
        xlabel_.append('top_gname=%r' % hs.cx2_gname(qcx))
        xlabel_.append('top_name=%r' % hs.cx2_name(qcx))

    if 'show_2nd_gname':
        xlabel_.append('\n below_gname=%r' % hs.cx2_gname(cx))
    if 'show_2nd_name':
        xlabel_.append('below_name=%r' % hs.cx2_name(cx))

    if 'time_appart':
        xlabel_.append('\n' + hs.get_timedelta_str(qcx, cx))
    xlabel = ', '.join(xlabel_)
    ax = df2.gca()
    ax._hs_viewtype = 'chipres'
    ax._hs_qcx = qcx
    ax._hs_cx = cx
    if NO_LABEL_OVERRIDE:
        title = ''
        xlabel = ''
    df2.set_title(title, ax)
    df2.set_xlabel(xlabel, ax)
    if in_image:
        roi1 = hs.cx2_roi(qcx) + np.array(list(offset1) + [0, 0])
        roi2 = hs.cx2_roi(cx) + np.array(list(offset2) + [0, 0])
        theta1 = hs.cx2_theta(qcx)
        theta2 = hs.cx2_theta(cx)
        # HACK!
        lbl1 = 'q' + hs.cidstr(qcx)
        lbl2 = hs.cidstr(cx)
        if show_query:
            df2.draw_roi(roi1, bbox_color=df2.ORANGE, label=lbl1, theta=theta1)
        df2.draw_roi(roi2, bbox_color=match_color, label=lbl2, theta=theta2)
        # No matches draw a red box
        if len(res.cx2_fm[cx]) == 0:
            df2.draw_boxedX(roi2, theta=theta2)
    else:
        if xywh2 is None:
            xy, w, h = df2._axis_xy_width_height(ax)
            xywh2 = (xy[0], xy[1], w, h)
        df2.draw_border(ax, match_color, 4, offset=offset2)
        # No matches draw a red box
        if len(res.cx2_fm[cx]) == 0:
            df2.draw_boxedX(xywh2)
示例#6
0
def annotate_chipres(hs, res, cx, showTF=True, showScore=True, title_pref='',
                     title_suff='', show_gname=False, show_name=True,
                     time_appart=True, in_image=False, offset1=(0, 0),
                     offset2=(0, 0), show_query=True, xywh2=None, **kwargs):
    printDBG('[viz] annotate_chipres()')
    #print('Did not expect args: %r' % (kwargs.keys(),))
    qcx = res.qcx
    score = res.cx2_score[cx]
    # TODO Use this function when you clean show_chipres
    (truestr, falsestr, nonamestr) = ('TRUE', 'FALSE', '???')
    is_true, is_unknown = hs.is_true_match(qcx, cx)
    isgt_str = nonamestr if is_unknown else (truestr if is_true else falsestr)
    match_color = {nonamestr: df2.UNKNOWN_PURP,
                   truestr:   df2.TRUE_GREEN,
                   falsestr:  df2.FALSE_RED}[isgt_str]
    # Build title
    title = '*%s*' % isgt_str if showTF else ''
    if showScore:
        score_str = (' score=' + helpers.num_fmt(score)) % (score)
        title += score_str
    title = title_pref + str(title) + title_suff
    # Build xlabel
    xlabel_ = []
    if 'show_gname':
        xlabel_.append('gname=%r' % hs.cx2_gname(cx))
    if 'show_name':
        xlabel_.append('name=%r' % hs.cx2_name(cx))
    if 'time_appart':
        xlabel_.append('\n' + hs.get_timedelta_str(qcx, cx))
    xlabel = ', '.join(xlabel_)
    ax = df2.gca()
    ax._hs_viewtype = 'chipres'
    ax._hs_qcx = qcx
    ax._hs_cx = cx
    if NO_LABEL_OVERRIDE:
        title = ''
        xlabel = ''
    df2.set_title(title, ax)
    df2.set_xlabel(xlabel, ax)
    if in_image:
        roi1 = hs.cx2_roi(qcx) + np.array(list(offset1) + [0, 0])
        roi2 = hs.cx2_roi(cx) + np.array(list(offset2) + [0, 0])
        theta1 = hs.cx2_theta(qcx)
        theta2 = hs.cx2_theta(cx)
        # HACK!
        lbl1 = 'q' + hs.cidstr(qcx)
        lbl2 = hs.cidstr(cx)
        if show_query:
            df2.draw_roi(roi1, bbox_color=df2.ORANGE, label=lbl1, theta=theta1)
        df2.draw_roi(roi2, bbox_color=match_color, label=lbl2, theta=theta2)
        # No matches draw a red box
        if len(res.cx2_fm[cx]) == 0:
            df2.draw_boxedX(roi2, theta=theta2)
    else:
        if xywh2 is None:
            xy, w, h = df2._axis_xy_width_height(ax)
            xywh2 = (xy[0], xy[1], w, h)
        df2.draw_border(ax, match_color, 4, offset=offset2)
        # No matches draw a red box
        if len(res.cx2_fm[cx]) == 0:
            df2.draw_boxedX(xywh2)
示例#7
0
 def draw_one_kp(patch, kp, plotx, color, desc=None):
     fig, ax = df2.imshow(patch, plotnum=(num_rows, num_cols, plotx))
     df2.draw_kpts2([kp], ell_color=color, pts=True)
     if not desc is None and not '--nodesc' in sys.argv:
         df2.draw_sift(desc, [kp])
     df2.draw_border(ax, color, 1)
示例#8
0
def viz_top_features(hs, res, low, high, fignum=0, draw_chips=True):
    from collections import defaultdict
    qcx = res.qcx
    cx2_nx = hs.tables.cx2_nx
    top_patches_list = get_top_scoring_patches(hs, res, low, high)
    num_rows = high-low
    num_cols = 4
    if params.__MATCH_TYPE__ == 'vsmany':
        num_cols = 6
    # Initialize Figure
    fig = df2.figure(fignum+1, plotnum=(num_rows, num_cols,1))
    cx2_rchip = defaultdict(int)
    cx2_kplist = defaultdict(list)
    def draw_one_kp(patch, kp, plotx, color, desc=None):
        fig, ax = df2.imshow(patch, plotnum=(num_rows, num_cols, plotx))
        df2.draw_kpts2([kp], ell_color=color, pts=True)
        if not desc is None and not '--nodesc' in sys.argv:
            df2.draw_sift(desc, [kp])
        df2.draw_border(ax, color, 1)

    for tx, (patches1, patches2, patchesN, cx, feat_score) in enumerate(top_patches_list):
        (kp1, subkp1, wkp1, patch1, wpatch1, cx1, desc1) = patches1
        (kp2, subkp2, wkp2, patch2, wpatch2, cx2, desc2) = patches2
        # draw on table
        # Draw Query Keypoint
        plotx = (tx*num_cols)
        draw_one_kp(patch1,  subkp1, plotx+1, df2.GREEN, desc1)
        qnx = cx2_nx[qcx]
        df2.plt.gca().set_xlabel('qcx=%r; qnx=%r' % (qcx, qnx))
        draw_one_kp(wpatch1, wkp1,   plotx+2, df2.GREEN, desc1)

        # Draw ith < k match
        draw_one_kp(patch2,  subkp2, plotx+3, df2.BLUE, desc2)
        nx = cx2_nx[cx]
        df2.plt.gca().set_xlabel('cx=%r; nx=%r' % (cx, nx))
        draw_one_kp(wpatch2, wkp2,   plotx+4, df2.BLUE, desc2)
        df2.plt.gca().set_xlabel('score=%r' % (feat_score))

        # Draw k+1th match
        if params.__MATCH_TYPE__ == 'vsmany':
            (kpN, subkpN, wkpN, patchN, wpatchN, cxN, descN) = patchesN
            draw_one_kp(patchN,  subkpN, plotx+5, df2.ORANGE, descN)
            nxN = cx2_nx[qcx]
            df2.plt.gca().set_xlabel('cxN=%r; nxN=%r' % (cxN, nxN))
            draw_one_kp(wpatchN, wkpN,   plotx+6, df2.ORANGE, descN)
        # Get other info
        cx2_rchip[cx] = hs.get_chip(cx)
        cx2_kplist[qcx].append(kp1)
        cx2_kplist[cx].append(kp2)
    # Draw annotations on
    df2.figure(plotnum=(num_rows, num_cols,1), title='Query Patch')
    df2.figure(plotnum=(num_rows, num_cols,2), title='Query Patch')
    df2.figure(plotnum=(num_rows, num_cols,3), title='Result Patch')
    df2.figure(plotnum=(num_rows, num_cols,4), title='Result Warped')

    if params.__MATCH_TYPE__ == 'vsmany':
        df2.figure(plotnum=(num_rows, num_cols,5), title='Normalizer Patch')
        df2.figure(plotnum=(num_rows, num_cols,6), title='Normalizer Warped')

    df2.set_figtitle('Top '+str(low)+' to '+str(high)+' scoring matches')
    if not draw_chips:
        return
    #
    # Draw on full images
    cx2_rchip[qcx] = hs.get_chip(qcx)
    cx_keys  = cx2_kplist.keys()
    cx_vals = map(len, cx2_kplist.values())
    cx_list = [x for (y,x) in sorted(zip(cx_vals, cx_keys))][::-1]
    num_chips = len(cx_list)
    pltnum_fn = lambda ix: (int(np.ceil(num_chips/2)), 2, ix)
    plotnum = pltnum_fn(1)
    fig2 = df2.figure(fignum-1, plotnum=plotnum)
    for ix, cx in enumerate(cx_list):
        plotnum = pltnum_fn(ix+1)
        fig2 = df2.figure(fignum-1, plotnum=plotnum)
        rchip = cx2_rchip[cx]
        fig, ax = df2.imshow(rchip)
        title_pref = ''
        color = df2.BLUE
        if res.qcx == cx:
            title_pref = 'q'
            color = df2.GREEN
        df2.draw_kpts2(cx2_kplist[cx], ell_color=color, ell_alpha=1)
        df2.draw_border(ax, color, 2)
        nx = cx2_nx[cx]
        ax.set_title(title_pref+'cx = %r; nx=%r' % (cx, nx))
        gname = hs.get_gname(cx)
        ax.set_xlabel(gname)