<commit_before>#!/usr/bin/python

import sys
sys.path.insert(0, "/usr/local/opencv-2.4.11/lib/python2.7/site-packages/")

import argparse
import commands
import cPickle as pickle
import cv2
import fnmatch
import math
import numpy as np
import os.path
from progress.bar import Bar
import scipy.spatial

sys.path.append('../lib')
import Matcher
import Pose
import ProjectMgr
import SRTM

# Rest all match point locations to their original direct
# georeferenced locations based on estimated camera pose and
# projection onto DEM earth surface

parser = argparse.ArgumentParser(description='Keypoint projection.')
parser.add_argument('--project', required=True, help='project directory')

args = parser.parse_args()

proj = ProjectMgr.ProjectMgr(args.project)
proj.load_image_info()
proj.load_features()
proj.undistort_keypoints()
proj.load_matches()

# setup SRTM ground interpolator
ref = proj.ned_reference_lla
sss = SRTM.NEDGround( ref, 2000, 2000, 30 )

# compute keypoint usage map
proj.compute_kp_usage()
                      
# fast way:
# 1. make a grid (i.e. 8x8) of uv coordinates covering the whole image
# 2. undistort these uv coordinates
# 3. project them into vectors
# 4. intersect them with the srtm terrain to get ned coordinates
# 5. use linearndinterpolator ... g = scipy.interpolate.LinearNDInterpolator([[0,0],[1,0],[0,1],[1,1]], [[0,4,8],[1,3,2],[2,2,-4],[4,1,0]])
#    with origin uv vs. 3d location to build a table
# 6. interpolate original uv coordinates to 3d locations
proj.fastProjectKeypointsTo3d(sss)

print "Indexing features by unique uv coordinates..."
# we typically expect many duplicated feature uv coordinates, but they
# may have different scaling or other attributes important during
# feature matching
for image in proj.image_list:
    print image.name
    # pass one, build a tmp structure of unique keypoints (by uv) and
    # the index of the first instance.
    image.kp_remap = {}
    for i, kp in enumerate(image.kp_list):
        if image.kp_used[i]:
            key = "%.3f-%.3f" % (kp.pt[0], kp.pt[1])
            if not key in image.kp_remap:
                image.kp_remap[key] = i
            # else:
            #     print "%d -> %d" % (i, image.kp_remap[key])
            #     print " ", image.coord_list[i], image.coord_list[image.kp_remap[key]]
    print " features:", len(image.kp_list)
    print " unique by uv and used:", len(image.kp_remap)

print "Collapsing keypoints with duplicate uv coordinates..."
# after feature matching we don't care about other attributes, just
# the uv coordinate.
for i1 in proj.image_list:
    for j, matches in enumerate(i1.match_list):
        i2 = proj.image_list[j]
        for k, pair in enumerate(matches):
            # print pair
            idx1 = pair[0]
            idx2 = pair[1]
            uv1 = list(i1.kp_list[idx1].pt)
            uv2 = list(i2.kp_list[idx2].pt)
            key1 = "%.3f-%.3f" % (uv1[0], uv1[1])
            key2 = "%.3f-%.3f" % (uv2[0], uv2[1])
            if j == 1 and idx2 == 1360:
                print key1, key2
            if j == 1 and idx2 == 3212:
                print key1, key2
            # print key1, key2
            new_idx1 = i1.kp_remap[key1]
            new_idx2 = i2.kp_remap[key2]
            if False and idx1 != new_idx1:
                print "1: %d -> %d" % (idx1, new_idx1)
                new_uv1 = list(i1.kp_list[new_idx1].pt)
                print "  [%.3f, %.3f] -> [%.3f, %.3f]" % (uv1[0], uv1[1],
                                                          new_uv1[0],
                                                          new_uv1[1])
                if not np.allclose(uv1, new_uv1):
                    print "OOPS!"
            if False and idx2 != new_idx2:
                print "2: %d -> %d" % (idx2, new_idx2)
                new_uv2 = list(i2.kp_list[new_idx2].pt)
                print "  [%.3f, %.3f] -> [%.3f, %.3f]" % (uv2[0], uv2[1],
                                                          new_uv2[0],
                                                          new_uv2[1])
                if not np.allclose(uv2, new_uv2):
                    print "OOPS!"
            matches[k] = [new_idx1, new_idx2]                
                
print "Eliminating duplicates..."
# after collapsing by uv coordinate, we could be left with duplicate
# matches (mached at different scales, but same exact point.)
for i1 in proj.image_list:
    for j, matches in enumerate(i1.match_list):
        i2 = proj.image_list[j]
        tmp_dict = {}
        new_matches = []
        for k, pair in enumerate(matches):
            key = "%d-%d" % (pair[0], pair[1])
            if not key in tmp_dict:
                tmp_dict[key] = True
                new_matches.append(pair)
        i1.match_list[j] = new_matches

print "Constructing unified match structure..."
# create an initial pair-wise match list
matches_direct = []
for i, i1 in enumerate(proj.image_list):
    # print i1.name
    for j, matches in enumerate(i1.match_list):
        # print proj.image_list[j].name
        if j > i:
            for pair in matches:
                match = []
                # ned place holder
                match.append([0.0, 0.0, 0.0])
                match.append([i, pair[0]])
                match.append([j, pair[1]])
                matches_direct.append(match)

# collect/group match chains that refer to the same keypoint (warning,
# if there are bad matches this can over-constrain the problem or tie
# the pieces together too tightly/incorrectly and lead to nans.)
count = 0
done = False
while not done:
    print "Iteration:", count
    count += 1
    matches_new = []<commit_msg>Add a command line option to allow disabling of feature grouping.<commit_after>#!/usr/bin/python

import sys
sys.path.insert(0, "/usr/local/opencv-2.4.11/lib/python2.7/site-packages/")

import argparse
import commands
import cPickle as pickle
import cv2
import fnmatch
import math
import numpy as np
import os.path
from progress.bar import Bar
import scipy.spatial

sys.path.append('../lib')
import Matcher
import Pose
import ProjectMgr
import SRTM

# Rest all match point locations to their original direct
# georeferenced locations based on estimated camera pose and
# projection onto DEM earth surface

parser = argparse.ArgumentParser(description='Keypoint projection.')
parser.add_argument('--project', required=True, help='project directory')
parser.add_argument('--no-grouping', action='store_true', help='don\'t group match cyles')

args = parser.parse_args()

proj = ProjectMgr.ProjectMgr(args.project)
proj.load_image_info()
proj.load_features()
proj.undistort_keypoints()
proj.load_matches()

# setup SRTM ground interpolator
ref = proj.ned_reference_lla
sss = SRTM.NEDGround( ref, 2000, 2000, 30 )

# compute keypoint usage map
proj.compute_kp_usage()
                      
# fast way:
# 1. make a grid (i.e. 8x8) of uv coordinates covering the whole image
# 2. undistort these uv coordinates
# 3. project them into vectors
# 4. intersect them with the srtm terrain to get ned coordinates
# 5. use linearndinterpolator ... g = scipy.interpolate.LinearNDInterpolator([[0,0],[1,0],[0,1],[1,1]], [[0,4,8],[1,3,2],[2,2,-4],[4,1,0]])
#    with origin uv vs. 3d location to build a table
# 6. interpolate original uv coordinates to 3d locations
proj.fastProjectKeypointsTo3d(sss)

print "Indexing features by unique uv coordinates..."
# we typically expect many duplicated feature uv coordinates, but they
# may have different scaling or other attributes important during
# feature matching
for image in proj.image_list:
    print image.name
    # pass one, build a tmp structure of unique keypoints (by uv) and
    # the index of the first instance.
    image.kp_remap = {}
    for i, kp in enumerate(image.kp_list):
        if image.kp_used[i]:
            key = "%.3f-%.3f" % (kp.pt[0], kp.pt[1])
            if not key in image.kp_remap:
                image.kp_remap[key] = i
            # else:
            #     print "%d -> %d" % (i, image.kp_remap[key])
            #     print " ", image.coord_list[i], image.coord_list[image.kp_remap[key]]
    print " features:", len(image.kp_list)
    print " unique by uv and used:", len(image.kp_remap)

print "Collapsing keypoints with duplicate uv coordinates..."
# after feature matching we don't care about other attributes, just
# the uv coordinate.
for i1 in proj.image_list:
    for j, matches in enumerate(i1.match_list):
        i2 = proj.image_list[j]
        for k, pair in enumerate(matches):
            # print pair
            idx1 = pair[0]
            idx2 = pair[1]
            uv1 = list(i1.kp_list[idx1].pt)
            uv2 = list(i2.kp_list[idx2].pt)
            key1 = "%.3f-%.3f" % (uv1[0], uv1[1])
            key2 = "%.3f-%.3f" % (uv2[0], uv2[1])
            if j == 1 and idx2 == 1360:
                print key1, key2
            if j == 1 and idx2 == 3212:
                print key1, key2
            # print key1, key2
            new_idx1 = i1.kp_remap[key1]
            new_idx2 = i2.kp_remap[key2]
            if False and idx1 != new_idx1:
                print "1: %d -> %d" % (idx1, new_idx1)
                new_uv1 = list(i1.kp_list[new_idx1].pt)
                print "  [%.3f, %.3f] -> [%.3f, %.3f]" % (uv1[0], uv1[1],
                                                          new_uv1[0],
                                                          new_uv1[1])
                if not np.allclose(uv1, new_uv1):
                    print "OOPS!"
            if False and idx2 != new_idx2:
                print "2: %d -> %d" % (idx2, new_idx2)
                new_uv2 = list(i2.kp_list[new_idx2].pt)
                print "  [%.3f, %.3f] -> [%.3f, %.3f]" % (uv2[0], uv2[1],
                                                          new_uv2[0],
                                                          new_uv2[1])
                if not np.allclose(uv2, new_uv2):
                    print "OOPS!"
            matches[k] = [new_idx1, new_idx2]                
                
print "Eliminating duplicates..."
# after collapsing by uv coordinate, we could be left with duplicate
# matches (mached at different scales, but same exact point.)
for i1 in proj.image_list:
    for j, matches in enumerate(i1.match_list):
        i2 = proj.image_list[j]
        tmp_dict = {}
        new_matches = []
        for k, pair in enumerate(matches):
            key = "%d-%d" % (pair[0], pair[1])
            if not key in tmp_dict:
                tmp_dict[key] = True
                new_matches.append(pair)
        i1.match_list[j] = new_matches

print "Constructing unified match structure..."
# create an initial pair-wise match list
matches_direct = []
for i, i1 in enumerate(proj.image_list):
    # print i1.name
    for j, matches in enumerate(i1.match_list):
        # print proj.image_list[j].name
        if j > i:
            for pair in matches:
                match = []
                # ned place holder
                match.append([0.0, 0.0, 0.0])
                match.append([i, pair[0]])
                match.append([j, pair[1]])
                matches_direct.append(match)

# collect/group match chains that refer to the same keypoint (warning,
# if there are bad matches this can over-constrain the problem or tie
# the pieces together too tightly/incorrectly and lead to nans.)
count = 0
if args.no_grouping:
    done = True
else:
    done = False
while not done:
    print "Iteration:", count
    count += 1
    matches_new = []
{'fraction_non_alphanumeric': 0.11324860724233983, 'fraction_numerical': 0.02942200557103064, 'mean_word_length': 2.083467525496511, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6119291', 'n_tokens_mistral': 3840, 'n_tokens_neox': 3565, 'n_words': 1075}