def GetPointData(info):
    reconstruction = Info.ReadReconstructionData(info)['shots']
    reconstruction_data = []
    latlon_data = []
    frame_lst = []
    f = open(Info.GetMatchLstFileName(info), 'r')
    for line in f:
        line = line[0:-1].split('\t')
        frame_name = line[0]
        pano_name = line[1]
        try:
            [x, y, z] = optical_center(reconstruction[frame_name])
        except:
            continue
        lat = float(line[2])
        lon = float(line[3])
        frame_lst.append(line[0])
        reconstruction_data.append([x, y])
        latlon_data.append([lat, lon])
    f.close()
    reconstruction_data = np.array(reconstruction_data, dtype=np.float32)
    latlon_data = np.array(latlon_data, dtype=np.float32)

    #plt.figure()
    #plt.subplot('111')
    #plt.plot(reconstruction_data[:,0], reconstruction_data[:,1], 'o')
    #plt.show()

    return [frame_lst, reconstruction_data, latlon_data]
def RunRANSAC(ID):
    print ID
    info = Info.GetVideoInfo(ID)
    subprocess.call('rm %s' % (Info.GetGCPFileName(info)), shell=True)
    [frame_lst, reconstruct_set, latlon_set] = GetPointData(info)
    if len(reconstruct_set) == 0 or len(latlon_set) == 0:
        return
    #try:
    [M, model] = RANSAC_2D(reconstruct_set,
                           latlon_set,
                           iteration=1000,
                           tolerance=Info.Config.STEP_TEN_METER)
    print M
    #except:
    #    return
    if not M is None and not model is None:
        reconstruct = Info.ReadReconstructionData(info)['shots']
        f = open(Info.GetGCPFileName(info), 'w')
        frame_lst = sorted(reconstruct.keys())
        for index, frame in enumerate(frame_lst):
            [x, y, z] = optical_center(reconstruct[frame])
            point = np.dot(M,
                           np.transpose(np.array([x, y, 1], dtype=np.float32)))
            s = '%s\t%f\t%f\n' % (frame, point[0], point[1])
            f.write(s)
        f.close()
def RansacPointExtract(ID):
    info = Info.GetVideoInfo(ID)
    f_name = Info.GetTrackFileName(info)
    f = open(f_name, 'r')
    obj, graph_feature_data, graph_track_data = dataset.load_tracks_graph(f)  #####
    #print graph_track_data
    f.close()
    track_nodes, image_nodes = nx.algorithms.bipartite.sets(obj) ####

    buf = Info.ReadReconstructionData(info)
    image_lst = buf['shots'].keys() #####
    reconstruct_data = buf['points'] ####
    reconstruct_data_key_lst = reconstruct_data.keys() #####

    match_data = np.load(Info.GetMatchResultFileName(info)).item()
    #print match_data.keys()
    f = open(Info.GetMatchResultPointFileName(info), 'r')
    pano_data = json.load(f)
    f.close()
    #print pano_data.keys()

    new_pano_data = {}
    for index, key in enumerate(pano_data):
        loc_key = pano_data[key].keys()[0]
        pano_key = match_data[key].keys()[0]
        tmp = np.reshape(pano_data[key][loc_key], (-1,3))
        try:
            new_pano_data[key][pano_key] = tmp
        except:
            new_pano_data[key] = {}
            new_pano_data[key][pano_key] = tmp

    recontruct_point = []
    pano_point = []
    for index, key in enumerate(match_data):
        #key_tmp = key + '.jpg'
        key_tmp = key
        if key_tmp in image_lst:
            pano_key = match_data[key].keys()[0]
            for index, image_index in enumerate(match_data[key][pano_key][0]):
                if image_index in graph_feature_data[key_tmp]:
                    track_id = graph_track_data[key_tmp][str(image_index)]
                    if track_id in reconstruct_data_key_lst:
                        point1 = reconstruct_data[track_id]['coordinates']
                        point2 = new_pano_data[key_tmp][pano_key][index, :]
                        if point2[np.abs(point2) > 10000].size == 0:
                            recontruct_point.append(point1)
                            pano_point.append(point2)
    recontruct_point = np.array(recontruct_point, dtype=np.float32)
    #length = recontruct_point.shape[0]
    #recontruct_point = np.array([recontruct_point[:,0], recontruct_point[:,1], recontruct_point[:,2], np.ones([length])], dtype=np.float32)
    pano_point = np.array(pano_point, dtype = np.float32)
    #pano_point = np.array([pano_point[:,0], pano_point[:,1], pano_point[:,2], np.ones([length])], dtype=np.float32)
    np.save(Info.GetMatchResultExtractPointFileName(info), [recontruct_point, pano_point])
    print recontruct_point.shape
    print pano_point.shape
    print '%s extract point finish'%ID
Example #4
0
def RunRansac(ID):
    print ID
    info = Info.GetVideoInfo(ID)
    data = np.load(Info.GetMatchResultExtractPointFileName(info))
    #data2 = data[1]
    #data[0] = data[0][np.abs(data[1]) <= 3000]
    #data[1] = data[1][np.abs(data[1]) <= 3000]
    #data2 = data[1i]
    try:
        M, align_data = RANSAC.RANSAC_affine(data[0], data[1], 10000, 5)
    except:
        return
    if M is None:
        return
    print M
    #print np.reshape(M, [12])
    try:
        M = np.reshape(M, [12]).astype(float)
    except:
        return
    gcp = Info.ReadGCPData(info)
    data = Info.ReadReconstructionData(info)
    trajectory = {}
    for shot in data['shots']:
        [x, y, z] = optical_center(data['shots'][shot])
        trajectory[shot] = [x, y, z]
    data['gcp'] = gcp
    #print gcp
    data['transformation'] = list(M)
    #print type(M[0])
    data['trajectory'] = trajectory
    #print trajectory
    f_name = Info.Get3DRansacFileName(info)
    f = open(f_name, 'w')
    f.write(json.dumps(data, indent=4))
    f.close()
    print '%s ransac finish' % ID