MCPcopy
hub / github.com/cvg/Hierarchical-Localization / visualize_loc_from_log

Function visualize_loc_from_log

hloc/visualization.py:79–139  ·  view source on GitHub ↗
(image_dir, query_name, loc, reconstruction=None,
                           db_image_dir=None, top_k_db=2, dpi=75)

Source from the content-addressed store, hash-verified

77
78
79def visualize_loc_from_log(image_dir, query_name, loc, reconstruction=None,
80 db_image_dir=None, top_k_db=2, dpi=75):
81
82 q_image = read_image(image_dir / query_name)
83 if loc.get('covisibility_clustering', False):
84 # select the first, largest cluster if the localization failed
85 loc = loc['log_clusters'][loc['best_cluster'] or 0]
86
87 inliers = np.array(loc['PnP_ret']['inliers'])
88 mkp_q = loc['keypoints_query']
89 n = len(loc['db'])
90 if reconstruction is not None:
91 # for each pair of query keypoint and its matched 3D point,
92 # we need to find its corresponding keypoint in each database image
93 # that observes it. We also count the number of inliers in each.
94 kp_idxs, kp_to_3D_to_db = loc['keypoint_index_to_db']
95 counts = np.zeros(n)
96 dbs_kp_q_db = [[] for _ in range(n)]
97 inliers_dbs = [[] for _ in range(n)]
98 for i, (inl, (p3D_id, db_idxs)) in enumerate(zip(inliers,
99 kp_to_3D_to_db)):
100 track = reconstruction.points3D[p3D_id].track
101 track = {el.image_id: el.point2D_idx for el in track.elements}
102 for db_idx in db_idxs:
103 counts[db_idx] += inl
104 kp_db = track[loc['db'][db_idx]]
105 dbs_kp_q_db[db_idx].append((i, kp_db))
106 inliers_dbs[db_idx].append(inl)
107 else:
108 # for inloc the database keypoints are already in the logs
109 assert 'keypoints_db' in loc
110 assert 'indices_db' in loc
111 counts = np.array([
112 np.sum(loc['indices_db'][inliers] == i) for i in range(n)])
113
114 # display the database images with the most inlier matches
115 db_sort = np.argsort(-counts)
116 for db_idx in db_sort[:top_k_db]:
117 if reconstruction is not None:
118 db = reconstruction.images[loc['db'][db_idx]]
119 db_name = db.name
120 db_kp_q_db = np.array(dbs_kp_q_db[db_idx])
121 kp_q = mkp_q[db_kp_q_db[:, 0]]
122 kp_db = np.array([db.points2D[i].xy for i in db_kp_q_db[:, 1]])
123 inliers_db = inliers_dbs[db_idx]
124 else:
125 db_name = loc['db'][db_idx]
126 kp_q = mkp_q[loc['indices_db'] == db_idx]
127 kp_db = loc['keypoints_db'][loc['indices_db'] == db_idx]
128 inliers_db = inliers[loc['indices_db'] == db_idx]
129
130 db_image = read_image((db_image_dir or image_dir) / db_name)
131 color = cm_RdGn(inliers_db).tolist()
132 text = f'inliers: {sum(inliers_db)}/{len(inliers_db)}'
133
134 plot_images([q_image, db_image], dpi=dpi)
135 plot_matches(kp_q, kp_db, color, a=0.1)
136 add_text(0, text)

Callers 1

visualize_locFunction · 0.85

Calls 5

read_imageFunction · 0.85
cm_RdGnFunction · 0.85
plot_imagesFunction · 0.85
plot_matchesFunction · 0.85
add_textFunction · 0.85

Tested by

no test coverage detected