6 cv::Mat cvMat = cv::Mat(4, 4, CV_32F);
13 cvMat.at<
float>(0, 0) = slMat.
m(0);
14 cvMat.at<
float>(1, 0) = slMat.
m(1);
15 cvMat.at<
float>(2, 0) = slMat.
m(2);
16 cvMat.at<
float>(3, 0) = slMat.
m(3);
18 cvMat.at<
float>(0, 1) = slMat.
m(4);
19 cvMat.at<
float>(1, 1) = slMat.
m(5);
20 cvMat.at<
float>(2, 1) = slMat.
m(6);
21 cvMat.at<
float>(3, 1) = slMat.
m(7);
23 cvMat.at<
float>(0, 2) = slMat.
m(8);
24 cvMat.at<
float>(1, 2) = slMat.
m(9);
25 cvMat.at<
float>(2, 2) = slMat.
m(10);
26 cvMat.at<
float>(3, 2) = slMat.
m(11);
28 cvMat.at<
float>(0, 3) = slMat.
m(12);
29 cvMat.at<
float>(1, 3) = slMat.
m(13);
30 cvMat.at<
float>(2, 3) = slMat.
m(14);
31 cvMat.at<
float>(3, 3) = slMat.
m(15);
45 cvMat.at<
float>(0, 0), cvMat.at<
float>(0, 1), cvMat.at<
float>(0, 2), cvMat.at<
float>(0, 3),
46 cvMat.at<
float>(1, 0), cvMat.at<
float>(1, 1), cvMat.at<
float>(1, 2), cvMat.at<
float>(1, 3),
47 cvMat.at<
float>(2, 0), cvMat.at<
float>(2, 1), cvMat.at<
float>(2, 2), cvMat.at<
float>(2, 3),
48 cvMat.at<
float>(3, 0), cvMat.at<
float>(3, 1), cvMat.at<
float>(3, 2), cvMat.at<
float>(3, 3));
55 std::map<
WAIKeyFrame*, std::map<size_t, size_t>>& KFmatching)
57 for (
int i = 0; i < kfs.size(); ++i)
66 std::map<size_t, size_t> matching;
69 for (
int j = 0; j < mps.size(); j++)
71 if (mps[j] !=
nullptr)
73 matching.insert(std::pair<size_t, size_t>(j,
id));
77 KFmatching.insert(std::pair<
WAIKeyFrame*, std::map<size_t, size_t>>(kf, matching));
82 std::map<
WAIKeyFrame*, std::map<size_t, size_t>>& KFmatching,
90 for (
int i = 0; i < kfs.size(); ++i)
100 fs <<
"id" << (int)kf->
mnId;
104 fs <<
"parentId" << -1;
108 if (loopEdges.size())
110 std::vector<int> loopEdgeIds;
111 for (
auto loopEdgeKf : loopEdges)
113 loopEdgeIds.push_back(loopEdgeKf->mnId);
115 fs <<
"loopEdges" << loopEdgeIds;
121 if (KFmatching.size() > 0)
124 const std::map<size_t, size_t>& matching = KFmatching[kf];
125 descriptors.create((
int)matching.size(), 32, CV_8U);
126 std::vector<cv::KeyPoint> keypoints(matching.size());
127 for (
int j = 0; j < kf->
mvKeysUn.size(); j++)
129 auto it = matching.find(j);
130 if (it != matching.end())
132 kf->
mDescriptors.row(j).copyTo(descriptors.row(it->second));
133 keypoints[it->second] = kf->
mvKeysUn[j];
136 fs <<
"featureDescriptors" << descriptors;
137 fs <<
"keyPtsUndist" << keypoints;
142 fs <<
"keyPtsUndist" << kf->
mvKeysUn;
148 std::vector<int> wordsId;
149 std::vector<float> tfIdf;
152 wordsId.push_back(it->first);
153 tfIdf.push_back(it->second);
156 fs <<
"BowVectorWordsId" << wordsId;
157 fs <<
"TfIdf" << tfIdf;
166 fs <<
"nMinX" << kf->
mnMinX;
167 fs <<
"nMinY" << kf->
mnMinY;
168 fs <<
"nMaxX" << kf->
mnMaxX;
169 fs <<
"nMaxY" << kf->
mnMaxY;
172 std::vector<int> bestCovisibleKeyFrameIds;
173 std::vector<int> bestCovisibleWeights;
177 if (covisible->isBad())
182 bestCovisibleKeyFrameIds.push_back(covisible->mnId);
183 bestCovisibleWeights.push_back(weight);
187 fs <<
"bestCovisibleKeyFrameIds" << bestCovisibleKeyFrameIds;
188 fs <<
"bestCovisibleWeights" << bestCovisibleWeights;
199 std::stringstream ss;
200 ss << imgDir <<
"kf" << (int)kf->
mnId <<
".jpg";
202 cv::cvtColor(kf->
imgGray, imgColor, cv::COLOR_GRAY2BGR);
203 cv::imwrite(ss.str(), imgColor);
214 std::map<
WAIKeyFrame*, std::map<size_t, size_t>>& KFmatching,
220 for (
int i = 0; i < mpts.size(); ++i)
229 fs <<
"id" << (int)mpt->
mnId;
234 vector<int> observingKfIds;
235 vector<int> corrKpIndices;
237 if (!KFmatching.empty())
239 for (
auto it : observations)
242 size_t kpIdx = it.second;
246 if (KFmatching.find(kf) == KFmatching.end())
248 std::cout <<
"observation not found in kfmatching" << std::endl;
252 const std::map<size_t, size_t>& matching = KFmatching[kf];
253 auto mit = matching.find(kpIdx);
254 if (mit != matching.end())
256 observingKfIds.push_back(kf->
mnId);
257 corrKpIndices.push_back(mit->second);
263 for (
auto it : observations)
265 if (!it.first->isBad())
267 observingKfIds.push_back(it.first->mnId);
268 corrKpIndices.push_back(it.second);
273 fs <<
"observingKfIds" << observingKfIds;
274 fs <<
"corrKpIndices" << corrKpIndices;
278 fs <<
"mNormalVector" << mpt->
GetNormal();
281 fs <<
"refKfId" << (int)mpt->
refKf()->
mnId;
289 std::string filename,
295 std::map<WAIKeyFrame*, std::map<size_t, size_t>> KFmatching;
303 cv::FileStorage fs(filename, cv::FileStorage::WRITE);
313 std::cout <<
"slOm: " << slOm.
toString() << std::endl;
315 std::cout <<
"cvOM: " << cvOm << std::endl;
316 fs <<
"mapNodeOm" << cvOm;
329 std::string filename,
334 std::map<WAIKeyFrame*, std::map<size_t, size_t>> KFmatching;
342 cv::FileStorage fs(filename, cv::FileStorage::WRITE);
352 std::cout <<
"slOm: " << slOm.
toString() << std::endl;
354 std::cout <<
"cvOM: " << cvOm << std::endl;
355 fs <<
"mapNodeOm" << cvOm;
368 std::vector<uint8_t> result;
371 cv::Mat continuousMat = mat.clone();
374 result.assign(continuousMat.data, continuousMat.data + continuousMat.total() * continuousMat.elemSize());
382 fwrite(vec.data(),
sizeof(T), vec.size(), f);
394 std::string filename,
400 std::map<WAIKeyFrame*, std::map<size_t, size_t>> KFmatching;
407 FILE* f = fopen(filename.c_str(),
"wb");
416 for (
int i = 0; i < kfs.size(); ++i)
427 for (
int i = 0; i < mpts.size(); ++i)
444 std::vector<uchar> omVec;
454 for (
int i = 0; i < kfs.size(); ++i)
464 kfInfo.
id = (int32_t)kf->
mnId;
478 if (KFmatching.size() > 0)
480 const std::map<size_t, size_t>& matching = KFmatching[kf];
481 descriptors.create((
int)matching.size(), 32, CV_8U);
482 keyPoints.resize(matching.size());
483 for (
int j = 0; j < kf->
mvKeysUn.size(); j++)
485 auto it = matching.find(j);
486 if (it != matching.end())
488 kf->
mDescriptors.row(j).copyTo(descriptors.row(it->second));
489 keyPoints[it->second] = kf->
mvKeysUn[j];
499 kfInfo.
kpCount = keyPoints.size();
508 std::vector<int32_t> loopEdgeIds;
509 if (loopEdges.size())
513 loopEdgeIds.push_back(loopEdgeKf->mnId);
518 std::vector<int32_t> wordsId;
519 std::vector<float> tfIdf;
527 wordsId.push_back(it->first);
528 tfIdf.push_back(it->second);
534 std::vector<int32_t> bestCovisibleKeyFrameIds;
535 std::vector<int32_t> bestCovisibleWeights;
539 if (covisible->isBad())
544 bestCovisibleKeyFrameIds.push_back(covisible->mnId);
545 bestCovisibleWeights.push_back(weight);
573 std::stringstream ss;
574 ss << imgDir <<
"kf" << (int)kf->
mnId <<
".jpg";
576 cv::cvtColor(kf->
imgGray, imgColor, cv::COLOR_GRAY2BGR);
577 cv::imwrite(ss.str(), imgColor);
586 for (
int i = 0; i < mpts.size(); ++i)
594 mpInfo.
id = (int32_t)mpt->
mnId;
599 vector<int32_t> observingKfIds;
600 vector<int32_t> corrKpIndices;
601 if (!KFmatching.empty())
603 for (std::pair<WAIKeyFrame* const, size_t> it : observations)
606 size_t kpIdx = it.second;
610 if (KFmatching.find(kf) == KFmatching.end())
612 std::cout <<
"observation not found in kfmatching" << std::endl;
616 const std::map<size_t, size_t>& matching = KFmatching[kf];
617 auto mit = matching.find(kpIdx);
618 if (mit != matching.end())
620 observingKfIds.push_back(kf->
mnId);
621 corrKpIndices.push_back(mit->second);
627 for (std::pair<WAIKeyFrame* const, size_t> it : observations)
629 if (!it.first->isBad())
631 observingKfIds.push_back(it.first->mnId);
632 corrKpIndices.push_back(it.second);
641 fwrite(&mpInfo,
sizeof(mpInfo), 1, f);
657 std::vector<T> result((T*)(*data), ((T*)(*data)) + count);
658 *data +=
sizeof(T) * count;
669 cv::Mat result = cv::Mat(rows, cols, type, *data);
671 *data += rows * cols * result.elemSize();
685 std::vector<WAIMapPoint*> mapPoints;
686 std::vector<WAIKeyFrame*> keyFrames;
687 std::map<int, int> parentIdMap;
688 std::map<int, std::vector<int>> loopEdgesMap;
689 std::map<int, WAIKeyFrame*> kfsMap;
690 int numLoopClosings = 0;
699 FILE* f = fopen(path.c_str(),
"rb");
703 fseek(f, 0, SEEK_END);
704 uint32_t contentSize = ftell(f);
707 uint8_t* fContent = (uint8_t*)malloc(
sizeof(uint8_t*) * contentSize);
708 uint8_t* fContentStart = fContent;
712 size_t readResult = fread(fContent, 1, contentSize, f);
713 if (readResult != contentSize)
724 mapNodeOm = cvMat.clone();
727 std::map<int, std::vector<int>> bestCovisibleKeyFrameIdsMap;
728 std::map<int, std::vector<int>> bestCovisibleWeightsMap;
730 for (
int i = 0; i < mapInfo->
kfCount; i++)
732 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::keyFrames");
742 parentIdMap[id] = parentId;
750 std::vector<int32_t> loopEdges =
751 loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->
loopEdgesCount);
753 loopEdgesMap[id] = loopEdges;
757 std::vector<cv::KeyPoint> keyPtsUndist = loadVectorFromBinaryStream<cv::KeyPoint>(&fContent, kfInfo->
kpCount);
763 std::vector<float> vScaleFactor;
764 std::vector<float> vInvScaleFactor;
765 std::vector<float> vLevelSigma2;
766 std::vector<float> vInvLevelSigma2;
767 vScaleFactor.clear();
768 vLevelSigma2.clear();
769 vScaleFactor.resize(nScaleLevels);
770 vLevelSigma2.resize(nScaleLevels);
772 vScaleFactor[0] = 1.0f;
773 vLevelSigma2[0] = 1.0f;
774 for (
int j = 1; j < nScaleLevels; j++)
776 vScaleFactor[j] = vScaleFactor[j - 1] * scaleFactor;
777 vLevelSigma2[j] = vScaleFactor[j] * vScaleFactor[j];
780 vInvScaleFactor.resize(nScaleLevels);
781 vInvLevelSigma2.resize(nScaleLevels);
782 for (
int j = 0; j < nScaleLevels; j++)
784 vInvScaleFactor[j] = 1.0f / vScaleFactor[j];
785 vInvLevelSigma2[j] = 1.0f / vLevelSigma2[j];
788 float fx, fy, cx, cy;
789 fx = K.at<
float>(0, 0);
790 fy = K.at<
float>(1, 1);
791 cx = K.at<
float>(0, 2);
792 cy = K.at<
float>(1, 2);
795 float nMinX = kfInfo->
minX;
796 float nMinY = kfInfo->
minY;
797 float nMaxX = kfInfo->
maxX;
798 float nMaxY = kfInfo->
maxY;
824 std::vector<int32_t> wordsId = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->
bowVecSize);
825 std::vector<float> tfIdf = loadVectorFromBinaryStream<float>(&fContent, kfInfo->
bowVecSize);
834 ss << imgDir <<
"kf" <<
id <<
".jpg";
839 cv::Mat imgColor = cv::imread(ss.str());
840 cv::cvtColor(imgColor, newKf->
imgGray, cv::COLOR_BGR2GRAY);
844 keyFrames.push_back(newKf);
845 kfsMap[newKf->
mnId] = newKf;
847 std::vector<int32_t> bestCovisibleKeyFrameIds = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->
covisiblesCount);
848 std::vector<int32_t> bestCovisibleWeights = loadVectorFromBinaryStream<int32_t>(&fContent, kfInfo->
covisiblesCount);
850 bestCovisibleKeyFrameIdsMap[newKf->
mnId] = bestCovisibleKeyFrameIds;
851 bestCovisibleWeightsMap[newKf->
mnId] = bestCovisibleWeights;
859 auto itParentId = parentIdMap.find(kf->mnId);
860 if (itParentId != parentIdMap.end())
862 int parentId = itParentId->second;
863 auto itParentKf = kfsMap.find(parentId);
864 if (itParentKf != kfsMap.end())
865 kf->ChangeParent(itParentKf->second);
867 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
870 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
874 int numberOfLoopClosings = 0;
878 auto it = loopEdgesMap.find(kf->mnId);
879 if (it != loopEdgesMap.end())
881 const auto& loopEdgeIds = it->second;
882 for (
int loopKfId : loopEdgeIds)
884 auto loopKfIt = kfsMap.find(loopKfId);
885 if (loopKfIt != kfsMap.end())
887 kf->AddLoopEdge(loopKfIt->second);
888 numberOfLoopClosings++;
891 cerr <<
"[WAIMapIO] loadKeyFrames: Loop keyframe id does not exist! FAIL" << endl;
895 numLoopClosings = numberOfLoopClosings / 2;
897 for (
int i = 0; i < mapInfo->
mpCount; i++)
899 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::mapPoints");
907 std::vector<int32_t> observingKfIds = loadVectorFromBinaryStream<int32_t>(&fContent, mpInfo->
nObervations);
908 std::vector<int32_t> corrKpIndices = loadVectorFromBinaryStream<int32_t>(&fContent, mpInfo->
nObervations);
920 int refKfId = (int)mpInfo->
refKfId;
921 bool refKFFound =
false;
923 if (kfsMap.find(refKfId) != kfsMap.end())
925 newPt->
refKf(kfsMap[refKfId]);
930 cout <<
"no reference keyframe found!" << endl;
931 if (observingKfIds.size())
934 int kfId = observingKfIds[0];
935 if (kfsMap.find(kfId) != kfsMap.end())
937 newPt->
refKf(kfsMap[kfId]);
946 for (
int j = 0; j < observingKfIds.size(); j++)
948 const int kfId = observingKfIds[j];
949 if (kfsMap.find(kfId) != kfsMap.end())
956 mapPoints.push_back(newPt);
966 bool buildSpanningTree =
false;
969 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::updateConnections");
971 std::map<WAIKeyFrame*, int> keyFrameWeightMap;
973 std::vector<int> bestCovisibleKeyFrameIds = bestCovisibleKeyFrameIdsMap[kf->mnId];
974 std::vector<int> bestCovisibleWeights = bestCovisibleWeightsMap[kf->mnId];
976 for (
int i = 0; i < bestCovisibleKeyFrameIds.size(); i++)
978 int keyFrameId = bestCovisibleKeyFrameIds[i];
979 int weight = bestCovisibleWeights[i];
981 keyFrameWeightMap[covisibleKF] = weight;
990 else if (kf->GetParent() == NULL)
992 buildSpanningTree =
true;
996 wai_assert(firstKF &&
"Could not find keyframe with id 0\n");
999 if (buildSpanningTree)
1001 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::buildSpanningTree");
1004 using QueueElem = std::tuple<WAIKeyFrame*, WAIKeyFrame*, int>;
1005 auto cmpQueue = [](
const QueueElem& left,
const QueueElem& right)
1006 {
return (std::get<2>(left) < std::get<2>(right)); };
1007 auto cmpMap = [](
const pair<WAIKeyFrame*, int>& left,
const pair<WAIKeyFrame*, int>& right)
1008 {
return left.second < right.second; };
1009 std::set<WAIKeyFrame*> graph;
1010 std::set<WAIKeyFrame*> unconKfs;
1011 for (
auto& kf : keyFrames)
1012 unconKfs.insert(kf);
1015 graph.insert(firstKF);
1016 unconKfs.erase(firstKF);
1018 while (unconKfs.size())
1020 std::priority_queue<QueueElem, std::vector<QueueElem>, decltype(cmpQueue)> q(cmpQueue);
1022 for (
auto& unconKf : unconKfs)
1024 const std::map<WAIKeyFrame*, int>& weights = unconKf->GetConnectedKfWeights();
1025 for (
auto& graphKf : graph)
1027 auto it = weights.find(graphKf);
1028 if (it != weights.end())
1030 QueueElem newElem = std::make_tuple(unconKf, it->first, it->second);
1039 Utils::log(
"WAIMapStorage",
"Error in building spanning tree: There are %i unconnected keyframes!", unconKfs.size());
1045 QueueElem topElem = q.top();
1048 unconKfs.erase(newGraphKf);
1052 graph.insert(newGraphKf);
1059 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::addKeyFrame");
1061 if (kf->mBowVec.data.empty())
1063 std::cout <<
"kf->mBowVec.data empty" << std::endl;
1078 PROFILE_SCOPE(
"WAI::WAIMapStorage::loadMapBinary::addMapPoint");
1085 free(fContentStart);
1099 std::vector<WAIMapPoint*> mapPoints;
1100 std::vector<WAIKeyFrame*> keyFrames;
1101 std::map<int, int> parentIdMap;
1102 std::map<int, std::vector<int>> loopEdgesMap;
1103 std::map<int, WAIKeyFrame*> kfsMap;
1104 int numLoopClosings = 0;
1113 cv::FileStorage fs(path, cv::FileStorage::READ);
1120 if (!fs[
"mapNodeOm"].empty())
1122 fs[
"mapNodeOm"] >> mapNodeOm;
1125 std::map<int, std::vector<int>> bestCovisibleKeyFrameIdsMap;
1126 std::map<int, std::vector<int>> bestCovisibleWeightsMap;
1128 bool updateKeyFrameConnections =
false;
1130 cv::FileNode n = fs[
"KeyFrames"];
1131 for (
auto it = n.begin(); it != n.end(); ++it)
1133 int id = (*it)[
"id"];
1134 if (!(*it)[
"parentId"].empty())
1136 int parentId = (*it)[
"parentId"];
1137 parentIdMap[id] = parentId;
1139 if (!(*it)[
"loopEdges"].empty() && (*it)[
"loopEdges"].isSeq())
1141 cv::FileNode les = (*it)[
"loopEdges"];
1142 std::vector<int> loopEdges;
1143 for (
auto itLes = les.begin(); itLes != les.end(); ++itLes)
1145 loopEdges.push_back((
int)*itLes);
1147 loopEdgesMap[id] = loopEdges;
1150 (*it)[
"Tcw"] >> Tcw;
1152 cv::Mat featureDescriptors;
1153 (*it)[
"featureDescriptors"] >> featureDescriptors;
1154 std::vector<cv::KeyPoint> keyPtsUndist;
1155 (*it)[
"keyPtsUndist"] >> keyPtsUndist;
1157 std::vector<int> wordsId;
1158 std::vector<float> tfIdf;
1159 if (!(*it)[
"BowVectorWordsId"].empty())
1160 (*it)[
"BowVectorWordsId"] >> wordsId;
1161 if (!(*it)[
"TfIdf"].empty())
1162 (*it)[
"TfIdf"] >> tfIdf;
1165 (*it)[
"scaleFactor"] >> scaleFactor;
1166 int nScaleLevels = -1;
1167 (*it)[
"nScaleLevels"] >> nScaleLevels;
1170 std::vector<float> vScaleFactor;
1171 std::vector<float> vInvScaleFactor;
1172 std::vector<float> vLevelSigma2;
1173 std::vector<float> vInvLevelSigma2;
1174 vScaleFactor.clear();
1175 vLevelSigma2.clear();
1176 vScaleFactor.resize(nScaleLevels);
1177 vLevelSigma2.resize(nScaleLevels);
1178 vScaleFactor[0] = 1.0f;
1179 vLevelSigma2[0] = 1.0f;
1180 for (
int i = 1; i < nScaleLevels; i++)
1182 vScaleFactor[i] = vScaleFactor[i - 1] * scaleFactor;
1183 vLevelSigma2[i] = vScaleFactor[i] * vScaleFactor[i];
1186 vInvScaleFactor.resize(nScaleLevels);
1187 vInvLevelSigma2.resize(nScaleLevels);
1188 for (
int i = 0; i < nScaleLevels; i++)
1190 vInvScaleFactor[i] = 1.0f / vScaleFactor[i];
1191 vInvLevelSigma2[i] = 1.0f / vLevelSigma2[i];
1196 float fx, fy, cx, cy;
1197 fx = K.at<
float>(0, 0);
1198 fy = K.at<
float>(1, 1);
1199 cx = K.at<
float>(0, 2);
1200 cy = K.at<
float>(1, 2);
1203 float nMinX, nMinY, nMaxX, nMaxY;
1204 (*it)[
"nMinX"] >> nMinX;
1205 (*it)[
"nMinY"] >> nMinY;
1206 (*it)[
"nMaxX"] >> nMaxX;
1207 (*it)[
"nMaxY"] >> nMaxY;
1216 keyPtsUndist.size(),
1231 if (!wordsId.empty() && !tfIdf.empty())
1240 ss << imgDir <<
"kf" <<
id <<
".jpg";
1245 cv::Mat imgColor = cv::imread(ss.str());
1246 cv::cvtColor(imgColor, newKf->
imgGray, cv::COLOR_BGR2GRAY);
1249 keyFrames.push_back(newKf);
1250 kfsMap[newKf->
mnId] = newKf;
1252 if (!(*it)[
"bestCovisibleKeyFrameIds"].empty() &&
1253 !(*it)[
"bestCovisibleWeights"].empty())
1255 std::vector<int> bestCovisibleKeyFrameIds;
1256 (*it)[
"bestCovisibleKeyFrameIds"] >> bestCovisibleKeyFrameIds;
1257 std::vector<int> bestCovisibleWeights;
1258 (*it)[
"bestCovisibleWeights"] >> bestCovisibleWeights;
1260 bestCovisibleKeyFrameIdsMap[newKf->
mnId] = bestCovisibleKeyFrameIds;
1261 bestCovisibleWeightsMap[newKf->
mnId] = bestCovisibleWeights;
1265 updateKeyFrameConnections =
true;
1274 auto itParentId = parentIdMap.find(kf->mnId);
1275 if (itParentId != parentIdMap.end())
1277 int parentId = itParentId->second;
1278 auto itParentKf = kfsMap.find(parentId);
1279 if (itParentKf != kfsMap.end())
1282 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
1285 cerr <<
"[WAIMapIO] loadKeyFrames: Parent does not exist of keyframe " << kf->mnId <<
"! FAIL" << endl;
1289 int numberOfLoopClosings = 0;
1293 auto it = loopEdgesMap.find(kf->mnId);
1294 if (it != loopEdgesMap.end())
1296 const auto& loopEdgeIds = it->second;
1297 for (
int loopKfId : loopEdgeIds)
1299 auto loopKfIt = kfsMap.find(loopKfId);
1300 if (loopKfIt != kfsMap.end())
1302 kf->AddLoopEdge(loopKfIt->second);
1303 numberOfLoopClosings++;
1306 cerr <<
"[WAIMapIO] loadKeyFrames: Loop keyframe id does not exist! FAIL" << endl;
1310 numLoopClosings = numberOfLoopClosings / 2;
1312 n = fs[
"MapPoints"];
1313 if (n.type() != cv::FileNode::SEQ)
1315 cerr <<
"strings is not a sequence! FAIL" << endl;
1318 bool needMapPointUpdate =
false;
1319 for (
auto it = n.begin(); it != n.end(); ++it)
1322 int id = (int)(*it)[
"id"];
1325 (*it)[
"mWorldPos"] >> mWorldPos;
1328 vector<int> observingKfIds;
1329 (*it)[
"observingKfIds"] >> observingKfIds;
1330 vector<int> corrKpIndices;
1331 (*it)[
"corrKpIndices"] >> corrKpIndices;
1334 int refKfId = (int)(*it)[
"refKfId"];
1335 bool refKFFound =
false;
1337 if (kfsMap.find(refKfId) != kfsMap.end())
1339 newPt->
refKf(kfsMap[refKfId]);
1344 cout <<
"no reference keyframe found!" << endl;
1345 if (observingKfIds.size())
1348 int kfId = observingKfIds[0];
1349 if (kfsMap.find(kfId) != kfsMap.end())
1351 newPt->
refKf(kfsMap[kfId]);
1357 if (!(*it)[
"mfMaxDistance"].empty() &&
1358 !(*it)[
"mfMinDistance"].empty() &&
1359 !(*it)[
"mNormalVector"].empty() &&
1360 !(*it)[
"mDescriptor"].empty())
1364 cv::Mat normal, descriptor;
1365 (*it)[
"mNormalVector"] >> normal;
1366 (*it)[
"mDescriptor"] >> descriptor;
1372 needMapPointUpdate =
true;
1378 for (
int i = 0; i < observingKfIds.size(); ++i)
1380 const int kfId = observingKfIds[i];
1381 if (kfsMap.find(kfId) != kfsMap.end())
1388 mapPoints.push_back(newPt);
1396 std::cout <<
"update the covisibility graph, when all keyframes and mappoints are loaded" << std::endl;
1399 bool buildSpanningTree =
false;
1402 if (updateKeyFrameConnections)
1409 std::map<WAIKeyFrame*, int> keyFrameWeightMap;
1411 std::vector<int> bestCovisibleKeyFrameIds = bestCovisibleKeyFrameIdsMap[kf->mnId];
1412 std::vector<int> bestCovisibleWeights = bestCovisibleWeightsMap[kf->mnId];
1414 for (
int i = 0; i < bestCovisibleKeyFrameIds.size(); i++)
1416 int keyFrameId = bestCovisibleKeyFrameIds[i];
1417 int weight = bestCovisibleWeights[i];
1419 keyFrameWeightMap[covisibleKF] = weight;
1428 else if (kf->GetParent() == NULL)
1430 buildSpanningTree =
true;
1434 wai_assert(firstKF &&
"Could not find keyframe with id 0\n");
1437 if (buildSpanningTree)
1440 using QueueElem = std::tuple<WAIKeyFrame*, WAIKeyFrame*, int>;
1441 auto cmpQueue = [](
const QueueElem& left,
const QueueElem& right)
1442 {
return (std::get<2>(left) < std::get<2>(right)); };
1443 auto cmpMap = [](
const pair<WAIKeyFrame*, int>& left,
const pair<WAIKeyFrame*, int>& right)
1444 {
return left.second < right.second; };
1445 std::set<WAIKeyFrame*> graph;
1446 std::set<WAIKeyFrame*> unconKfs;
1447 for (
auto& kf : keyFrames)
1448 unconKfs.insert(kf);
1451 graph.insert(firstKF);
1452 unconKfs.erase(firstKF);
1454 while (unconKfs.size())
1456 std::priority_queue<QueueElem, std::vector<QueueElem>, decltype(cmpQueue)> q(cmpQueue);
1458 for (
auto& unconKf : unconKfs)
1460 const std::map<WAIKeyFrame*, int>& weights = unconKf->GetConnectedKfWeights();
1461 for (
auto& graphKf : graph)
1463 auto it = weights.find(graphKf);
1464 if (it != weights.end())
1466 QueueElem newElem = std::make_tuple(unconKf, it->first, it->second);
1475 Utils::log(
"WAIMapStorage",
"Error in building spanning tree: There are %i unconnected keyframes!");
1481 QueueElem topElem = q.top();
1484 unconKfs.erase(newGraphKf);
1488 graph.insert(newGraphKf);
1493 if (needMapPointUpdate)
1501 mp->UpdateNormalAndDepth();
1502 mp->ComputeDistinctiveDescriptors();
1508 if (kf->mBowVec.data.empty())
1510 std::cout <<
"kf->mBowVec.data empty" << std::endl;
1538 ofs.open(dir +
"/" + outputKFMatchingFile, std::ofstream::out);
1540 ofs << to_string(vidname.size()) <<
"\n";
1542 for (
int i = 0; i < vidname.size(); i++)
1545 ofs << vidname[i] <<
"\n";
1548 for (
int i = 0; i < keyFrameVideoMatching.size(); i++)
1550 if (keyFrameVideoMatching[i] >= 0)
1552 ofs << to_string(i) +
" " + to_string(keyFrameVideoMatching[i]) <<
"\n";
1560 std::ifstream ifs(dir +
"/" + kFMatchingFile);
1561 keyFrameVideoMatching.resize(1000, -1);
1565 vidname.resize(nVid);
1567 for (
int i = 0; i < nVid; i++)
1575 while (ifs >> kfId >> vid)
1577 if (kfId > keyFrameVideoMatching.size())
1579 keyFrameVideoMatching.resize(keyFrameVideoMatching.size() * 2, -1);
1581 keyFrameVideoMatching[kfId] = vid;
vector< cv::KeyPoint > CVVKeyPoint
#define PROFILE_SCOPE(name)
#define PROFILE_FUNCTION()
#define wai_assert(expression)
void saveKeyFrames(std::vector< WAIKeyFrame * > &kfs, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching, cv::FileStorage &fs, std::string imgDir, bool saveBOW)
void saveMapPoints(std::vector< WAIMapPoint * > mpts, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching, cv::FileStorage &fs)
void buildMatching(std::vector< WAIKeyFrame * > &kfs, std::map< WAIKeyFrame *, std::map< size_t, size_t >> &KFmatching)
void setMatrix(const SLMat4 &A)
Set matrix by other 4x4 matrix.
SLstring toString() const
SLNode represents a node in a hierarchical scene graph.
void om(const SLMat4f &mat)
void add(WAIKeyFrame *pKF)
void SetBowVector(WAIBowVector &bow)
std::vector< WAIKeyFrame * > GetBestCovisibilityKeyFrames(const int &N)
void AddMapPoint(WAIMapPoint *pMP, size_t idx)
void setTexturePath(const std::string &path)
void ChangeParent(WAIKeyFrame *pKF)
WAIKeyFrame * GetParent()
const float mfScaleFactor
void FindAndUpdateConnections(bool buildSpanningTree=true)
std::set< WAIKeyFrame * > GetLoopEdges()
const std::vector< cv::KeyPoint > mvKeysUn
void UpdateConnections(std::map< WAIKeyFrame *, int > KFcounter, bool buildSpanningTree)
std::vector< WAIMapPoint * > GetMapPointMatches()
const cv::Mat mDescriptors
int GetWeight(WAIKeyFrame *pKF)
vector< WAIKeyFrame * > mvpKeyFrameOrigins
void AddKeyFrame(WAIKeyFrame *pKF)
void setNumLoopClosings(int n)
std::vector< WAIKeyFrame * > GetAllKeyFrames()
WAIKeyFrameDB * GetKeyFrameDB()
void AddMapPoint(WAIMapPoint *pMP)
std::vector< WAIMapPoint * > GetAllMapPoints()
void SetMinDistance(float minDist)
void AddObservation(WAIKeyFrame *pKF, size_t idx)
std::map< WAIKeyFrame *, size_t > GetObservations()
void SetDescriptor(const cv::Mat &descriptor)
void SetNormal(const cv::Mat &normal)
void SetMaxDistance(float maxDist)
WAIKeyFrame * refKf() const
static bool saveMapRaw(WAIMap *waiMap, SLNode *mapNode, std::string fileName, std::string imgDir="")
static void saveKeyFrameVideoMatching(std::vector< int > &keyFrameVideoMatching, std::vector< std::string > vidname, const std::string &mapDir, const std::string outputKFMatchingFile)
static std::vector< T > loadVectorFromBinaryStream(uint8_t **data, int count)
static void writeVectorToBinaryFile(FILE *f, const std::vector< T > vec)
static cv::Mat convertToCVMat(const SLMat4f slMat)
static void writeCVMatToBinaryFile(FILE *f, const cv::Mat &mat)
static bool saveMapBinary(WAIMap *waiMap, SLNode *mapNode, std::string fileName, std::string imgDir="", bool saveBOW=true)
static bool loadMap(WAIMap *waiMap, cv::Mat &mapNodeOm, WAIOrbVocabulary *voc, std::string path, bool loadImgs, bool fixKfsAndMPts)
static cv::Mat loadCVMatFromBinaryStream(uint8_t **data, int rows, int cols, int type)
static SLMat4f convertToSLMat(const cv::Mat &cvMat)
static void loadKeyFrameVideoMatching(std::vector< int > &keyFrameVideoMatching, std::vector< std::string > &vidname, const std::string &mapDir, const std::string outputKFMatchingFile)
static std::vector< uint8_t > convertCVMatToVector(const cv::Mat &mat)
static bool loadMapBinary(WAIMap *waiMap, cv::Mat &mapNodeOm, WAIOrbVocabulary *voc, std::string path, bool loadImgs, bool fixKfsAndMPts)
static bool saveMap(WAIMap *waiMap, SLNode *mapNode, std::string fileName, std::string imgDir="", bool saveBOW=true)
bool fileExists(const string &pathfilename)
Returns true if a file exists.
bool dirExists(const string &path)
Returns true if a directory exists.
bool makeDir(const string &path)
Creates a directory with given path.
string getFileNameWOExt(const string &pathFilename)
Returns the filename without extension.
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
string getPath(const string &pathFilename)
Returns the path w. '\' of path-filename string.
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
fbow::fBow & getWordScoreMapping()