mirror of
https://github.com/introlab/rtabmap.git
synced 2026-09-01 17:10:26 +08:00
Integration of viso2 odometry approach.
This commit is contained in:
@@ -61,6 +61,7 @@ SET(SRC_FILES
|
||||
OdometryMono.cpp
|
||||
OdometryF2F.cpp
|
||||
OdometryFovis.cpp
|
||||
OdometryViso2.cpp
|
||||
|
||||
Stereo.cpp
|
||||
StereoDense.cpp
|
||||
@@ -287,6 +288,17 @@ IF(libfovis_FOUND)
|
||||
)
|
||||
ENDIF(libfovis_FOUND)
|
||||
|
||||
IF(libviso2_FOUND)
|
||||
SET(INCLUDE_DIRS
|
||||
${INCLUDE_DIRS}
|
||||
${libviso2_INCLUDE_DIRS}
|
||||
)
|
||||
SET(LIBRARIES
|
||||
${LIBRARIES}
|
||||
${libviso2_LIBRARIES}
|
||||
)
|
||||
ENDIF(libviso2_FOUND)
|
||||
|
||||
####################################
|
||||
# Generate resources files
|
||||
####################################
|
||||
|
||||
@@ -29,6 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include "rtabmap/core/Odometry.h"
|
||||
#include "rtabmap/core/OdometryF2F.h"
|
||||
#include "rtabmap/core/OdometryFovis.h"
|
||||
#include "rtabmap/core/OdometryViso2.h"
|
||||
#include "rtabmap/core/OdometryInfo.h"
|
||||
#include "rtabmap/core/util3d.h"
|
||||
#include "rtabmap/core/util3d_mapping.h"
|
||||
@@ -57,6 +58,9 @@ Odometry * Odometry::create(Odometry::Type & type, const ParametersMap & paramet
|
||||
Odometry * odometry = 0;
|
||||
switch(type)
|
||||
{
|
||||
case Odometry::kTypeViso2:
|
||||
odometry = new OdometryViso2(parameters);
|
||||
break;
|
||||
case Odometry::kTypeFovis:
|
||||
odometry = new OdometryFovis(parameters);
|
||||
break;
|
||||
|
||||
270
corelib/src/OdometryViso2.cpp
Normal file
270
corelib/src/OdometryViso2.cpp
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "rtabmap/core/OdometryViso2.h"
|
||||
#include "rtabmap/core/OdometryInfo.h"
|
||||
#include "rtabmap/core/util2d.h"
|
||||
#include "rtabmap/core/Version.h"
|
||||
#include "rtabmap/utilite/ULogger.h"
|
||||
#include "rtabmap/utilite/UTimer.h"
|
||||
#include "rtabmap/utilite/UStl.h"
|
||||
|
||||
#ifdef RTABMAP_VISO2
|
||||
#include <viso_stereo.h>
|
||||
|
||||
double computeFeatureFlow(const std::vector<Matcher::p_match>& matches)
|
||||
{
|
||||
double total_flow = 0.0;
|
||||
for (size_t i = 0; i < matches.size(); ++i)
|
||||
{
|
||||
double x_diff = matches[i].u1c - matches[i].u1p;
|
||||
double y_diff = matches[i].v1c - matches[i].v1p;
|
||||
total_flow += sqrt(x_diff * x_diff + y_diff * y_diff);
|
||||
}
|
||||
return total_flow / matches.size();
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace rtabmap {
|
||||
|
||||
OdometryViso2::OdometryViso2(const ParametersMap & parameters) :
|
||||
Odometry(parameters),
|
||||
viso2_(0),
|
||||
ref_frame_change_method_(0),
|
||||
ref_frame_inlier_threshold_(Parameters::defaultOdomVisKeyFrameThr()),
|
||||
ref_frame_motion_threshold_(5.0),
|
||||
lost_(false),
|
||||
keep_reference_frame_(false),
|
||||
reference_motion_(Transform::getIdentity())
|
||||
{
|
||||
Parameters::parse(parameters, Parameters::kOdomVisKeyFrameThr(), ref_frame_inlier_threshold_);
|
||||
}
|
||||
|
||||
OdometryViso2::~OdometryViso2()
|
||||
{
|
||||
#ifdef RTABMAP_VISO2
|
||||
if(viso2_)
|
||||
{
|
||||
delete viso2_;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void OdometryViso2::reset(const Transform & initialPose)
|
||||
{
|
||||
Odometry::reset(initialPose);
|
||||
#ifdef RTABMAP_VISO2
|
||||
if(viso2_)
|
||||
{
|
||||
delete viso2_;
|
||||
viso2_ = 0;
|
||||
}
|
||||
lost_ = false;
|
||||
reference_motion_.setIdentity();
|
||||
#endif
|
||||
}
|
||||
|
||||
// return not null transform if odometry is correctly computed
|
||||
Transform OdometryViso2::computeTransform(
|
||||
SensorData & data,
|
||||
const Transform & guess,
|
||||
OdometryInfo * info)
|
||||
{
|
||||
Transform t;
|
||||
#ifdef RTABMAP_VISO2
|
||||
//based on https://github.com/srv/viso2/blob/indigo/viso2_ros/src/stereo_odometer.cpp
|
||||
|
||||
UTimer timer;
|
||||
|
||||
if(!data.depthRaw().empty())
|
||||
{
|
||||
UERROR("viso2 odometry doesn't support RGB-D data, only stereo. Aborting odometry update...");
|
||||
return t;
|
||||
}
|
||||
|
||||
if(data.imageRaw().empty() ||
|
||||
data.imageRaw().rows != data.rightRaw().rows ||
|
||||
data.imageRaw().cols != data.rightRaw().cols)
|
||||
{
|
||||
UERROR("Not compatible left (%dx%d) or right (%dx%d) image.",
|
||||
data.imageRaw().rows,
|
||||
data.imageRaw().cols,
|
||||
data.rightRaw().rows,
|
||||
data.rightRaw().cols);
|
||||
return t;
|
||||
}
|
||||
|
||||
if(!(data.stereoCameraModel().isValidForProjection() &&
|
||||
data.stereoCameraModel().left().isValidForReprojection() &&
|
||||
data.stereoCameraModel().right().isValidForReprojection()))
|
||||
{
|
||||
UERROR("Invalid stereo camera model!");
|
||||
return t;
|
||||
}
|
||||
|
||||
cv::Mat leftGray;
|
||||
if(data.imageRaw().type() == CV_8UC3)
|
||||
{
|
||||
cv::cvtColor(data.imageRaw(), leftGray, CV_BGR2GRAY);
|
||||
}
|
||||
else if(data.imageRaw().type() == CV_8UC1)
|
||||
{
|
||||
leftGray = data.imageRaw();
|
||||
}
|
||||
else
|
||||
{
|
||||
UFATAL("Not supported color type!");
|
||||
}
|
||||
cv::Mat rightGray;
|
||||
if(data.rightRaw().type() == CV_8UC3)
|
||||
{
|
||||
cv::cvtColor(data.rightRaw(), rightGray, CV_BGR2GRAY);
|
||||
}
|
||||
else if(data.rightRaw().type() == CV_8UC1)
|
||||
{
|
||||
rightGray = data.rightRaw();
|
||||
}
|
||||
else
|
||||
{
|
||||
UFATAL("Not supported color type!");
|
||||
}
|
||||
|
||||
int32_t dims[] = {leftGray.cols, leftGray.rows, leftGray.cols};
|
||||
cv::Mat covariance;
|
||||
if(viso2_ == 0)
|
||||
{
|
||||
VisualOdometryStereo::parameters params;
|
||||
params.base = data.stereoCameraModel().baseline();
|
||||
params.calib.cu = data.stereoCameraModel().left().cx();
|
||||
params.calib.cv = data.stereoCameraModel().left().cy();
|
||||
params.calib.f = data.stereoCameraModel().left().fx();
|
||||
viso2_ = new VisualOdometryStereo(params);
|
||||
|
||||
viso2_->process(leftGray.data, rightGray.data, dims);
|
||||
t.setIdentity();
|
||||
covariance = cv::Mat::eye(6,6, CV_64FC1)*9999.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool success = viso2_->process(leftGray.data, rightGray.data, dims, lost_ || keep_reference_frame_);
|
||||
if (success)
|
||||
{
|
||||
Matrix motionViso = Matrix::inv(viso2_->getMotion());
|
||||
Transform motion(motionViso.val[0][0], motionViso.val[0][1], motionViso.val[0][2],motionViso.val[0][3],
|
||||
motionViso.val[1][0], motionViso.val[1][1], motionViso.val[1][2],motionViso.val[1][3],
|
||||
motionViso.val[2][0], motionViso.val[2][1], motionViso.val[2][2], motionViso.val[2][3]);
|
||||
Transform camera_motion;
|
||||
|
||||
if(lost_ || keep_reference_frame_)
|
||||
{
|
||||
camera_motion = reference_motion_.inverse() * motion;
|
||||
}
|
||||
else
|
||||
{
|
||||
camera_motion = motion;
|
||||
}
|
||||
reference_motion_ = motion; // store last motion as reference
|
||||
|
||||
t=camera_motion;
|
||||
|
||||
//based on values set in viso2_ros
|
||||
covariance = cv::Mat::eye(6,6, CV_64FC1);
|
||||
covariance.at<double>(0,0) = 0.002;
|
||||
covariance.at<double>(1,1) = 0.002;
|
||||
covariance.at<double>(2,2) = 0.05;
|
||||
covariance.at<double>(3,3) = 0.09;
|
||||
covariance.at<double>(4,4) = 0.09;
|
||||
covariance.at<double>(5,5) = 0.09;
|
||||
|
||||
lost_=false;
|
||||
}
|
||||
else
|
||||
{
|
||||
covariance = cv::Mat::eye(6,6, CV_64FC1)*9999.0;
|
||||
lost_ = true;
|
||||
}
|
||||
|
||||
if(success)
|
||||
{
|
||||
// Proceed depending on the reference frame change method
|
||||
if(ref_frame_change_method_==1)
|
||||
{
|
||||
// calculate current feature flow
|
||||
double feature_flow = computeFeatureFlow(viso2_->getMatches());
|
||||
keep_reference_frame_ = (feature_flow < ref_frame_motion_threshold_);
|
||||
}
|
||||
else
|
||||
{
|
||||
keep_reference_frame_ = ref_frame_inlier_threshold_==0 || viso2_->getNumberOfInliers() > ref_frame_inlier_threshold_;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
keep_reference_frame_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!t.isNull() && !t.isIdentity() && !data.stereoCameraModel().localTransform().isIdentity() && !data.stereoCameraModel().localTransform().isNull())
|
||||
{
|
||||
// from camera frame to base frame
|
||||
t = data.stereoCameraModel().localTransform() * t * data.stereoCameraModel().localTransform().inverse();
|
||||
}
|
||||
|
||||
if(info)
|
||||
{
|
||||
info->type = (int)kTypeViso2;
|
||||
info->keyFrameAdded = !keep_reference_frame_;
|
||||
info->matches = viso2_->getNumberOfMatches();
|
||||
info->inliers = viso2_->getNumberOfInliers();
|
||||
if(covariance.cols == 6 && covariance.rows == 6 && covariance.type() == CV_64FC1)
|
||||
{
|
||||
info->covariance = covariance;
|
||||
}
|
||||
|
||||
std::vector<Matcher::p_match> matches = viso2_->getMatches();
|
||||
info->refCorners.resize(matches.size());
|
||||
info->newCorners.resize(matches.size());
|
||||
info->cornerInliers.resize(matches.size());
|
||||
for (size_t i = 0; i < matches.size(); ++i)
|
||||
{
|
||||
info->refCorners[i].x = matches[i].u1c;
|
||||
info->refCorners[i].y = matches[i].v1c;
|
||||
info->newCorners[i].x = matches[i].u1p;
|
||||
info->newCorners[i].y = matches[i].v1p;
|
||||
info->cornerInliers[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
UINFO("Odom update time = %fs lost=%s", timer.elapsed(), lost_?"true":"false");
|
||||
|
||||
#else
|
||||
UERROR("RTAB-Map is not built with VISO2 support! Select another visual odometry approach.");
|
||||
#endif
|
||||
return t;
|
||||
}
|
||||
|
||||
} // namespace rtabmap
|
||||
Reference in New Issue
Block a user