beverley-gorry/colmap-vslamlab
0
1# Copyright (c), ETH Zurich and UNC Chapel Hill.2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are met:6#7# * Redistributions of source code must retain the above copyright8# notice, this list of conditions and the following disclaimer.9#10# * Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of15# its contributors may be used to endorse or promote products derived16# from this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28# POSSIBILITY OF SUCH DAMAGE.29 30 31# This script exports inlier matches from a COLMAP database to a text file.32 33import argparse34import os35import sqlite336 37import numpy as np38 39 40def parse_args():41 parser = argparse.ArgumentParser()42 parser.add_argument("--database_path", required=True)43 parser.add_argument("--output_path", required=True)44 parser.add_argument("--min_num_matches", type=int, default=15)45 args = parser.parse_args()46 return args47 48 49def pair_id_to_image_ids(pair_id):50 image_id2 = pair_id % 214748364751 image_id1 = (pair_id - image_id2) / 214748364752 return image_id1, image_id253 54 55def main():56 args = parse_args()57 58 connection = sqlite3.connect(args.database_path)59 cursor = connection.cursor()60 61 images = {}62 cursor.execute("SELECT image_id, camera_id, name FROM images;")63 for row in cursor:64 image_id = row[0]65 image_name = row[2]66 images[image_id] = image_name67 68 with open(os.path.join(args.output_path), "w") as fid:69 cursor.execute(70 "SELECT pair_id, data FROM two_view_geometries WHERE rows>=?;",71 (args.min_num_matches,),72 )73 for row in cursor:74 pair_id = row[0]75 inlier_matches = np.fromstring(row[1], dtype=np.uint32).reshape(76 -1, 277 )78 image_id1, image_id2 = pair_id_to_image_ids(pair_id)79 image_name1 = images[image_id1]80 image_name2 = images[image_id2]81 fid.write(82 "%s %s %d\n"83 % (image_name1, image_name2, inlier_matches.shape[0])84 )85 for i in range(inlier_matches.shape[0]):86 fid.write("%d %d\n" % tuple(inlier_matches[i]))87 88 cursor.close()89 connection.close()90 91 92if __name__ == "__main__":93 main()94 