Exemplo n.º 1
0
def match(args):
    """Compute all matches for a single image"""
    im1, candidates, i, n, ctx = args
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    im1_matches = {}

    for im2 in candidates:
        # preemptive matching
        if preemptive_threshold > 0:
            t = time.time()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(ctx.f_pre[im1], ctx.f_pre[im2], config)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(len(matches_pre), time.time() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug("Discarding based of preemptive matches {0} < {1}".format(len(matches_pre), preemptive_threshold))
                continue

        # symmetric matching
        t = time.time()
        p1, f1, c1 = ctx.data.load_features(im1)
        i1 = ctx.data.load_feature_index(im1, f1)

        p2, f2, c2 = ctx.data.load_features(im2)
        i2 = ctx.data.load_feature_index(im2, f2)

        matches = matching.match_symmetric(f1, i1, f2, i2, config)
        logger.debug('{} - {} has {} candidate matches'.format(im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        # robust matching
        t_robust_matching = time.time()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        rmatches = matching.robust_match(p1, p2, camera1, camera2, matches, config)

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue
        im1_matches[im2] = rmatches
        
        logger.debug('Robust matching time : {0}s'.format( time.time() - t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format( len(rmatches), len(matches), time.time() - t))
    ctx.data.save_matches(im1, im1_matches)
Exemplo n.º 2
0
def match(args):
    """Compute all matches for a single image"""
    log.setup()

    im1, candidates, i, n, ctx = args
    #cv2.imshow('123',im1)
    print("....................1%s" % im1)
    print("....................2%s" % candidates)
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    im1_matches = {}

    for im2 in candidates:
        # preemptive matching
        if preemptive_threshold > 0:
            t = timer()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(ctx.f_pre[im1],
                                                 ctx.f_pre[im2], config)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(
                len(matches_pre),
                timer() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug(
                    "Discarding based of preemptive matches {0} < {1}".format(
                        len(matches_pre), preemptive_threshold))
                continue

        # symmetric matching
        t = timer()
        p1, f1, c1 = ctx.data.load_features(im1)
        p2, f2, c2 = ctx.data.load_features(im2)

        if config['matcher_type'] == 'FLANN':
            print('.....................using FLANN step 1')
            i1 = ctx.data.load_feature_index(im1, f1)
            i2 = ctx.data.load_feature_index(im2, f2)
        else:
            i1 = None
            i2 = None

        matches = matching.match_symmetric(
            f1, i1, f2, i2,
            config)  #, p1, p2, im1, im2)#add keypoints and images qli
        # Apply ratio test
        """
        matchesMask = [[0,0] for ii in range(len(matches))]
        for ii,(m,n) in enumerate(matches):
           if 0.55*n.distance<m.distance < 0.80*n.distance:
              matchesMask[ii]=[1,0]
        
        draw_params=dict(matchesMask=matchesMask)
        """
        siftQ = cv2.xfeatures2d.SIFT_create()
        print('loading image 1 ..................%s' % im1)
        print('loading image 2 ..................%s' % im2)
        img1 = cv2.imread(
            '/home/qli/workspace/OpenSfM/data/MattGPS/images/%s' % im1)
        img2 = cv2.imread(
            '/home/qli/workspace/OpenSfM/data/MattGPS/images/%s' % im2)

        kp1, des1 = siftQ.detectAndCompute(img1, None)
        kp2, des2 = siftQ.detectAndCompute(img2, None)
        # BFMatcher with default params
        bf = cv2.BFMatcher()
        matchesQ = bf.knnMatch(des1, des2, k=2)
        matchesMask = [[0, 0] for i in range(len(matchesQ))]
        for i, (m, n) in enumerate(matchesQ):
            if 0.55 * n.distance < m.distance < 0.80 * n.distance:
                matchesMask[i] = [1, 0]
            # cv2.drawMatchesKnn expects list of lists as matches.
        draw_params = dict(matchesMask=matchesMask)
        img3 = None
        img3 = cv2.drawMatchesKnn(img1,
                                  kp1,
                                  img2,
                                  kp2,
                                  matchesQ,
                                  None,
                                  flags=2,
                                  **draw_params)
        #namestr= '/home/qli/workspace/OpenSfM/data/MattGPS/images/'+str(im1)+str(im2)
        #cv2.imwrite(namestr,img3)
        #plt.figure(str(im1)+str(im2))
        #plt.imshow(img3)
        savefigname = "/home/qli/workspace/OpenSfM/data/MattGPS/GPS%s%s.jpg" % (
            str(im1), str(im2))
        #plt.savefig(savefigname)
        cv2.imwrite(savefigname, img3)
        #plt.show()
        #plt.close()
        #return
        logger.debug('{} - {} has {} candidate matches'.format(
            im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        # robust matching
        t_robust_matching = timer()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        rmatches = matching.robust_match(p1, p2, camera1, camera2, matches,
                                         config)

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue
        im1_matches[im2] = rmatches
        logger.debug('Robust matching time : {0}s'.format(timer() -
                                                          t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format(
            len(rmatches), len(matches),
            timer() - t))
    ctx.data.save_matches(im1, im1_matches)
Exemplo n.º 3
0
def match(args):
    '''
    Compute all matches for a single image
    '''
    im1, candidates, i, n, ctx = args
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    if ctx.data.matches_exists(im1):
        im1_matches = ctx.data.load_matches(im1)
    else:
        im1_matches = {}

    for im2 in candidates:
        if im2 in im1_matches:
            continue

        # preemptive matching
        if preemptive_threshold > 0:
            t = time.time()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(ctx.f_pre[im1],
                                                 ctx.f_pre[im2], config)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(
                len(matches_pre),
                time.time() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug(
                    "Discarding based of preemptive matches {0} < {1}".format(
                        len(matches_pre), preemptive_threshold))
                continue

        # symmetric matching
        t = time.time()
        p1, f1, c1 = ctx.data.load_features(im1)
        # if we are using bruteforce matching, the loaded index will simplily be False.
        i1 = ctx.data.load_feature_index(im1, f1)

        p2, f2, c2 = ctx.data.load_features(im2)
        i2 = ctx.data.load_feature_index(im2, f2)

        matches = matching.match_symmetric(f1, i1, f2, i2, config)
        logger.debug('{} - {} has {} candidate matches'.format(
            im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        # robust matching
        t_robust_matching = time.time()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        # add extra matches on the road with homography method
        # filter the candidate points by semantic segmentation

        rmatches = matching.robust_match(p1, p2, camera1, camera2, matches,
                                         config)

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue
        im1_matches[im2] = rmatches
        logger.debug('Robust matching time : {0}s'.format(time.time() -
                                                          t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format(
            len(rmatches), len(matches),
            time.time() - t))
    ctx.data.save_matches(im1, im1_matches)
Exemplo n.º 4
0
def match(args):
    """Compute all matches for a single image"""
    log.setup()

    im1, candidates, i, n, ctx = args
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    im1_matches = {}

    for im2 in candidates:
        # preemptive matching
        if preemptive_threshold > 0:
            t = timer()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(
                ctx.f_pre[im1], ctx.f_pre[im2], config)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(
                len(matches_pre), timer() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug(
                    "Discarding based of preemptive matches {0} < {1}".format(
                        len(matches_pre), preemptive_threshold))
                continue

        # symmetric matching
        t = timer()
        p1, f1, c1 = ctx.data.load_features(im1)
        p2, f2, c2 = ctx.data.load_features(im2)

        if config['matcher_type'] == 'FLANN':
            i1 = ctx.data.load_feature_index(im1, f1)
            i2 = ctx.data.load_feature_index(im2, f2)
        else:
            i1 = None
            i2 = None

        matches = matching.match_symmetric(f1, i1, f2, i2, config)
        logger.debug('{} - {} has {} candidate matches'.format(
            im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        # robust matching
        t_robust_matching = timer()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        rmatches = matching.robust_match(p1, p2, camera1, camera2, matches,
                                         config)

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue
        im1_matches[im2] = rmatches
        logger.debug('Robust matching time : {0}s'.format(
            timer() - t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format(
            len(rmatches), len(matches), timer() - t))
    ctx.data.save_matches(im1, im1_matches)
Exemplo n.º 5
0
def match(args):
    '''
    Compute all matches for a single image
    '''
    im1, candidates, i, n, ctx = args
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    path_seg = ctx.data.data_path + "/images/output/results/frontend_vgg/" + os.path.splitext(
        im1)[0] + '.png'
    file_name = Path(path_seg)
    if file_name.is_file():
        im1_seg = Image.open(path_seg)
        im1_seg = np.array(im1_seg)
    p1, f1, c1 = ctx.data.load_features(im1)

    # if we are using bruteforce matching, the loaded index will simplily be False.
    i1 = ctx.data.load_feature_index(im1, f1)
    if file_name.is_file():
        idx_u1 = im1_seg.shape[1] * (p1[:, 0] + 0.5)
        idx_v1 = im1_seg.shape[0] * (p1[:, 1] + 0.5)
        im1_seg = im1_seg[idx_v1.astype(np.int), idx_u1.astype(np.int)]
    else:
        im1_seg = None

    if ctx.data.matches_exists(im1):
        im1_matches = ctx.data.load_matches(im1)
    else:
        im1_matches = {}

    for im2 in candidates:
        if im2 in im1_matches:
            continue

        path_seg = ctx.data.data_path + "/images/output/results/frontend_vgg/" + os.path.splitext(
            im2)[0] + '.png'
        file_name = Path(path_seg)
        if file_name.is_file():
            im2_seg = Image.open(path_seg)
            im2_seg = np.array(im2_seg)

        p2, f2, c2 = ctx.data.load_features(im2)
        i2 = ctx.data.load_feature_index(im2, f2)

        if file_name.is_file():
            idx_u2 = im2_seg.shape[1] * (p2[:, 0] + 0.5)
            idx_v2 = im2_seg.shape[0] * (p2[:, 1] + 0.5)
            im2_seg = im2_seg[idx_v2.astype(np.int), idx_u2.astype(np.int)]
        else:
            im2_seg = None

        # preemptive matching
        if preemptive_threshold > 0:
            t = time.time()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(ctx.f_pre[im1],
                                                 ctx.f_pre[im2], config,
                                                 im1_seg, im2_seg)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(
                len(matches_pre),
                time.time() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug(
                    "Discarding based of preemptive matches {0} < {1}".format(
                        len(matches_pre), preemptive_threshold))
                continue

        # symmetric matching
        t = time.time()

        matches = matching.match_symmetric(f1, i1, f2, i2, config, im1_seg,
                                           im2_seg)
        logger.debug('{} - {} has {} candidate matches'.format(
            im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        # robust matching
        t_robust_matching = time.time()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        # add extra matches on the road with homography method
        # filter the candidate points by semantic segmentation

        rmatches = matching.robust_match(p1, p2, camera1, camera2, matches,
                                         config)

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue
        im1_matches[im2] = rmatches
        logger.debug('Robust matching time : {0}s'.format(time.time() -
                                                          t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format(
            len(rmatches), len(matches),
            time.time() - t))
    ctx.data.save_matches(im1, im1_matches)
Exemplo n.º 6
0
def match(args):
    """Compute all matches for a single image"""
    log.setup()

    im1, candidates, i, n, ctx = args
    logger.info('Matching {}  -  {} / {}'.format(im1, i + 1, n))

    config = ctx.data.config
    robust_matching_min_match = config['robust_matching_min_match']
    preemptive_threshold = config['preemptive_threshold']
    lowes_ratio = config['lowes_ratio']
    preemptive_lowes_ratio = config['preemptive_lowes_ratio']

    im1_matches = {}
    im1_all_matches = {}
    im1_all_robust_matches = {}
    im1_valid_rmatches = {}
    im1_T = {}
    im1_F = {}
    im1_valid_inliers = {}
    im1_calibration_flag = {}
    im1_unthresholded_matches = {}

    for im2 in candidates:
        # preemptive matching
        if preemptive_threshold > 0:
            t = timer()
            config['lowes_ratio'] = preemptive_lowes_ratio
            matches_pre = matching.match_lowe_bf(
                ctx.f_pre[im1], ctx.f_pre[im2], config)
            config['lowes_ratio'] = lowes_ratio
            logger.debug("Preemptive matching {0}, time: {1}s".format(
                len(matches_pre), timer() - t))
            if len(matches_pre) < preemptive_threshold:
                logger.debug(
                    "Discarding based of preemptive matches {0} < {1}".format(
                        len(matches_pre), preemptive_threshold))
                im1_all_matches[im2] = matches_pre
                im1_all_robust_matches[im2] = []
                im1_valid_rmatches[im2] = -1
                continue

        # symmetric matching
        t = timer()
        p1, f1, c1 = ctx.data.load_features(im1)
        p2, f2, c2 = ctx.data.load_features(im2)

        if config['matcher_type'] == 'FLANN':
            i1 = ctx.data.load_feature_index(im1, f1)
            i2 = ctx.data.load_feature_index(im2, f2)
        else:
            i1 = None
            i2 = None

        matches_all = classifier.unthresholded_match_symmetric(f1, i1, f2, i2, config)
        if config['matcher_type'] == 'FLANN':
            # Flann returns squared L2 distances
            ri = np.where((matches_all[:,2] <= config['lowes_ratio']**2) & (matches_all[:,3] <= config['lowes_ratio']**2))[0]
        else:
            ri = np.where((matches_all[:,2] <= config['lowes_ratio']) & (matches_all[:,3] <= config['lowes_ratio']))[0]
        matches = matches_all[ri,:]
        
        logger.debug('{} - {} has {} candidate matches'.format(
            im1, im2, len(matches)))
        if len(matches) < robust_matching_min_match:
            im1_matches[im2] = []
            im1_all_matches[im2] = matches
            im1_unthresholded_matches[im2] = matches_all
            im1_all_robust_matches[im2] = []
            im1_valid_rmatches[im2] = 0
            continue

        # robust matching
        t_robust_matching = timer()
        camera1 = ctx.cameras[ctx.exifs[im1]['camera']]
        camera2 = ctx.cameras[ctx.exifs[im2]['camera']]

        [rmatches, T, F, validity], calibration_flag = matching.robust_match(p1, p2, camera1, camera2, matches,
                                         config)

        im1_all_matches[im2] = matches
        im1_unthresholded_matches[im2] = matches_all
        im1_all_robust_matches[im2] = rmatches
        im1_valid_rmatches[im2] = 1
        im1_T[im2] = T.tolist()
        im1_F[im2] = F.tolist()
        im1_valid_inliers[im2] = validity
        im1_calibration_flag[im2] = calibration_flag

        if len(rmatches) < robust_matching_min_match:
            im1_matches[im2] = []
            continue

        im1_matches[im2] = rmatches
        logger.debug('Robust matching time : {0}s'.format(
            timer() - t_robust_matching))

        logger.debug("Full matching {0} / {1}, time: {2}s".format(
            len(rmatches), len(matches), timer() - t))
    ctx.data.save_matches(im1, im1_matches)
    ctx.data.save_unthresholded_matches(im1, im1_unthresholded_matches)
    ctx.data.save_all_matches(im1, im1_all_matches, im1_valid_rmatches, im1_all_robust_matches)
    ctx.data.save_pairwise_results(im1, im1_T, im1_F, im1_valid_inliers, im1_calibration_flag)