Beispiel #1
0
def spatially_verify(kpts1, kpts2, rchip_size2, fm, fs):
    '''1) compute a robust transform from img2 -> img1
       2) keep feature matches which are inliers 
       returns fm_V, fs_V, H '''
    # Return if pathological
    min_num_inliers   = 4
    if len(fm) < min_num_inliers:
        return __default_sv_return()
    # Get homography parameters
    xy_thresh         = params.__XY_THRESH__
    scale_thresh_high = params.__SCALE_THRESH_HIGH__
    scale_thresh_low  = params.__SCALE_THRESH_LOW__
    if params.__USE_CHIP_EXTENT__:
        diaglen_sqrd = rchip_size2[0]**2 + rchip_size2[1]**2
    else:
        x_m = kpts2[fm[:,1],0].T
        y_m = kpts2[fm[:,1],1].T
        diaglen_sqrd = sv2.calc_diaglen_sqrd(x_m, y_m)
    # Try and find a homography
    sv_tup = sv2.homography_inliers(kpts1, kpts2, fm, xy_thresh, 
                                    scale_thresh_high, scale_thresh_low,
                                    diaglen_sqrd, min_num_inliers)
    if sv_tup is None:
        return __default_sv_return()
    # Return the inliers to the homography
    (H, inliers, Aff, aff_inliers) = sv_tup
    fm_V = fm[inliers, :]
    fs_V = fs[inliers]
    return fm_V, fs_V
Beispiel #2
0
def __spatially_verify(kpts1, kpts2, fm, fs, min_num_inliers, xy_thresh,
                       scale_thresh_high, scale_thresh_low, diaglen_sqrd):
    '''Work function for spatial verification'''
    sv_tup = sv2.homography_inliers(kpts1, kpts2, fm, xy_thresh, 
                                    scale_thresh_high, scale_thresh_low,
                                    diaglen_sqrd, min_num_inliers)
    if not sv_tup is None:
        # Return the inliers to the homography
        (H, inliers, Aff, aff_inliers) = sv_tup
        fm_V = fm[inliers, :]
        fs_V = fs[inliers, :]
    else:
        fm_V = np.empty((0, 2))
        fs_V = np.array((0, 1))
        H = np.eye(3)
    return fm_V, fs_V, H
def spatial_verification(hs, qcx2_chipmatch, qdat):
    sv_cfg = qdat.cfg.sv_cfg
    if not sv_cfg.sv_on or sv_cfg.xy_thresh is None:
        print('[mf] Step 5) Spatial verification: off')
        return qcx2_chipmatch
    print('[mf] Step 5) Spatial verification: ' + sv_cfg.get_uid())
    prescore_method = sv_cfg.prescore_method
    nShortlist = sv_cfg.nShortlist
    xy_thresh = sv_cfg.xy_thresh
    min_scale = sv_cfg.scale_thresh_low
    max_scale = sv_cfg.scale_thresh_high
    use_chip_extent = sv_cfg.use_chip_extent
    min_nInliers = sv_cfg.min_nInliers
    just_affine = sv_cfg.just_affine
    cx2_rchip_size = hs.cpaths.cx2_rchip_size
    cx2_kpts = hs.feats.cx2_kpts
    qcx2_chipmatchSV = {}
    #printDBG(qdat._dcxs)
    dcxs_ = set(qdat._dcxs)
    USE_1_to_2 = True
    # Find a transform from chip2 to chip1 (the old way was 1 to 2)
    for qcx in qcx2_chipmatch.iterkeys():
        #printDBG('[mf] verify qcx=%r' % qcx)
        chipmatch = qcx2_chipmatch[qcx]
        cx2_prescore = score_chipmatch(hs, qcx, chipmatch, prescore_method,
                                       qdat)
        (cx2_fm, cx2_fs, cx2_fk) = chipmatch
        topx2_cx = cx2_prescore.argsort(
        )[::-1]  # Only allow indexed cxs to be in the top results
        topx2_cx = [cx for cx in iter(topx2_cx) if cx in dcxs_]
        nRerank = min(len(topx2_cx), nShortlist)
        # Precompute output container
        cx2_fm_V, cx2_fs_V, cx2_fk_V = new_fmfsfk(hs)
        # Query Keypoints
        kpts1 = cx2_kpts[qcx]
        # Check the diaglen sizes before doing the homography
        topx2_dlen_sqrd = _precompute_topx2_dlen_sqrd(cx2_rchip_size, cx2_kpts,
                                                      cx2_fm, topx2_cx,
                                                      nRerank, use_chip_extent,
                                                      USE_1_to_2)
        # spatially verify the top __NUM_RERANK__ results
        for topx in xrange(nRerank):
            cx = topx2_cx[topx]
            fm = cx2_fm[cx]
            #printDBG('[mf] vs topcx=%r, score=%r' % (cx, cx2_prescore[cx]))
            #printDBG('[mf] len(fm)=%r' % (len(fm)))
            if len(fm) >= min_nInliers:
                dlen_sqrd = topx2_dlen_sqrd[topx]
                kpts2 = cx2_kpts[cx]
                fs = cx2_fs[cx]
                fk = cx2_fk[cx]
                #printDBG('[mf] computing homog')
                sv_tup = sv2.homography_inliers(kpts1, kpts2, fm, xy_thresh,
                                                max_scale, min_scale,
                                                dlen_sqrd, min_nInliers,
                                                just_affine)
                #printDBG('[mf] sv_tup = %r' % (sv_tup,))
                if sv_tup is None:
                    print_('o')  # sv failure
                else:
                    # Return the inliers to the homography
                    (H, inliers) = sv_tup
                    cx2_fm_V[cx] = fm[inliers, :]
                    cx2_fs_V[cx] = fs[inliers]
                    cx2_fk_V[cx] = fk[inliers]
                    print_('.')  # verified something
            else:
                print_('x')  # not enough initial matches
        # Rebuild the feature match / score arrays to be consistent
        chipmatchSV = _fix_fmfsfk(cx2_fm_V, cx2_fs_V, cx2_fk_V)
        qcx2_chipmatchSV[qcx] = chipmatchSV
    print('')
    print('[mf] Finished sv')
    return qcx2_chipmatchSV
def spatial_verification(hs, qcx2_chipmatch, qdat):
    sv_cfg = qdat.cfg.sv_cfg
    if not sv_cfg.sv_on or sv_cfg.xy_thresh is None:
        print('[mf] Step 5) Spatial verification: off')
        return qcx2_chipmatch
    print('[mf] Step 5) Spatial verification: ' + sv_cfg.get_uid())
    prescore_method = sv_cfg.prescore_method
    nShortlist      = sv_cfg.nShortlist
    xy_thresh       = sv_cfg.xy_thresh
    min_scale = sv_cfg.scale_thresh_low
    max_scale = sv_cfg.scale_thresh_high
    use_chip_extent = sv_cfg.use_chip_extent
    min_nInliers    = sv_cfg.min_nInliers
    just_affine     = sv_cfg.just_affine
    cx2_rchip_size  = hs.cpaths.cx2_rchip_size
    cx2_kpts = hs.feats.cx2_kpts
    qcx2_chipmatchSV = {}
    #printDBG(qdat._dcxs)
    dcxs_ = set(qdat._dcxs)
    USE_1_to_2 = True
    # Find a transform from chip2 to chip1 (the old way was 1 to 2)
    for qcx in qcx2_chipmatch.iterkeys():
        #printDBG('[mf] verify qcx=%r' % qcx)
        chipmatch = qcx2_chipmatch[qcx]
        cx2_prescore = score_chipmatch(hs, qcx, chipmatch, prescore_method, qdat)
        (cx2_fm, cx2_fs, cx2_fk) = chipmatch
        topx2_cx = cx2_prescore.argsort()[::-1]  # Only allow indexed cxs to be in the top results
        topx2_cx = [cx for cx in iter(topx2_cx) if cx in dcxs_]
        nRerank = min(len(topx2_cx), nShortlist)
        # Precompute output container
        cx2_fm_V, cx2_fs_V, cx2_fk_V = new_fmfsfk(hs)
        # Query Keypoints
        kpts1 = cx2_kpts[qcx]
        # Check the diaglen sizes before doing the homography
        topx2_dlen_sqrd = _precompute_topx2_dlen_sqrd(cx2_rchip_size, cx2_kpts,
                                                      cx2_fm, topx2_cx, nRerank,
                                                      use_chip_extent,
                                                      USE_1_to_2)
        # spatially verify the top __NUM_RERANK__ results
        for topx in xrange(nRerank):
            cx = topx2_cx[topx]
            fm = cx2_fm[cx]
            #printDBG('[mf] vs topcx=%r, score=%r' % (cx, cx2_prescore[cx]))
            #printDBG('[mf] len(fm)=%r' % (len(fm)))
            if len(fm) >= min_nInliers:
                dlen_sqrd = topx2_dlen_sqrd[topx]
                kpts2 = cx2_kpts[cx]
                fs    = cx2_fs[cx]
                fk    = cx2_fk[cx]
                #printDBG('[mf] computing homog')
                sv_tup = sv2.homography_inliers(kpts1, kpts2, fm, xy_thresh,
                                                max_scale, min_scale, dlen_sqrd,
                                                min_nInliers, just_affine)
                #printDBG('[mf] sv_tup = %r' % (sv_tup,))
                if sv_tup is None:
                    print_('o')  # sv failure
                else:
                    # Return the inliers to the homography
                    (H, inliers) = sv_tup
                    cx2_fm_V[cx] = fm[inliers, :]
                    cx2_fs_V[cx] = fs[inliers]
                    cx2_fk_V[cx] = fk[inliers]
                    print_('.')  # verified something
            else:
                print_('x')  # not enough initial matches
        # Rebuild the feature match / score arrays to be consistent
        chipmatchSV = _fix_fmfsfk(cx2_fm_V, cx2_fs_V, cx2_fk_V)
        qcx2_chipmatchSV[qcx] = chipmatchSV
    print('')
    print('[mf] Finished sv')
    return qcx2_chipmatchSV