$search
00001 00005 #include "keyframes.hpp" 00006 00007 bool getValidLocalMaxima(Mat& scores, unsigned int last_iii, unsigned int last_jjj, unsigned int& opt_iii, unsigned int& opt_jjj) { 00008 00009 // First, find the cell/s that could be optimized now 00010 00011 if (last_iii == 0) { 00012 return false; 00013 } 00014 00015 bool foundMaxima = false; 00016 00017 00018 // make sure you never select it if it's value is zero - or do that check in the main prog? 00019 00020 00021 // cell to the above left is a candidate 00022 double candidateScore = scores.at<double>(last_iii-1, last_jjj-1); 00023 00024 foundMaxima = true; 00025 for (unsigned int aaa = std::max(((int) last_iii)-2,0); aaa < std::min(((int) last_iii),scores.cols); aaa++) { 00026 for (unsigned int bbb = std::max(((int) last_jjj)-2,0); bbb < std::min(((int) last_jjj),scores.rows); bbb++) { 00027 00028 if (scores.at<double>(aaa,bbb) > candidateScore) { 00029 foundMaxima = false; 00030 } 00031 00032 } 00033 } 00034 00035 double bestCandidateScore = 0.00; 00036 00037 if (foundMaxima == true) { 00038 bestCandidateScore = candidateScore; 00039 opt_iii = last_iii-1; 00040 opt_jjj = last_jjj-1; 00041 } 00042 00043 if (last_jjj == (scores.cols-1)) { 00044 foundMaxima = true; 00045 00046 // cell to the direct above is a candidate 00047 candidateScore = scores.at<double>(last_iii-1, last_jjj); 00048 00049 for (unsigned int aaa = std::max(((int) last_iii)-2,0); aaa < std::min(((int) last_iii),scores.cols); aaa++) { 00050 for (unsigned int bbb = std::max(((int) last_jjj)-1,0); bbb < std::min(((int) last_jjj)+1,scores.rows); bbb++) { 00051 00052 if (scores.at<double>(aaa,bbb) > candidateScore) { 00053 foundMaxima = false; 00054 } 00055 00056 } 00057 } 00058 00059 if (foundMaxima == true) { 00060 if (candidateScore > bestCandidateScore) { 00061 bestCandidateScore = candidateScore; 00062 opt_iii = last_iii-1; 00063 opt_jjj = last_jjj; 00064 } 00065 00066 } 00067 00068 } 00069 00070 if ((last_jjj == (scores.cols-1)) && (last_iii == (scores.rows-2))) { 00071 foundMaxima = true; 00072 00073 // current cell is a candidate 00074 candidateScore = scores.at<double>(last_iii, last_jjj); 00075 00076 for (unsigned int aaa = std::max(((int) last_iii)-1,0); aaa < std::min(((int) last_iii)+1,scores.cols); aaa++) { 00077 for (unsigned int bbb = std::max(((int) last_jjj)-1,0); bbb < std::min(((int) last_jjj)+1,scores.rows); bbb++) { 00078 00079 if (scores.at<double>(aaa,bbb) > candidateScore) { 00080 foundMaxima = false; 00081 } 00082 00083 } 00084 } 00085 00086 if (foundMaxima == true) { 00087 if (candidateScore > bestCandidateScore) { 00088 bestCandidateScore = candidateScore; 00089 opt_iii = last_iii; 00090 opt_jjj = last_jjj; 00091 } 00092 00093 } 00094 } 00095 00096 //printf("%s << opt_iii = %d; opt_jjj = %d\n", __FUNCTION__, opt_iii, opt_jjj); 00097 00098 if (bestCandidateScore > 0.00) { 00099 return true; 00100 } else { 00101 return false; 00102 } 00103 00104 } 00105 00106 void keyframeStore::findStrongConnections(int idx, vector<unsigned int>& cIndices) { 00107 00108 bool connectionsAllFound = false; 00109 00110 vector<unsigned int> kIndices; 00111 kIndices.push_back(idx); 00112 00113 //printf("%s << Entered. connections.size() = %d\n", __FUNCTION__, connections.size()); 00114 00115 while (!connectionsAllFound) { 00116 00117 connectionsAllFound = true; 00118 00119 for (unsigned int iii = 0; iii < connections.size(); iii++) { 00120 00121 //printf("%s << Connection between (%d) and (%d) [searching for %d]\n", __FUNCTION__, connections.at(iii).idx1, connections.at(iii).idx2, idx); 00122 00123 bool connectionAlreadyAdded = false; 00124 00125 for (unsigned int jjj = 0; jjj < cIndices.size(); jjj++) { 00126 00127 if (cIndices.at(jjj) == iii) { 00128 connectionAlreadyAdded = true; 00129 } 00130 00131 } 00132 00133 if (connectionAlreadyAdded == true) { 00134 continue; 00135 } 00136 00137 for (unsigned int jjj = 0; jjj < kIndices.size(); jjj++) { 00138 if ((connections.at(iii).idx1 == kIndices.at(jjj)) || (connections.at(iii).idx2 == kIndices.at(jjj))) { 00139 00140 bool connection_index_added = false; 00141 for (unsigned int kkk = 0; kkk < cIndices.size(); kkk++) { 00142 if (iii == cIndices.at(kkk)) { 00143 connection_index_added = true; 00144 } 00145 } 00146 00147 if (connection_index_added) { 00148 break; 00149 } 00150 00151 cIndices.push_back(iii); 00152 connectionsAllFound = false; 00153 00154 // Then only add these keyframe indices if they haven't been added already.. 00155 00156 bool alreadyAdded1 = false, alreadyAdded2 = false; 00157 for (unsigned int kkk = 0; kkk < kIndices.size(); kkk++) { 00158 00159 if (kIndices.at(kkk) == connections.at(iii).idx1) { 00160 alreadyAdded1 = true; 00161 } 00162 00163 if (kIndices.at(kkk) == connections.at(iii).idx2) { 00164 alreadyAdded2 = true; 00165 } 00166 00167 } 00168 00169 if (!alreadyAdded1) { 00170 kIndices.push_back(connections.at(iii).idx1); 00171 } 00172 00173 if (!alreadyAdded2) { 00174 kIndices.push_back(connections.at(iii).idx2); 00175 } 00176 00177 } 00178 } 00179 00180 00181 } 00182 } 00183 00184 sort(kIndices.begin(), kIndices.end()); 00185 00186 cIndices.clear(); 00187 00188 for (unsigned int iii = 0; iii < connections.size(); iii++) { 00189 00190 for (unsigned int jjj = 0; jjj < kIndices.size(); jjj++) { 00191 00192 if (connections.at(iii).idx1 == kIndices.at(jjj)) { 00193 cIndices.push_back(iii); 00194 break; 00195 } 00196 00197 if (connections.at(iii).idx2 == kIndices.at(jjj)) { 00198 cIndices.push_back(iii); 00199 break; 00200 } 00201 00202 } 00203 } 00204 00205 } 00206 00207 void keyframe::detectKeypoints(Ptr<FeatureDetector>& detector) { 00208 detector -> detect(im, keypoints); 00209 sortKeypoints(keypoints, MAXIMUM_FEATURES_PER_FRAME); 00210 } 00211 00212 void keyframe::extractDescriptors(Ptr<DescriptorExtractor>& extractor) { 00213 extractor->compute(im, keypoints, descriptors); 00214 } 00215 00216 keyframeStore::keyframeStore() { 00217 count = 0; 00218 } 00219 00220 bool keyframeStore::getBestPair(int& idx1, int& idx2) { 00221 00222 return true; 00223 00224 } 00225 00226 void keyframeStore::addKeyframe(int idx, Mat& image) { 00227 00228 keyframe newFrame; 00229 00230 newFrame.idx = idx; 00231 image.copyTo(newFrame.im); 00232 00233 //newFrame.pose = Mat::eye(4, 4, CV_64FC1); 00234 newFrame.poseDetermined = false; 00235 00236 keyframes.push_back(newFrame); 00237 00238 00239 00240 count++; 00241 00242 } 00243 00244 void keyframeStore::addConnection(int idx1, int idx2, int type, Mat rel) { 00245 00246 connection cnct; 00247 cnct.idx1 = idx1; 00248 cnct.idx2 = idx2; 00249 cnct.type = type; 00250 rel.copyTo(cnct.relation); 00251 cnct.processed = false; 00252 00253 connections.push_back(cnct); 00254 00255 } 00256 00257 void keyframeStore::findMatches() { 00258 00259 00260 for (unsigned int iii = 0; iii < keyframes.size()-1; iii++) { 00261 00262 for (unsigned int jjj = iii+1; jjj < keyframes.size(); jjj++) { 00263 00264 bool connectionExists = false; 00265 int idx; 00266 00267 for (unsigned int kkk = 0; kkk < connections.size(); kkk++) { 00268 00269 if ((connections.at(kkk).idx1 == iii) && (connections.at(kkk).idx2 == jjj)) { 00270 00271 idx = kkk; 00272 connectionExists = true; 00273 continue; 00274 00275 } 00276 00277 } 00278 00279 if (!connectionExists) { 00280 00281 connection cnct; 00282 cnct.idx1 = iii; 00283 cnct.idx2 = jjj; 00284 cnct.type = KF_CONNECTION_WEAK; 00285 00286 createMatchingMatrix(cnct.matchingMatrix, keyframes.at(iii).descriptors, keyframes.at(jjj).descriptors); 00287 00288 constrainMatchingMatrix(cnct.matchingMatrix, keyframes.at(iii).keypoints, keyframes.at(jjj).keypoints, MATCHING_DIST_CONSTRAINT, MATCHING_SIZE_CONSTRAINT); 00289 00290 twoWayPriorityMatching(cnct.matchingMatrix, cnct.matches1to2); 00291 00292 sortMatches(cnct.matches1to2); 00293 00294 // 0.5 for ratio prioritization 00295 filterMatches(cnct.matches1to2, 0.5); 00296 00297 connections.push_back(cnct); 00298 00299 } else if (connections.at(idx).type == KF_CONNECTION_WEAK) { 00300 00301 createMatchingMatrix(connections.at(idx).matchingMatrix, keyframes.at(iii).descriptors, keyframes.at(jjj).descriptors); 00302 00303 constrainMatchingMatrix(connections.at(idx).matchingMatrix, keyframes.at(iii).keypoints, keyframes.at(jjj).keypoints, MATCHING_DIST_CONSTRAINT, MATCHING_SIZE_CONSTRAINT); 00304 00305 twoWayPriorityMatching(connections.at(idx).matchingMatrix, connections.at(idx).matches1to2); 00306 00307 sortMatches(connections.at(idx).matches1to2); 00308 00309 // 0.5 for ratio prioritization 00310 filterMatches(connections.at(idx).matches1to2, 0.5); 00311 00312 } 00313 00314 } 00315 00316 } 00317 00318 } 00319