10 #define LOG_WAISLAM_WARN(...) Utils::log("WAISlam", __VA_ARGS__);
11 #define LOG_WAISLAM_INFO(...) Utils::log("WAISlam", __VA_ARGS__);
12 #define LOG_WAISLAM_DEBUG(...) Utils::log("WAISlam", __VA_ARGS__);
17 int rhw = (int)(scale * 3.0f);
20 for (
size_t i = 0; i < frame.
N; i++)
24 auto pt = scale * frame.
mvKeys[i].pt;
26 cv::Rect((
int)(pt.x - rhw), (
int)(pt.y - rhw), rw, rw),
27 cv::Scalar(0, 0, 255));
34 int rhw = (int)(scale * 3.0f);
36 for (
size_t i = 0; i < frame.
N; i++)
43 auto pt = scale * frame.
mvKeys[i].pt;
45 cv::Rect((
int)(pt.x - rhw), (
int)(pt.y - rhw), rw, rw),
46 cv::Scalar(0, 255, 0));
55 int rhw = (int)(scale * 3.0f);
61 cv::rectangle(imageBGR,
62 cv::Rect((
int)(pt.x - rhw), (
int)(pt.y - rhw), rw, rw),
63 cv::Scalar(0, 0, 255));
66 for (
unsigned int i = 0; i < iniData.
iniMatches.size(); i++)
73 cv::Scalar(0, 255, 0));
84 int matchesNeeded = mapPointsNeeded;
89 if (frame.
mvKeys.size() > matchesNeeded)
95 for (
size_t i = 0; i < frame.
mvKeysUn.size(); i++)
98 iniData.
initializer =
new ORB_SLAM2::Initializer(frame, 1.0, 200);
106 if ((
int)frame.
mvKeys.size() <= matchesNeeded)
109 iniData.
initializer =
static_cast<Initializer*
>(NULL);
115 ORBmatcher matcher(0.9f,
true);
119 if (nmatches < matchesNeeded)
122 iniData.
initializer =
static_cast<Initializer*
>(NULL);
129 vector<bool> vbTriangulated;
133 for (
size_t i = 0, iend = iniData.
iniMatches.size(); i < iend; i++)
135 if (iniData.
iniMatches[i] >= 0 && !vbTriangulated[i])
151 cv::Mat Tcw = cv::Mat::eye(4, 4, CV_32F);
152 Rcw.copyTo(Tcw.rowRange(0, 3).colRange(0, 3));
153 tcw.copyTo(Tcw.rowRange(0, 3).col(3));
170 for (
size_t i = 0; i < iniData.
iniMatches.size(); i++)
199 float invMedianDepth = 1.0f / medianDepth;
201 if (medianDepth < 0 || pKFcur->TrackedMapPoints(1) < mapPointsNeeded)
203 Utils::log(
"WAI",
"Wrong initialization, reseting...");
212 localMap.keyFrames.push_back(pKFini);
213 localMap.keyFrames.push_back(pKFcur);
215 cv::Mat Tc2w = pKFcur->
GetPose();
216 Tc2w.col(3).rowRange(0, 3) = Tc2w.col(3).rowRange(0, 3) * invMedianDepth;
221 for (
size_t iMP = 0; iMP < vpAllMapPoints.size(); iMP++)
223 if (vpAllMapPoints[iMP])
227 localMap.mapPoints.push_back(pMP);
232 localMap.refKF = pKFcur;
239 LocalMapping* localMapper,
240 LoopClosing* loopCloser,
243 if (localMap.keyFrames.size() != 2)
246 std::unique_lock<std::mutex> lock(map->
mMutexMapUpdate, std::defer_lock);
253 for (
size_t i = 0; i < localMap.mapPoints.size(); i++)
261 Optimizer::GlobalBundleAdjustemnt(map, 20);
273 LocalMapping* localMapper,
274 LoopClosing* loopCloser,
278 int matchesNeeded = mapPointsNeeded;
281 std::unique_lock<std::mutex> lock(map->
mMutexMapUpdate, std::defer_lock);
287 if (frame.
mvKeys.size() > matchesNeeded)
293 for (
size_t i = 0; i < frame.
mvKeysUn.size(); i++)
296 iniData.
initializer =
new ORB_SLAM2::Initializer(frame, 1.0, 200);
303 if ((
int)frame.
mvKeys.size() <= matchesNeeded)
306 iniData.
initializer =
static_cast<Initializer*
>(NULL);
312 ORBmatcher matcher(0.9f,
true);
316 if (nmatches < matchesNeeded)
319 iniData.
initializer =
static_cast<Initializer*
>(NULL);
325 vector<bool> vbTriangulated;
329 for (
size_t i = 0, iend = iniData.
iniMatches.size(); i < iend; i++)
331 if (iniData.
iniMatches[i] >= 0 && !vbTriangulated[i])
345 cv::Mat Tcw = cv::Mat::eye(4, 4, CV_32F);
346 Rcw.copyTo(Tcw.rowRange(0, 3).colRange(0, 3));
347 tcw.copyTo(Tcw.rowRange(0, 3).col(3));
364 for (
size_t i = 0; i < iniData.
iniMatches.size(); i++)
397 Optimizer::GlobalBundleAdjustemnt(map, 20);
401 float invMedianDepth = 1.0f / medianDepth;
403 if (medianDepth < 0 || pKFcur->TrackedMapPoints(1) < mapPointsNeeded)
405 Utils::log(
"WAI",
"Wrong initialization, reseting...");
406 localMapper->RequestReset();
410 localMap.keyFrames.clear();
411 localMap.mapPoints.clear();
412 localMap.refKF =
nullptr;
421 localMap.keyFrames.push_back(pKFcur);
422 localMap.keyFrames.push_back(pKFini);
425 cv::Mat Tc2w = pKFcur->
GetPose();
426 Tc2w.col(3).rowRange(0, 3) = Tc2w.col(3).rowRange(0, 3) * invMedianDepth;
431 for (
size_t iMP = 0; iMP < vpAllMapPoints.size(); iMP++)
433 if (vpAllMapPoints[iMP])
440 localMapper->InsertKeyFrame(pKFini);
441 localMapper->InsertKeyFrame(pKFcur);
444 localMap.refKF = pKFcur;
458 int lastRelocFrameId,
474 return trackLocalMap(localMap, frame, lastRelocFrameId, inliers);
481 int lastRelocFrameId,
488 return trackLocalMap(localMap, frame, lastRelocFrameId, inliers);
493 int lastRelocFrameId,
497 if (!localMap.keyFrames.empty())
509 LocalMapping* localMapper,
512 const unsigned long lastRelocFrameId,
513 unsigned long& lastKeyFrameFrameId)
515 if (
needNewKeyFrame(map, localMap, localMapper, frame, inliers, lastRelocFrameId, lastKeyFrameFrameId))
523 LocalMapping* localMapper,
526 const unsigned long lastRelocFrameId,
527 unsigned long& lastKeyFrameFrameId)
529 if (
strictNeedNewKeyFrame(map, localMap, localMapper, frame, inliers, lastRelocFrameId, lastKeyFrameFrameId))
540 if (!lastFrame.
mTcw.empty())
542 cv::Mat LastTwc = cv::Mat::eye(4, 4, CV_32F);
548 cc.copyTo(LastTwc.rowRange(0, 3).col(3));
550 velocity = frame.
mTcw * LastTwc;
551 pose = frame.
mTcw.clone();
555 velocity = cv::Mat();
563 unsigned long& lastKeyFrameFrameId)
567 lastKeyFrameFrameId = frame.
mnId;
571 localMapper->InsertKeyFrame(pKF);
577 double reprojectionError = 0.0;
583 const float fx = frame.
fx;
584 const float fy = frame.
fy;
585 const float cx = frame.
cx;
586 const float cy = frame.
cy;
591 for (
size_t i = 0; i < frame.
N; i++)
607 const cv::Mat Pc = Rcw * Pw + tcw;
608 const float& PcX = Pc.at<
float>(0);
609 const float& PcY = Pc.at<
float>(1);
610 const float& PcZ = Pc.at<
float>(2);
617 const float invz = 1.0f / PcZ;
618 const float u = fx * PcX * invz + cx;
619 const float v = fy * PcY * invz + cy;
621 cv::Point2f ptProj(u, v);
623 const auto& ptImg = frame.
mvKeysUn[i].pt;
625 if (cv::norm(cv::Mat(ptImg), cv::Mat(ptProj)) > 1.3)
632 LocalMapping* localMapper,
635 const unsigned long lastRelocFrameId,
636 const unsigned long lastKeyFrameFrameId)
638 if (localMapper->isPaused())
654 int nRefMatches = localMap.refKF->TrackedMapPoints(nMinObs);
657 float thRefRatio = 0.9f;
664 const bool c1b = (frame.
mnId >= lastKeyFrameFrameId +
MIN_FRAMES) && localMapper->AcceptKeyFrames();
666 const bool c2 = ((nInliers < nRefMatches * thRefRatio) && nInliers > 15);
668 return ((c1a || c1b) && c2);
673 LocalMapping* localMapper,
676 const unsigned long lastRelocFrameId,
677 const unsigned long lastKeyFrameFrameId)
679 if (localMapper->isPaused() || !localMapper->AcceptKeyFrames())
695 int nRefMatches = localMap.refKF->TrackedMapPoints(nMinObs);
697 unsigned int m, n, outliers;
698 bool c2 = nInliers > 45;
702 unsigned int nLoadedCorrect = n - outliers;
703 if (nLoadedCorrect > 0.25 * m)
705 if ((
float)(nLoadedCorrect) / (
float)n > 0.5f)
716 float minCommonWordFactor,
718 bool minAccScoreFilter)
725 vector<WAIKeyFrame*> vpCandidateKFs;
728 if (vpCandidateKFs.empty())
735 const int nKFs = (int)vpCandidateKFs.size();
744 ORBmatcher matcher(0.75,
true);
746 vector<PnPsolver*> vpPnPsolvers;
747 vpPnPsolvers.resize(nKFs);
749 vector<vector<WAIMapPoint*>> vvpMapPointMatches;
750 vvpMapPointMatches.resize(nKFs);
752 std::vector<bool> outliers;
754 vector<bool> vbDiscarded;
755 vbDiscarded.resize(nKFs);
759 for (
int i = 0; i < nKFs; i++)
763 vbDiscarded[i] =
true;
766 int nmatches = matcher.SearchByBoW(pKF, currentFrame, vvpMapPointMatches[i]);
769 vbDiscarded[i] =
true;
774 PnPsolver* pSolver =
new PnPsolver(currentFrame, vvpMapPointMatches[i]);
775 pSolver->SetRansacParameters(0.99, 10, 300, 4, 0.5f, 5.991f);
776 vpPnPsolvers[i] = pSolver;
785 ORBmatcher matcher2(0.9f,
true);
787 while (nCandidates > 0 && !bMatch)
789 for (
int i = 0; i < nKFs; i++)
795 vector<bool> vbInliers;
799 PnPsolver* pSolver = vpPnPsolvers[i];
800 cv::Mat Tcw = pSolver->iterate(5, bNoMore, vbInliers, nInliers);
805 vbDiscarded[i] =
true;
812 Tcw.copyTo(currentFrame.
mTcw);
814 set<WAIMapPoint*> sFound;
816 const int np = (int)vbInliers.size();
818 for (
int j = 0; j < np; j++)
822 currentFrame.
mvpMapPoints[j] = vvpMapPointMatches[i][j];
823 sFound.insert(vvpMapPointMatches[i][j]);
829 int nGood = Optimizer::PoseOptimization(¤tFrame, outliers);
842 int nadditional = matcher2.SearchByProjection(currentFrame, vpCandidateKFs[i], sFound, 10, 100);
844 if (nadditional + nGood >= 50)
846 nGood = Optimizer::PoseOptimization(¤tFrame, outliers);
850 if (nGood > 30 && nGood < 50)
853 for (
int ip = 0; ip < currentFrame.
N; ip++)
856 nadditional = matcher2.SearchByProjection(currentFrame, vpCandidateKFs[i], sFound, 3, 64);
859 if (nGood + nadditional >= 50)
861 nGood = Optimizer::PoseOptimization(¤tFrame);
887 float minCommonWordFactor,
889 bool minAccScoreFilter)
894 vector<WAIKeyFrame*> vpCandidateKFs;
900 cv::Mat tcw = kf->GetPose();
901 cv::Mat_<double> front(3, 1);
908 if (dirENU.dot(front) < 0)
911 cv::Mat pose = tcw.col(3).rowRange(0, 2);
913 float dist = (float)cv::norm(locENU, pose);
918 vpCandidateKFs.push_back(kf);
921 if (vpCandidateKFs.empty())
927 const int nKFs = (int)vpCandidateKFs.size();
932 ORBmatcher matcher(0.75,
true);
934 vector<PnPsolver*> vpPnPsolvers;
935 vpPnPsolvers.resize(nKFs);
937 vector<vector<WAIMapPoint*>> vvpMapPointMatches;
938 vvpMapPointMatches.resize(nKFs);
940 std::vector<bool> outliers;
942 vector<bool> vbDiscarded;
943 vbDiscarded.resize(nKFs);
947 for (
int i = 0; i < nKFs; i++)
951 vbDiscarded[i] =
true;
954 int nmatches = matcher.SearchByBoW(pKF, currentFrame, vvpMapPointMatches[i]);
957 vbDiscarded[i] =
true;
962 PnPsolver* pSolver =
new PnPsolver(currentFrame, vvpMapPointMatches[i]);
963 pSolver->SetRansacParameters(0.99, 10, 300, 4, 0.5f, 5.991f);
964 vpPnPsolvers[i] = pSolver;
973 ORBmatcher matcher2(0.9f,
true);
975 while (nCandidates > 0 && !bMatch)
977 for (
int i = 0; i < nKFs; i++)
983 vector<bool> vbInliers;
987 PnPsolver* pSolver = vpPnPsolvers[i];
988 cv::Mat Tcw = pSolver->iterate(5, bNoMore, vbInliers, nInliers);
993 vbDiscarded[i] =
true;
1000 Tcw.copyTo(currentFrame.
mTcw);
1002 set<WAIMapPoint*> sFound;
1004 const int np = (int)vbInliers.size();
1006 for (
int j = 0; j < np; j++)
1010 currentFrame.
mvpMapPoints[j] = vvpMapPointMatches[i][j];
1011 sFound.insert(vvpMapPointMatches[i][j]);
1017 int nGood = Optimizer::PoseOptimization(¤tFrame, outliers);
1030 int nadditional = matcher2.SearchByProjection(currentFrame, vpCandidateKFs[i], sFound, 10, 100);
1032 if (nadditional + nGood >= 50)
1034 nGood = Optimizer::PoseOptimization(¤tFrame, outliers);
1038 if (nGood > 30 && nGood < 50)
1041 for (
int ip = 0; ip < currentFrame.
N; ip++)
1044 nadditional = matcher2.SearchByProjection(currentFrame, vpCandidateKFs[i], sFound, 3, 64);
1047 if (nGood + nadditional >= 50)
1049 nGood = Optimizer::PoseOptimization(¤tFrame);
1058 bMatch =
trackLocalMap(localMap, currentFrame, (
int)currentFrame.
mnId, inliers);
1090 ORBmatcher matcher(0.7f,
true);
1091 vector<WAIMapPoint*> vpMapPointMatches;
1093 int nmatches = matcher.SearchByBoW(map.refKF, frame, vpMapPointMatches);
1104 nmatches = Optimizer::PoseOptimization(&frame);
1111 return nmatches >= 10;
1134 ORBmatcher matcher(0.7f,
true);
1135 vector<WAIMapPoint*> vpMapPointMatches;
1137 int nmatches = matcher.SearchByBoW(map.refKF, frame, vpMapPointMatches);
1148 nmatches = Optimizer::PoseOptimization(&frame);
1155 return nmatches >= 20;
1159 int lastRelocFrameId,
1163 for (vector<WAIMapPoint*>::iterator vit = frame.
mvpMapPoints.begin(), vend = frame.
mvpMapPoints.end(); vit != vend; vit++)
1184 for (vector<WAIMapPoint*>::iterator vit = localMap.mapPoints.begin(), vend = localMap.mapPoints.end(); vit != vend; vit++)
1201 ORBmatcher matcher(0.8f);
1204 if (frame.
mnId - 1 == lastRelocFrameId)
1206 matcher.SearchByProjection(frame, localMap.mapPoints, (
float)th);
1210 Optimizer::PoseOptimization(&frame);
1211 int matchesInliers = 0;
1214 for (
int i = 0; i < frame.
N; i++)
1226 return matchesInliers;
1233 map<WAIKeyFrame*, int> keyframeCounter;
1234 for (
int i = 0; i < frame.
N; i++)
1241 const map<WAIKeyFrame*, size_t> observations = pMP->
GetObservations();
1242 for (map<WAIKeyFrame*, size_t>::const_iterator it = observations.begin(), itend = observations.end(); it != itend; it++)
1243 keyframeCounter[it->first]++;
1252 if (keyframeCounter.empty())
1258 localMap.keyFrames.clear();
1259 localMap.keyFrames.reserve(3 * keyframeCounter.size());
1263 for (
auto it = keyframeCounter.begin(), itEnd = keyframeCounter.end(); it != itEnd; it++)
1270 if (it->second > max)
1273 localMap.refKF = pKF;
1276 localMap.keyFrames.push_back(it->first);
1281 for (
int i = 0, iend = (
int)localMap.keyFrames.size(); i < iend; ++i)
1284 if (localMap.keyFrames.size() > 80)
1291 for (vector<WAIKeyFrame*>::const_iterator itNeighKF = vNeighs.begin(), itEndNeighKF = vNeighs.end(); itNeighKF != itEndNeighKF; itNeighKF++)
1294 if (!pNeighKF->
isBad())
1298 localMap.keyFrames.push_back(pNeighKF);
1305 const set<WAIKeyFrame*> spChilds = pKF->
GetChilds();
1306 for (set<WAIKeyFrame*>::const_iterator sit = spChilds.begin(), send = spChilds.end(); sit != send; sit++)
1309 if (!pChildKF->
isBad())
1313 localMap.keyFrames.push_back(pChildKF);
1325 localMap.keyFrames.push_back(pParent);
1332 localMap.mapPoints.clear();
1333 for (
int i = 0; i < localMap.keyFrames.size(); ++i)
1338 for (vector<WAIMapPoint*>::const_iterator itMP = vpMPs.begin(), itEndMP = vpMPs.end(); itMP != itEndMP; itMP++)
1347 localMap.mapPoints.push_back(pMP);
1360 if (velocity.empty() || frame.
mnId > previousFrame.
mnId + 1)
1363 ORBmatcher matcher(0.9f,
true);
1378 int nmatches = matcher.SearchByProjection(frame, previousFrame, (
float)th,
true);
1384 nmatches = matcher.SearchByProjection(frame, previousFrame, (
float)(2 * th),
true);
1394 nmatches = Optimizer::PoseOptimization(&frame);
1398 return nmatches >= 10;
1402 KPextractor* markerExtractor,
1403 const cv::Mat& markerCameraIntrinsic,
1406 cv::Mat markerImgGray = cv::imread(markerFile, cv::IMREAD_GRAYSCALE);
1408 float fyCam = (float)markerCameraIntrinsic.at<
double>(1, 1);
1409 float cyCam = (float)markerCameraIntrinsic.at<
double>(1, 2);
1410 float fov = (float)(2.0f * atan2(cyCam, fyCam) * 180.0f / M_PI);
1412 float cx = (float)markerImgGray.cols * 0.5f;
1413 float cy = (
float)markerImgGray.rows * 0.5f;
1414 float fy = cy / tanf(fov * 0.5f * (
float)M_PI / 180.0f);
1418 cv::Mat markerCameraMat = (cv::Mat_<float>(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
1419 cv::Mat markerDistortionMat = cv::Mat::zeros(4, 1, CV_32F);
1421 WAIFrame result =
WAIFrame(markerImgGray, 0.0f, markerExtractor, markerCameraMat, markerDistortionMat, voc,
true);
1422 result =
WAIFrame(markerImgGray, 0.0f, markerExtractor, markerCameraMat, markerDistortionMat, voc,
true);
1423 result =
WAIFrame(markerImgGray, 0.0f, markerExtractor, markerCameraMat, markerDistortionMat, voc,
true);
1429 cv::Mat& homography,
1432 bool result =
false;
1434 ORBmatcher matcher(0.9f,
true);
1436 std::vector<int> markerMatchesToCurrentFrame;
1437 int nmatches = matcher.SearchForMarkerMap(markerFrame, *kfCand, markerMatchesToCurrentFrame);
1439 if (nmatches > minMatches)
1441 std::vector<cv::Point2f> markerPoints;
1442 std::vector<cv::Point2f> framePoints;
1444 for (
int j = 0; j < markerMatchesToCurrentFrame.size(); j++)
1446 if (markerMatchesToCurrentFrame[j] >= 0)
1448 markerPoints.push_back(markerFrame.
mvKeysUn[j].pt);
1449 framePoints.push_back(kfCand->
mvKeysUn[markerMatchesToCurrentFrame[j]].pt);
1453 homography = cv::findHomography(markerPoints,
1457 if (!homography.empty())
1459 homography.convertTo(homography, CV_32F);
1468 cv::Mat& nodeTransform,
1469 float markerWidthInM,
1470 KPextractor* markerExtractor,
1472 const cv::Mat& markerCameraIntrinsic,
1480 markerCameraIntrinsic,
1489 cv::Mat ul = cv::Mat(cv::Point3f(0, 0, 1));
1490 cv::Mat ur = cv::Mat(cv::Point3f((
float)markerFrame.
imgGray.cols, 0, 1));
1491 cv::Mat ll = cv::Mat(cv::Point3f(0, (
float)markerFrame.
imgGray.rows, 1));
1492 cv::Mat lr = cv::Mat(cv::Point3f((
float)markerFrame.
imgGray.cols, (
float)markerFrame.
imgGray.rows, 1));
1494 cv::Mat ulKf1, urKf1, llKf1, lrKf1, ulKf2, urKf2, llKf2, lrKf2;
1495 cv::Mat ul3D, ur3D, ll3D, lr3D;
1498 for (
int i1 = 0; i1 < kfs.size() - 1; i1++)
1502 if (kfCand1->
isBad())
continue;
1505 cv::Mat homography1;
1511 ulKf1 = homography1 * ul;
1512 ulKf1 /= ulKf1.at<
float>(2, 0);
1513 urKf1 = homography1 * ur;
1514 urKf1 /= urKf1.at<
float>(2, 0);
1515 llKf1 = homography1 * ll;
1516 llKf1 /= llKf1.at<
float>(2, 0);
1517 lrKf1 = homography1 * lr;
1518 lrKf1 /= lrKf1.at<
float>(2, 0);
1520 for (
int i2 = i1 + 1; i2 < kfs.size(); i2++)
1524 if (kfCand2->
isBad())
continue;
1526 cv::Mat homography2;
1532 ulKf2 = homography2 * ul;
1533 ulKf2 /= ulKf2.at<
float>(2, 0);
1534 urKf2 = homography2 * ur;
1535 urKf2 /= urKf2.at<
float>(2, 0);
1536 llKf2 = homography2 * ll;
1537 llKf2 /= llKf2.at<
float>(2, 0);
1538 lrKf2 = homography2 * lr;
1539 lrKf2 /= lrKf2.at<
float>(2, 0);
1543 cv::Mat Rwc1 = Rcw1.t();
1545 cv::Mat Tcw1(3, 4, CV_32F);
1546 Rcw1.copyTo(Tcw1.colRange(0, 3));
1547 tcw1.copyTo(Tcw1.col(3));
1549 const float& fx1 = kfCand1->
fx;
1550 const float& fy1 = kfCand1->
fy;
1551 const float& cx1 = kfCand1->
cx;
1552 const float& cy1 = kfCand1->
cy;
1553 const float& invfx1 = kfCand1->
invfx;
1554 const float& invfy1 = kfCand1->
invfy;
1557 cv::Mat Rwc2 = Rcw2.t();
1559 cv::Mat Tcw2(3, 4, CV_32F);
1560 Rcw2.copyTo(Tcw2.colRange(0, 3));
1561 tcw2.copyTo(Tcw2.col(3));
1563 const float& fx2 = kfCand2->
fx;
1564 const float& fy2 = kfCand2->
fy;
1565 const float& cx2 = kfCand2->
cx;
1566 const float& cy2 = kfCand2->
cy;
1567 const float& invfx2 = kfCand2->
invfx;
1568 const float& invfy2 = kfCand2->
invfy;
1571 cv::Mat ul1 = (cv::Mat_<float>(3, 1) << (ulKf1.at<
float>(0, 0) - cx1) * invfx1, (ulKf1.at<
float>(1, 0) - cy1) * invfy1, 1.0);
1572 cv::Mat ul2 = (cv::Mat_<float>(3, 1) << (ulKf2.at<
float>(0, 0) - cx2) * invfx2, (ulKf2.at<
float>(1, 0) - cy2) * invfy2, 1.0);
1575 cv::Mat A(4, 4, CV_32F);
1576 A.row(0) = ul1.at<
float>(0) * Tcw1.row(2) - Tcw1.row(0);
1577 A.row(1) = ul1.at<
float>(1) * Tcw1.row(2) - Tcw1.row(1);
1578 A.row(2) = ul2.at<
float>(0) * Tcw2.row(2) - Tcw2.row(0);
1579 A.row(3) = ul2.at<
float>(1) * Tcw2.row(2) - Tcw2.row(1);
1582 cv::SVD::compute(A, w, u, vt, cv::SVD::MODIFY_A | cv::SVD::FULL_UV);
1584 ul3D = vt.row(3).t();
1586 if (ul3D.at<
float>(3) != 0)
1589 ul3D = ul3D.rowRange(0, 3) / ul3D.at<
float>(3);
1594 cv::Mat ur1 = (cv::Mat_<float>(3, 1) << (urKf1.at<
float>(0, 0) - cx1) * invfx1, (urKf1.at<
float>(1, 0) - cy1) * invfy1, 1.0);
1595 cv::Mat ur2 = (cv::Mat_<float>(3, 1) << (urKf2.at<
float>(0, 0) - cx2) * invfx2, (urKf2.at<
float>(1, 0) - cy2) * invfy2, 1.0);
1598 cv::Mat A(4, 4, CV_32F);
1599 A.row(0) = ur1.at<
float>(0) * Tcw1.row(2) - Tcw1.row(0);
1600 A.row(1) = ur1.at<
float>(1) * Tcw1.row(2) - Tcw1.row(1);
1601 A.row(2) = ur2.at<
float>(0) * Tcw2.row(2) - Tcw2.row(0);
1602 A.row(3) = ur2.at<
float>(1) * Tcw2.row(2) - Tcw2.row(1);
1605 cv::SVD::compute(A, w, u, vt, cv::SVD::MODIFY_A | cv::SVD::FULL_UV);
1607 ur3D = vt.row(3).t();
1609 if (ur3D.at<
float>(3) != 0)
1612 ur3D = ur3D.rowRange(0, 3) / ur3D.at<
float>(3);
1617 cv::Mat ll1 = (cv::Mat_<float>(3, 1) << (llKf1.at<
float>(0, 0) - cx1) * invfx1, (llKf1.at<
float>(1, 0) - cy1) * invfy1, 1.0);
1618 cv::Mat ll2 = (cv::Mat_<float>(3, 1) << (llKf2.at<
float>(0, 0) - cx2) * invfx2, (llKf2.at<
float>(1, 0) - cy2) * invfy2, 1.0);
1621 cv::Mat A(4, 4, CV_32F);
1622 A.row(0) = ll1.at<
float>(0) * Tcw1.row(2) - Tcw1.row(0);
1623 A.row(1) = ll1.at<
float>(1) * Tcw1.row(2) - Tcw1.row(1);
1624 A.row(2) = ll2.at<
float>(0) * Tcw2.row(2) - Tcw2.row(0);
1625 A.row(3) = ll2.at<
float>(1) * Tcw2.row(2) - Tcw2.row(1);
1628 cv::SVD::compute(A, w, u, vt, cv::SVD::MODIFY_A | cv::SVD::FULL_UV);
1630 ll3D = vt.row(3).t();
1632 if (ll3D.at<
float>(3) != 0)
1635 ll3D = ll3D.rowRange(0, 3) / ll3D.at<
float>(3);
1640 cv::Mat lr1 = (cv::Mat_<float>(3, 1) << (lrKf1.at<
float>(0, 0) - cx1) * invfx1, (lrKf1.at<
float>(1, 0) - cy1) * invfy1, 1.0);
1641 cv::Mat lr2 = (cv::Mat_<float>(3, 1) << (lrKf2.at<
float>(0, 0) - cx2) * invfx2, (lrKf2.at<
float>(1, 0) - cy2) * invfy2, 1.0);
1644 cv::Mat A(4, 4, CV_32F);
1645 A.row(0) = lr1.at<
float>(0) * Tcw1.row(2) - Tcw1.row(0);
1646 A.row(1) = lr1.at<
float>(1) * Tcw1.row(2) - Tcw1.row(1);
1647 A.row(2) = lr2.at<
float>(0) * Tcw2.row(2) - Tcw2.row(0);
1648 A.row(3) = lr2.at<
float>(1) * Tcw2.row(2) - Tcw2.row(1);
1651 cv::SVD::compute(A, w, u, vt, cv::SVD::MODIFY_A | cv::SVD::FULL_UV);
1653 lr3D = vt.row(3).t();
1655 if (lr3D.at<
float>(3) != 0)
1658 lr3D = lr3D.rowRange(0, 3) / lr3D.at<
float>(3);
1668 cv::Vec3f vn = vAB.cross(vAC);
1671 cv::Mat AD = lr3D - ul3D;
1674 float d = (float)cv::norm(vn.dot(vAD)) / (
float)cv::norm(vn);
1677 matchedKf1 = kfCand1;
1678 matchedKf2 = kfCand2;
1686 if (matchedKf2)
break;
1689 if (!matchedKf1 || !matchedKf2)
1697 cv::Mat system = cv::Mat::zeros(3, 3, CV_32F);
1698 AC.copyTo(system.rowRange(0, 3).col(0));
1699 AB.copyTo(system.rowRange(0, 3).col(1));
1700 n.copyTo(system.rowRange(0, 3).col(2));
1702 cv::Mat systemInv = system.inv();
1704 for (
int i = 0; i < mapPoints.size(); i++)
1708 if (mp->
isBad())
continue;
1710 cv::Mat sol = systemInv * (mp->
GetWorldPos() - ul3D);
1712 if (sol.at<
float>(0, 0) < 0 || sol.at<
float>(0, 0) > 1 ||
1713 sol.at<
float>(1, 0) < 0 || sol.at<
float>(1, 0) > 1 ||
1714 sol.at<
float>(2, 0) < -0.1f || sol.at<
float>(2, 0) > 0.1f)
1720 for (
int i = 0; i < kfs.size(); i++)
1724 if (kf->
mnId == 0 || kf->
isBad())
continue;
1729 for (
int j = 0; j < mps.size(); j++)
1733 if (!mp || mp->
isBad())
continue;
1744 cv::Mat systemNorm = cv::Mat::zeros(3, 3, CV_32F);
1745 systemNorm.rowRange(0, 3).col(0) = system.rowRange(0, 3).col(1) / cv::norm(AB);
1746 systemNorm.rowRange(0, 3).col(1) = system.rowRange(0, 3).col(0) / cv::norm(AC);
1747 systemNorm.rowRange(0, 3).col(2) = system.rowRange(0, 3).col(2) / cv::norm(n);
1749 cv::Mat systemNormInv = systemNorm.inv();
1751 nodeTransform = cv::Mat::eye(4, 4, CV_32F);
1752 cv::Mat ul3Dinv = -systemNormInv * ul3D;
1753 ul3Dinv.copyTo(nodeTransform.rowRange(0, 3).col(3));
1754 systemNormInv.copyTo(nodeTransform.rowRange(0, 3).colRange(0, 3));
1756 cv::Mat scaleMat = cv::Mat::eye(4, 4, CV_32F);
1757 float markerWidthInRef = (float)cv::norm(ul3D - ur3D);
1758 float scaleFactor = markerWidthInM / markerWidthInRef;
1759 scaleMat.at<
float>(0, 0) = scaleFactor;
1760 scaleMat.at<
float>(1, 1) = scaleFactor;
1761 scaleMat.at<
float>(2, 2) = scaleFactor;
1763 nodeTransform = scaleMat * nodeTransform;
1770 std::stack<WAIKeyFrame*> nodeStack;
1774 while (!nodeStack.empty())
1779 if (visitedNode.count(k) == 0)
1781 visitedNode.insert(k);
1797 unsigned int countGood = 0;
1811 if ((kf->GetParent() ==
nullptr || kf->GetParent()->isBad()) && kf->mnId != 0)
1813 std::cout <<
"kf " << kf->
mnId <<
" has no parent" << std::endl;
1817 std::set<WAIKeyFrame*> visitedNode;
1819 if (root ==
nullptr)
1821 std::cout <<
"no root kf" << std::endl;
1827 std::cout <<
"keyframe graph has a cycle" << std::endl;
1830 if (visitedNode.size() != countGood)
1832 std::cout <<
"keyframe graph is split" << std::endl;
1833 std::cout <<
"visitedNode.size() = " << visitedNode.size() <<
" != number of good keyframe = " << countGood << std::endl;
#define AVERAGE_TIMING_STOP(name)
#define AVERAGE_TIMING_START(name)
cv::Mat GetCameraCenter()
std::vector< WAIMapPoint * > mvpMapPoints
bool isInFrustum(WAIMapPoint *pMP, float viewingCosLimit)
std::vector< cv::KeyPoint > mvKeys
static long unsigned int nNextId
WAIKeyFrame * mpReferenceKF
cv::Mat GetRotationInverse()
cv::Mat GetTranslationCW()
std::vector< cv::KeyPoint > mvKeysUn
void SetPose(cv::Mat Tcw)
static bool mbInitialComputations
std::vector< WAIKeyFrame * > DetectRelocalizationCandidates(WAIFrame *F, float minCommonWordFactor, bool applyMinAccScoreFilter=false)
float ComputeSceneMedianDepth(const int q)
void SetPose(const cv::Mat &Tcw)
std::vector< WAIKeyFrame * > GetBestCovisibilityKeyFrames(const int &N)
void AddMapPoint(WAIMapPoint *pMP, size_t idx)
WAIKeyFrame * GetParent()
void FindAndUpdateConnections(bool buildSpanningTree=true)
std::set< WAIKeyFrame * > GetChilds()
const std::vector< cv::KeyPoint > mvKeysUn
long unsigned int mnMarker[7]
std::vector< WAIMapPoint * > GetMapPointMatches()
void ComputeBoW(WAIOrbVocabulary *vocabulary)
static long unsigned int nNextId
long unsigned int KeyFramesInMap()
vector< WAIKeyFrame * > mvpKeyFrameOrigins
void AddKeyFrame(WAIKeyFrame *pKF)
std::mutex mMutexMapUpdate
void SetReferenceMapPoints(const std::vector< WAIMapPoint * > &vpMPs)
std::vector< WAIKeyFrame * > GetAllKeyFrames()
WAIKeyFrameDB * GetKeyFrameDB()
void AddMapPoint(WAIMapPoint *pMP)
std::vector< WAIMapPoint * > GetAllMapPoints()
void AddObservation(WAIKeyFrame *pKF, size_t idx)
static long unsigned int nNextId
std::map< WAIKeyFrame *, size_t > GetObservations()
void UpdateNormalAndDepth()
long unsigned int mnLastFrameSeen
void IncreaseVisible(int n=1)
void ComputeDistinctiveDescriptors()
void SetWorldPos(const cv::Mat &Pos)
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
std::vector< int > iniMatches
Initializer * initializer
std::vector< cv::Point2f > prevMatched
std::vector< cv::Point3f > iniPoint3D