|
8 | 8 | namespace stella_vslam { |
9 | 9 | namespace match { |
10 | 10 |
|
| 11 | +unsigned int bow_tree::match_for_triangulation(const std::shared_ptr<data::keyframe>& keyfrm_1, |
| 12 | + const std::shared_ptr<data::keyframe>& keyfrm_2, |
| 13 | + const Mat33_t& E_12, |
| 14 | + std::vector<std::pair<unsigned int, unsigned int>>& matched_idx_pairs, |
| 15 | + const float residual_rad_thr) const { |
| 16 | + unsigned int num_matches = 0; |
| 17 | + |
| 18 | + // Project the center of keyframe 1 to keyframe 2 |
| 19 | + // to acquire the epipole coordinates of the candidate keyframe |
| 20 | + const Vec3_t cam_center_1 = keyfrm_1->get_trans_wc(); |
| 21 | + const Mat33_t rot_2w = keyfrm_2->get_rot_cw(); |
| 22 | + const Vec3_t trans_2w = keyfrm_2->get_trans_cw(); |
| 23 | + Vec3_t epiplane_in_keyfrm_2; |
| 24 | + const bool valid_epiplane = keyfrm_2->camera_->reproject_to_bearing(rot_2w, trans_2w, cam_center_1, epiplane_in_keyfrm_2); |
| 25 | + |
| 26 | + // Acquire the 3D point information of the keframes |
| 27 | + const auto assoc_lms_in_keyfrm_1 = keyfrm_1->get_landmarks(); |
| 28 | + const auto assoc_lms_in_keyfrm_2 = keyfrm_2->get_landmarks(); |
| 29 | + |
| 30 | + // Save the matching information |
| 31 | + // Discard the already matched keypoints in keyframe 2 |
| 32 | + // to acquire a unique association to each keypoint in keyframe 1 |
| 33 | + std::vector<bool> is_already_matched_in_keyfrm_2(keyfrm_2->frm_obs_.undist_keypts_.size(), false); |
| 34 | + // Save the keypoint idx in keyframe 2 which is already associated to the keypoint idx in keyframe 1 |
| 35 | + std::vector<int> matched_indices_2_in_keyfrm_1(keyfrm_1->frm_obs_.undist_keypts_.size(), -1); |
| 36 | + |
| 37 | + data::bow_feature_vector::const_iterator itr_1 = keyfrm_1->bow_feat_vec_.begin(); |
| 38 | + data::bow_feature_vector::const_iterator itr_2 = keyfrm_2->bow_feat_vec_.begin(); |
| 39 | + const data::bow_feature_vector::const_iterator itr_1_end = keyfrm_1->bow_feat_vec_.end(); |
| 40 | + const data::bow_feature_vector::const_iterator itr_2_end = keyfrm_2->bow_feat_vec_.end(); |
| 41 | + |
| 42 | + while (itr_1 != itr_1_end && itr_2 != itr_2_end) { |
| 43 | + // Check if the node numbers of BoW tree match |
| 44 | + if (itr_1->first == itr_2->first) { |
| 45 | + // If the node numbers of BoW tree match, |
| 46 | + // Check in practice if matches exist between keyframes |
| 47 | + const auto& keyfrm_1_indices = itr_1->second; |
| 48 | + const auto& keyfrm_2_indices = itr_2->second; |
| 49 | + |
| 50 | + for (const auto idx_1 : keyfrm_1_indices) { |
| 51 | + const auto& lm_1 = assoc_lms_in_keyfrm_1.at(idx_1); |
| 52 | + // Ignore if the keypoint of keyframe is associated any 3D points |
| 53 | + if (lm_1) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // Check if it's a stereo keypoint or not |
| 58 | + const bool is_stereo_keypt_1 = !keyfrm_1->frm_obs_.stereo_x_right_.empty() && 0 <= keyfrm_1->frm_obs_.stereo_x_right_.at(idx_1); |
| 59 | + |
| 60 | + // Acquire the keypoints and ORB feature vectors |
| 61 | + const auto& keypt_1 = keyfrm_1->frm_obs_.undist_keypts_.at(idx_1); |
| 62 | + const Vec3_t& bearing_1 = keyfrm_1->frm_obs_.bearings_.at(idx_1); |
| 63 | + const auto& desc_1 = keyfrm_1->frm_obs_.descriptors_.row(idx_1); |
| 64 | + |
| 65 | + // Find a keypoint in keyframe 2 that has the minimum hamming distance |
| 66 | + unsigned int best_hamm_dist = HAMMING_DIST_THR_LOW; |
| 67 | + int best_idx_2 = -1; |
| 68 | + unsigned int second_best_hamm_dist = MAX_HAMMING_DIST; |
| 69 | + |
| 70 | + for (const auto idx_2 : keyfrm_2_indices) { |
| 71 | + // Ignore if the keypoint is associated any 3D points |
| 72 | + // (because this function is used for triangulation) |
| 73 | + const auto& lm_2 = assoc_lms_in_keyfrm_2.at(idx_2); |
| 74 | + if (lm_2) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // Ignore if matches are already aquired |
| 79 | + if (is_already_matched_in_keyfrm_2.at(idx_2)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if (check_orientation_ && std::abs(util::angle::diff(keypt_1.angle, keyfrm_2->frm_obs_.undist_keypts_.at(idx_2).angle)) > 30.0) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // Check if it's a stereo keypoint or not |
| 88 | + const bool is_stereo_keypt_2 = !keyfrm_2->frm_obs_.stereo_x_right_.empty() && 0 <= keyfrm_2->frm_obs_.stereo_x_right_.at(idx_2); |
| 89 | + |
| 90 | + // Acquire the keypoints and ORB feature vectors |
| 91 | + const Vec3_t& bearing_2 = keyfrm_2->frm_obs_.bearings_.at(idx_2); |
| 92 | + const auto& desc_2 = keyfrm_2->frm_obs_.descriptors_.row(idx_2); |
| 93 | + |
| 94 | + // Compute the distance |
| 95 | + const auto hamm_dist = compute_descriptor_distance_32(desc_1, desc_2); |
| 96 | + |
| 97 | + if (HAMMING_DIST_THR_LOW < hamm_dist || best_hamm_dist < hamm_dist) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (valid_epiplane && !is_stereo_keypt_1 && !is_stereo_keypt_2) { |
| 102 | + // Do not use any keypoints near the epipole if both are not stereo keypoints |
| 103 | + const auto cos_dist = epiplane_in_keyfrm_2.dot(bearing_2); |
| 104 | + // The threshold of the minimum angle formed by the epipole and the bearing vector is 3.0 degree |
| 105 | + constexpr double cos_dist_thr = 0.99862953475; |
| 106 | + |
| 107 | + // Do not allow to match if the formed angle is narrower that the threshold value |
| 108 | + if (cos_dist_thr < cos_dist) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Check consistency in Matrix E |
| 114 | + const bool is_inlier = check_epipolar_constraint(bearing_1, bearing_2, E_12, |
| 115 | + keyfrm_1->orb_params_->scale_factors_.at(keypt_1.octave), |
| 116 | + residual_rad_thr); |
| 117 | + if (is_inlier) { |
| 118 | + if (hamm_dist < best_hamm_dist) { |
| 119 | + second_best_hamm_dist = best_hamm_dist; |
| 120 | + best_hamm_dist = hamm_dist; |
| 121 | + best_idx_2 = idx_2; |
| 122 | + } |
| 123 | + else if (hamm_dist < second_best_hamm_dist) { |
| 124 | + second_best_hamm_dist = hamm_dist; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (best_idx_2 < 0) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + // Ratio test |
| 134 | + if (lowe_ratio_ * second_best_hamm_dist < static_cast<float>(best_hamm_dist)) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + is_already_matched_in_keyfrm_2.at(best_idx_2) = true; |
| 139 | + matched_indices_2_in_keyfrm_1.at(idx_1) = best_idx_2; |
| 140 | + ++num_matches; |
| 141 | + } |
| 142 | + |
| 143 | + ++itr_1; |
| 144 | + ++itr_2; |
| 145 | + } |
| 146 | + else if (itr_1->first < itr_2->first) { |
| 147 | + // Since the node number of keyframe 1 is smaller, increment the iterator until the node numbers match |
| 148 | + itr_1 = keyfrm_1->bow_feat_vec_.lower_bound(itr_2->first); |
| 149 | + } |
| 150 | + else { |
| 151 | + // Since the node number of keyframe 2 is smaller, increment the iterator until the node numbers match |
| 152 | + itr_2 = keyfrm_2->bow_feat_vec_.lower_bound(itr_1->first); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + matched_idx_pairs.clear(); |
| 157 | + matched_idx_pairs.reserve(num_matches); |
| 158 | + |
| 159 | + for (unsigned int idx_1 = 0; idx_1 < matched_indices_2_in_keyfrm_1.size(); ++idx_1) { |
| 160 | + if (matched_indices_2_in_keyfrm_1.at(idx_1) < 0) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + matched_idx_pairs.emplace_back(std::make_pair(idx_1, matched_indices_2_in_keyfrm_1.at(idx_1))); |
| 164 | + } |
| 165 | + |
| 166 | + return num_matches; |
| 167 | +} |
| 168 | + |
11 | 169 | unsigned int bow_tree::match_frame_and_keyframe(const std::shared_ptr<data::keyframe>& keyfrm, data::frame& frm, std::vector<std::shared_ptr<data::landmark>>& matched_lms_in_frm) const { |
12 | 170 | unsigned int num_matches = 0; |
13 | 171 |
|
|
0 commit comments