1 // This is brl/bseg/bapl/bapl_connectivity.h
2 #ifndef bapl_connectivity_h_
3 #define bapl_connectivity_h_
4 //:
5 // \file
6 // \brief A class to build image connectivity graph, see Snavely, Seitz, Szeliski - Modeling the World from Internet Photo Collections.
7 // \author Ozge C. Ozcanli, (ozge@lems.brown.edu)
8 // \date Sep 21, 2010
9 //
10 // \verbatim
11 //   none
12 // \endverbatim
13 
14 #include <vector>
15 #include <iostream>
16 #include <iosfwd>
17 #include <vbl/vbl_ref_count.h>
18 #include <bapl/bapl_keypoint_set_sptr.h>
19 #include <bapl/bapl_keypoint_sptr.h>
20 #include <vsl/vsl_binary_io.h>
21 #ifdef _MSC_VER
22 #  include <vcl_msvc_warnings.h>
23 #endif
24 
25 typedef std::pair< int, bapl_keypoint_sptr > bapl_image_key;
26 typedef std::vector< bapl_image_key > bapl_image_key_vector;
27 
28 //: class to hold point tracks (2d correspondences in each image for a single 3d point)
29 class bapl_track_data : public vbl_ref_count
30 {
31  public:
bapl_track_data(bapl_image_key_vector & vec)32   bapl_track_data(bapl_image_key_vector& vec) : views_(vec) {}
33 
34   bapl_image_key_vector views_;
35 };
36 
37 //: For sorting keypoint_match_sets
38 bool second_less( const bapl_keypoint_match_set_sptr& left_set, const bapl_keypoint_match_set_sptr& right_set);
39 
40 //: a type to hold a list of matches to other images for an image, initially during construction connectivities of each image will be empty
41 //  Stores all the connectivities of this image to the other images in a set of images
42 typedef std::vector<bapl_keypoint_match_set_sptr> bapl_conn;
43 
44 //: Class to hold connectivities of each image in a list
45 //  If an image does not have a conn to any other image then its bapl_conn vector will be empty
46 class bapl_conn_table : public vbl_ref_count
47 {
48  public:
49   //: Constructor
bapl_conn_table(int n_images)50   bapl_conn_table(int n_images) : conns_(n_images), img_data_(n_images), img_data_key_flags_(n_images, std::vector<bool>()) {}  // initialize with an empty connectivity
51   //: add this match set symmetrically, i.e. into the list of img id 1 as well img id 2, while adding for img id 2, reverse the keypoint pairs
52   bool add_sym(const bapl_keypoint_match_set_sptr& set);
53   bool add(const bapl_keypoint_match_set_sptr& set);
54 
55   //: add keypoints for the given image with id i
add_image_data(int i,bapl_keypoint_set_sptr data)56   void add_image_data(int i, bapl_keypoint_set_sptr data) { img_data_[i] = data; }
57 
58   //: check if vector of image id1 already contains a match set for image id2
59   bool contains(int id1, int id2);
60 
61   //: make the table symmetric, only necessary if add() method is used as opposed to add_sym()
62   void make_symmetric();
63 
64   void print_table();
65   void print_table_with_matches();
66 
67   //: return the number of neighbors for image with id i
68   unsigned get_number_of_neighbors(unsigned i);
get_neighbors(unsigned i)69   bapl_conn& get_neighbors(unsigned i) { return conns_[i]; }
70 
71   //: compute a set of tracks, each corresponding to a separate 3d point
72   //  Assumes a symmetric connectivity table
73   bool compute_tracks(std::vector<bapl_track_data>& tracks, int new_image_start = 0);
74 
75   std::vector<bapl_conn> conns_;
76   std::vector<bapl_keypoint_set_sptr> img_data_;
77   std::vector<std::vector<bool> > img_data_key_flags_;
78 };
79 
80 //: Print a summary of the connectivity data to a stream
81 std::ostream& operator<< (std::ostream& os, bapl_conn_table const & t);
82 
83 //: Print tracks as correspondences in BWM_VIDEO_SITE format for visualization
84 void print_tracks(std::ostream& os, std::vector<bapl_track_data>& tracks, int img_width = 0, int img_height = 0);
85 
86 // Binary io, NOT IMPLEMENTED, signatures defined to use bapl_keypoint_set as a brdb_value
87 void vsl_b_write(vsl_b_ostream & os, bapl_conn_table const &ph);
88 void vsl_b_read(vsl_b_istream & is, bapl_conn_table &ph);
89 void vsl_b_read(vsl_b_istream& is, bapl_conn_table* ph);
90 void vsl_b_write(vsl_b_ostream& os, const bapl_conn_table* &ph);
91 
92 #endif // bapl_connectivity_h_
93