2012-12-11 18:05:05 +00:00
/*
2016-07-17 21:57:10 -04:00
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
2014-08-11 17:00:55 +00:00
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.
*/
2012-12-11 18:05:05 +00:00
#include "rtabmap/core/Features2d.h"
2014-04-03 19:59:46 +00:00
#include "rtabmap/core/util3d.h"
2015-12-21 17:27:05 -05:00
#include "rtabmap/core/util3d_features.h"
#include "rtabmap/core/Stereo.h"
2016-02-15 19:35:24 -05:00
#include "rtabmap/core/util2d.h"
2013-04-30 20:16:01 +00:00
#include "rtabmap/utilite/UStl.h"
#include "rtabmap/utilite/UConversion.h"
#include "rtabmap/utilite/ULogger.h"
#include "rtabmap/utilite/UMath.h"
#include "rtabmap/utilite/ULogger.h"
#include "rtabmap/utilite/UTimer.h"
2012-12-11 18:05:05 +00:00
#include <opencv2/core/version.hpp>
2015-06-18 23:00:46 -04:00
#include <opencv2/opencv_modules.hpp>
2014-06-23 15:19:17 +00:00
2019-04-18 19:50:58 -04:00
#ifdef RTABMAP_ORB_OCTREE
#include "opencv/ORBextractor.h"
#endif
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
#include "superpoint_torch/SuperPoint.h"
#endif
2025-11-07 17:22:35 -08:00
#if defined(RTABMAP_TORCH) && defined(RTABMAP_PYTHON)
#include "superpoint_rpautrat/SuperpointRpautrat.h"
#endif
2020-04-16 17:59:45 -04:00
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_PYTHON
#include "python/PyDetector.h"
#endif
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-03-08 12:31:32 -05:00
#include "opencv/Orb.h"
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2015-06-18 23:00:46 -04:00
#include <opencv2/gpu/gpu.hpp>
2016-01-21 17:11:57 -05:00
#endif
2015-06-18 23:00:46 -04:00
#else
#include <opencv2/core/cuda.hpp>
2012-12-11 18:05:05 +00:00
#endif
2015-06-18 23:00:46 -04:00
#ifdef HAVE_OPENCV_NONFREE
#if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >=4
#include <opencv2/nonfree/gpu.hpp>
#include <opencv2/nonfree/features2d.hpp>
#endif
#endif
#ifdef HAVE_OPENCV_XFEATURES2D
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/xfeatures2d/cuda.hpp>
2014-11-21 23:37:23 +00:00
#endif
2020-05-03 14:05:14 -04:00
#ifdef HAVE_OPENCV_CUDAFEATURES2D
#include <opencv2/cudafeatures2d.hpp>
#endif
2024-09-13 14:22:54 -07:00
#ifdef HAVE_OPENCV_CUDAIMGPROC
#include <opencv2/cudaimgproc.hpp>
#endif
2012-12-11 18:05:05 +00:00
2019-08-14 12:09:56 -04:00
#ifdef RTABMAP_FASTCV
#include <fastcv.h>
#endif
2024-09-13 14:22:54 -07:00
#ifdef RTABMAP_CUDASIFT
#include <cudasift/cudaImage.h>
#include <cudasift/cudaSift.h>
#endif
2012-12-11 18:05:05 +00:00
namespace rtabmap {
2014-10-13 19:10:22 +00:00
void Feature2D :: filterKeypointsByDepth (
2014-04-03 19:59:46 +00:00
std :: vector < cv :: KeyPoint > & keypoints ,
const cv :: Mat & depth ,
2015-11-22 18:08:32 -05:00
float minDepth ,
2014-04-03 19:59:46 +00:00
float maxDepth )
{
cv :: Mat descriptors ;
2015-11-22 18:08:32 -05:00
filterKeypointsByDepth ( keypoints , descriptors , depth , minDepth , maxDepth );
2014-04-03 19:59:46 +00:00
}
2014-10-13 19:10:22 +00:00
void Feature2D :: filterKeypointsByDepth (
2014-04-03 19:59:46 +00:00
std :: vector < cv :: KeyPoint > & keypoints ,
cv :: Mat & descriptors ,
const cv :: Mat & depth ,
2015-11-22 18:08:32 -05:00
float minDepth ,
2014-04-03 19:59:46 +00:00
float maxDepth )
{
2015-11-22 18:08:32 -05:00
UASSERT ( minDepth >= 0.0f );
UASSERT ( maxDepth <= 0.0f || maxDepth > minDepth );
2015-10-24 17:03:44 -04:00
if ( ! depth . empty () && ( descriptors . empty () || descriptors . rows == ( int ) keypoints . size ()))
2014-04-03 19:59:46 +00:00
{
std :: vector < cv :: KeyPoint > output ( keypoints . size ());
std :: vector < int > indexes ( keypoints . size (), 0 );
int oi = 0 ;
2014-10-13 19:10:22 +00:00
bool isInMM = depth . type () == CV_16UC1 ;
2014-04-03 19:59:46 +00:00
for ( unsigned int i = 0 ; i < keypoints . size (); ++ i )
{
2014-10-13 19:10:22 +00:00
int u = int ( keypoints [ i ]. pt . x + 0.5f );
int v = int ( keypoints [ i ]. pt . y + 0.5f );
if ( u >= 0 && u < depth . cols && v >= 0 && v < depth . rows )
2014-04-03 19:59:46 +00:00
{
2014-10-13 19:10:22 +00:00
float d = isInMM ? ( float ) depth . at < uint16_t > ( v , u ) * 0.001f : depth . at < float > ( v , u );
2015-11-22 18:08:32 -05:00
if ( uIsFinite ( d ) && d > minDepth && ( maxDepth <= 0.0f || d < maxDepth ))
2014-10-13 19:10:22 +00:00
{
output [ oi ++ ] = keypoints [ i ];
indexes [ i ] = 1 ;
}
2014-04-03 19:59:46 +00:00
}
}
output . resize ( oi );
keypoints = output ;
2014-10-16 00:14:23 +00:00
if ( ! descriptors . empty () && ( int ) keypoints . size () != descriptors . rows )
{
if ( keypoints . size () == 0 )
{
descriptors = cv :: Mat ();
}
else
{
2014-10-28 04:16:36 +00:00
cv :: Mat newDescriptors (( int ) keypoints . size (), descriptors . cols , descriptors . type ());
2014-10-16 00:14:23 +00:00
int di = 0 ;
for ( unsigned int i = 0 ; i < indexes . size (); ++ i )
{
if ( indexes [ i ] == 1 )
{
if ( descriptors . type () == CV_32FC1 )
{
memcpy ( newDescriptors . ptr < float > ( di ++ ), descriptors . ptr < float > ( i ), descriptors . cols * sizeof ( float ));
}
else // CV_8UC1
{
memcpy ( newDescriptors . ptr < char > ( di ++ ), descriptors . ptr < char > ( i ), descriptors . cols * sizeof ( char ));
}
}
}
descriptors = newDescriptors ;
}
}
}
}
2020-04-30 15:07:32 -04:00
void Feature2D :: filterKeypointsByDepth (
std :: vector < cv :: KeyPoint > & keypoints ,
cv :: Mat & descriptors ,
std :: vector < cv :: Point3f > & keypoints3D ,
float minDepth ,
float maxDepth )
{
UDEBUG ( "" );
//remove all keypoints/descriptors with no valid 3D points
UASSERT ((( int ) keypoints . size () == descriptors . rows || descriptors . empty ()) &&
keypoints3D . size () == keypoints . size ());
std :: vector < cv :: KeyPoint > validKeypoints ( keypoints . size ());
std :: vector < cv :: Point3f > validKeypoints3D ( keypoints . size ());
cv :: Mat validDescriptors ( descriptors . size (), descriptors . type ());
int oi = 0 ;
float minDepthSqr = minDepth * minDepth ;
float maxDepthSqr = maxDepth * maxDepth ;
for ( unsigned int i = 0 ; i < keypoints3D . size (); ++ i )
{
cv :: Point3f & pt = keypoints3D [ i ];
if ( util3d :: isFinite ( pt ))
{
float distSqr = pt . x * pt . x + pt . y * pt . y + pt . z * pt . z ;
if ( distSqr >= minDepthSqr && ( maxDepthSqr == 0.0f || distSqr <= maxDepthSqr ))
{
validKeypoints [ oi ] = keypoints [ i ];
validKeypoints3D [ oi ] = pt ;
if ( ! descriptors . empty ())
{
descriptors . row ( i ). copyTo ( validDescriptors . row ( oi ));
}
++ oi ;
}
}
}
UDEBUG ( "Removed %d invalid 3D points" , ( int ) keypoints3D . size () - oi );
validKeypoints . resize ( oi );
validKeypoints3D . resize ( oi );
keypoints = validKeypoints ;
keypoints3D = validKeypoints3D ;
if ( ! descriptors . empty ())
{
descriptors = validDescriptors . rowRange ( 0 , oi ). clone ();
}
}
2014-10-16 00:14:23 +00:00
void Feature2D :: filterKeypointsByDisparity (
std :: vector < cv :: KeyPoint > & keypoints ,
const cv :: Mat & disparity ,
float minDisparity )
{
cv :: Mat descriptors ;
filterKeypointsByDisparity ( keypoints , descriptors , disparity , minDisparity );
}
void Feature2D :: filterKeypointsByDisparity (
std :: vector < cv :: KeyPoint > & keypoints ,
cv :: Mat & descriptors ,
const cv :: Mat & disparity ,
float minDisparity )
{
if ( ! disparity . empty () && minDisparity > 0.0f && ( descriptors . empty () || descriptors . rows == ( int ) keypoints . size ()))
{
std :: vector < cv :: KeyPoint > output ( keypoints . size ());
std :: vector < int > indexes ( keypoints . size (), 0 );
int oi = 0 ;
for ( unsigned int i = 0 ; i < keypoints . size (); ++ i )
{
int u = int ( keypoints [ i ]. pt . x + 0.5f );
int v = int ( keypoints [ i ]. pt . y + 0.5f );
if ( u >= 0 && u < disparity . cols && v >= 0 && v < disparity . rows )
{
float d = disparity . type () == CV_16SC1 ? float ( disparity . at < short > ( v , u )) / 16.0f : disparity . at < float > ( v , u );
if ( d != 0.0f && uIsFinite ( d ) && d >= minDisparity )
{
output [ oi ++ ] = keypoints [ i ];
indexes [ i ] = 1 ;
}
}
}
output . resize ( oi );
keypoints = output ;
2014-06-15 03:31:35 +00:00
if ( ! descriptors . empty () && ( int ) keypoints . size () != descriptors . rows )
2014-04-03 19:59:46 +00:00
{
if ( keypoints . size () == 0 )
{
descriptors = cv :: Mat ();
}
else
{
2014-10-28 04:16:36 +00:00
cv :: Mat newDescriptors (( int ) keypoints . size (), descriptors . cols , descriptors . type ());
2014-04-03 19:59:46 +00:00
int di = 0 ;
for ( unsigned int i = 0 ; i < indexes . size (); ++ i )
{
if ( indexes [ i ] == 1 )
{
2014-10-06 19:41:45 +00:00
if ( descriptors . type () == CV_32FC1 )
{
memcpy ( newDescriptors . ptr < float > ( di ++ ), descriptors . ptr < float > ( i ), descriptors . cols * sizeof ( float ));
}
else // CV_8UC1
{
memcpy ( newDescriptors . ptr < char > ( di ++ ), descriptors . ptr < char > ( i ), descriptors . cols * sizeof ( char ));
}
2014-04-03 19:59:46 +00:00
}
}
descriptors = newDescriptors ;
}
}
}
}
2024-05-20 09:21:00 +08:00
void Feature2D :: limitKeypoints ( std :: vector < cv :: KeyPoint > & keypoints , int maxKeypoints , const cv :: Size & imageSize , bool ssc )
2013-12-11 00:12:44 +00:00
{
cv :: Mat descriptors ;
2024-05-20 09:21:00 +08:00
limitKeypoints ( keypoints , descriptors , maxKeypoints , imageSize , ssc );
2013-12-11 00:12:44 +00:00
}
2024-05-20 09:21:00 +08:00
void Feature2D :: limitKeypoints ( std :: vector < cv :: KeyPoint > & keypoints , cv :: Mat & descriptors , int maxKeypoints , const cv :: Size & imageSize , bool ssc )
2017-01-03 21:05:00 -05:00
{
std :: vector < cv :: Point3f > keypoints3D ;
2024-05-20 09:21:00 +08:00
limitKeypoints ( keypoints , keypoints3D , descriptors , maxKeypoints , imageSize , ssc );
2017-01-03 21:05:00 -05:00
}
2024-05-20 09:21:00 +08:00
void Feature2D :: limitKeypoints ( std :: vector < cv :: KeyPoint > & keypoints , std :: vector < cv :: Point3f > & keypoints3D , cv :: Mat & descriptors , int maxKeypoints , const cv :: Size & imageSize , bool ssc )
2013-12-11 00:12:44 +00:00
{
2014-10-13 19:10:22 +00:00
UASSERT_MSG (( int ) keypoints . size () == descriptors . rows || descriptors . rows == 0 , uFormat ( "keypoints=%d descriptors=%d" , ( int ) keypoints . size (), descriptors . rows ). c_str ());
2017-01-04 17:49:59 -05:00
UASSERT_MSG ( keypoints . size () == keypoints3D . size () || keypoints3D . size () == 0 , uFormat ( "keypoints=%d keypoints3D=%d" , ( int ) keypoints . size (), ( int ) keypoints3D . size ()). c_str ());
2013-12-11 00:12:44 +00:00
if ( maxKeypoints > 0 && ( int ) keypoints . size () > maxKeypoints )
{
UTimer timer ;
2024-05-20 09:21:00 +08:00
int removed ;
std :: vector < cv :: KeyPoint > kptsTmp ;
2021-01-09 20:52:34 -05:00
std :: vector < cv :: Point3f > kpts3DTmp ;
2013-12-11 00:12:44 +00:00
cv :: Mat descriptorsTmp ;
2024-05-20 09:21:00 +08:00
if ( ssc )
2013-12-11 00:12:44 +00:00
{
2026-01-17 18:21:16 -08:00
ULOGGER_DEBUG ( "too many words (%d), removing words with SSC" , keypoints . size ());
2024-10-27 05:44:22 +08:00
// Sorting keypoints by deacreasing order of strength
std :: vector < float > responseVector ;
for ( unsigned int i = 0 ; i < keypoints . size (); i ++ )
{
responseVector . push_back ( keypoints [ i ]. response );
}
std :: vector < int > indx ( responseVector . size ());
std :: iota ( std :: begin ( indx ), std :: end ( indx ), 0 );
#if CV_MAJOR_VERSION >= 4
cv :: sortIdx ( responseVector , indx , cv :: SORT_DESCENDING );
#else
cv :: sortIdx ( responseVector , indx , CV_SORT_DESCENDING );
#endif
2024-05-20 09:21:00 +08:00
static constexpr float tolerance = 0.1 ;
2024-10-27 05:44:22 +08:00
auto ResultVec = util2d :: SSC ( keypoints , maxKeypoints , tolerance , imageSize . width , imageSize . height , indx );
2024-05-20 09:21:00 +08:00
removed = keypoints . size () - ResultVec . size ();
// retrieve final keypoints
kptsTmp . resize ( ResultVec . size ());
if ( ! keypoints3D . empty ())
2017-01-03 21:05:00 -05:00
{
2024-05-20 09:21:00 +08:00
kpts3DTmp . resize ( ResultVec . size ());
2017-01-03 21:05:00 -05:00
}
2013-12-11 00:12:44 +00:00
if ( descriptors . rows )
{
2024-05-20 09:21:00 +08:00
descriptorsTmp = cv :: Mat ( ResultVec . size (), descriptors . cols , descriptors . type ());
}
for ( unsigned int k = 0 ; k < ResultVec . size (); ++ k )
{
kptsTmp [ k ] = keypoints [ ResultVec [ k ]];
if ( keypoints3D . size ())
2014-10-06 19:41:45 +00:00
{
2024-05-20 09:21:00 +08:00
kpts3DTmp [ k ] = keypoints3D [ ResultVec [ k ]];
2014-10-06 19:41:45 +00:00
}
2024-05-20 09:21:00 +08:00
if ( descriptors . rows )
2014-10-06 19:41:45 +00:00
{
2024-05-20 09:21:00 +08:00
if ( descriptors . type () == CV_32FC1 )
{
memcpy ( descriptorsTmp . ptr < float > ( k ), descriptors . ptr < float > ( ResultVec [ k ]), descriptors . cols * sizeof ( float ));
}
else
{
memcpy ( descriptorsTmp . ptr < char > ( k ), descriptors . ptr < char > ( ResultVec [ k ]), descriptors . cols * sizeof ( char ));
}
2014-10-06 19:41:45 +00:00
}
2013-12-11 00:12:44 +00:00
}
}
2024-05-20 09:21:00 +08:00
else
{
2024-09-13 14:22:54 -07:00
ULOGGER_DEBUG ( "too many words (%d), removing words with the hessian threshold" , keypoints . size ());
2024-05-20 09:21:00 +08:00
// Remove words under the new hessian threshold
// Sort words by hessian
std :: multimap < float , int > hessianMap ; // <hessian,id>
for ( unsigned int i = 0 ; i < keypoints . size (); ++ i )
{
//Keep track of the data, to be easier to manage the data in the next step
hessianMap . insert ( std :: pair < float , int > ( fabs ( keypoints [ i ]. response ), i ));
}
// Remove them from the signature
removed = ( int ) hessianMap . size () - maxKeypoints ;
std :: multimap < float , int >:: reverse_iterator iter = hessianMap . rbegin ();
kptsTmp . resize ( maxKeypoints );
if ( ! keypoints3D . empty ())
{
kpts3DTmp . resize ( maxKeypoints );
}
if ( descriptors . rows )
{
descriptorsTmp = cv :: Mat ( maxKeypoints , descriptors . cols , descriptors . type ());
}
for ( unsigned int k = 0 ; k < kptsTmp . size () && iter != hessianMap . rend (); ++ k , ++ iter )
{
kptsTmp [ k ] = keypoints [ iter -> second ];
if ( keypoints3D . size ())
{
kpts3DTmp [ k ] = keypoints3D [ iter -> second ];
}
if ( descriptors . rows )
{
if ( descriptors . type () == CV_32FC1 )
{
memcpy ( descriptorsTmp . ptr < float > ( k ), descriptors . ptr < float > ( iter -> second ), descriptors . cols * sizeof ( float ));
}
else
{
memcpy ( descriptorsTmp . ptr < char > ( k ), descriptors . ptr < char > ( iter -> second ), descriptors . cols * sizeof ( char ));
}
}
}
}
ULOGGER_DEBUG ( "%d keypoints removed, (kept %d), minimum response=%f" , removed , ( int ) kptsTmp . size (), ! ssc && kptsTmp . size () ? kptsTmp . back (). response : 0.0f );
2013-12-11 00:12:44 +00:00
ULOGGER_DEBUG ( "removing words time = %f s" , timer . ticks ());
keypoints = kptsTmp ;
2017-01-03 21:05:00 -05:00
keypoints3D = kpts3DTmp ;
2013-12-11 00:12:44 +00:00
if ( descriptors . rows )
{
descriptors = descriptorsTmp ;
}
}
}
2012-12-11 18:05:05 +00:00
2024-05-20 09:21:00 +08:00
void Feature2D :: limitKeypoints ( const std :: vector < cv :: KeyPoint > & keypoints , std :: vector < bool > & inliers , int maxKeypoints , const cv :: Size & imageSize , bool ssc )
2017-02-15 19:07:10 -05:00
{
if ( maxKeypoints > 0 && ( int ) keypoints . size () > maxKeypoints )
{
UTimer timer ;
float minimumHessian = 0.0f ;
2024-05-20 09:21:00 +08:00
int removed ;
inliers . resize ( keypoints . size (), false );
if ( ssc )
2017-02-15 19:07:10 -05:00
{
2026-01-17 18:21:16 -08:00
ULOGGER_DEBUG ( "too many words (%d), removing words with SSC" , keypoints . size ());
2024-10-27 05:44:22 +08:00
// Sorting keypoints by deacreasing order of strength
std :: vector < float > responseVector ;
for ( unsigned int i = 0 ; i < keypoints . size (); i ++ )
{
responseVector . push_back ( keypoints [ i ]. response );
}
std :: vector < int > indx ( responseVector . size ());
std :: iota ( std :: begin ( indx ), std :: end ( indx ), 0 );
#if CV_MAJOR_VERSION >= 4
cv :: sortIdx ( responseVector , indx , cv :: SORT_DESCENDING );
#else
cv :: sortIdx ( responseVector , indx , CV_SORT_DESCENDING );
#endif
2024-05-20 09:21:00 +08:00
static constexpr float tolerance = 0.1 ;
2024-10-27 05:44:22 +08:00
auto ResultVec = util2d :: SSC ( keypoints , maxKeypoints , tolerance , imageSize . width , imageSize . height , indx );
2024-05-20 09:21:00 +08:00
removed = keypoints . size () - ResultVec . size ();
for ( unsigned int k = 0 ; k < ResultVec . size (); ++ k )
{
inliers [ ResultVec [ k ]] = true ;
}
}
else
{
ULOGGER_DEBUG ( "too much words (%d), removing words with the hessian threshold" , keypoints . size ());
// Remove words under the new hessian threshold
// Sort words by hessian
std :: multimap < float , int > hessianMap ; // <hessian,id>
for ( unsigned int i = 0 ; i < keypoints . size (); ++ i )
{
//Keep track of the data, to be easier to manage the data in the next step
hessianMap . insert ( std :: pair < float , int > ( fabs ( keypoints [ i ]. response ), i ));
}
// Keep keypoints with highest response
removed = ( int ) hessianMap . size () - maxKeypoints ;
std :: multimap < float , int >:: reverse_iterator iter = hessianMap . rbegin ();
for ( int k = 0 ; k < maxKeypoints && iter != hessianMap . rend (); ++ k , ++ iter )
{
inliers [ iter -> second ] = true ;
minimumHessian = iter -> first ;
}
2017-02-15 19:07:10 -05:00
}
2026-03-28 18:47:46 -07:00
ULOGGER_DEBUG ( "%d keypoints removed, (kept %d), minimum response=%f" , removed , keypoints . size () - removed , minimumHessian );
2017-02-15 19:07:10 -05:00
ULOGGER_DEBUG ( "filter keypoints time = %f s" , timer . ticks ());
}
else
{
2020-05-31 11:22:12 -04:00
ULOGGER_DEBUG ( "keeping all %d keypoints" , ( int ) keypoints . size ());
2017-02-15 19:07:10 -05:00
inliers . resize ( keypoints . size (), true );
}
}
2024-05-20 09:21:00 +08:00
void Feature2D :: limitKeypoints ( const std :: vector < cv :: KeyPoint > & keypoints , std :: vector < bool > & inliers , int maxKeypoints , const cv :: Size & imageSize , int gridRows , int gridCols , bool ssc )
2020-05-31 11:22:12 -04:00
{
if ( maxKeypoints <= 0 || ( int ) keypoints . size () <= maxKeypoints )
{
inliers . resize ( keypoints . size (), true );
return ;
}
UASSERT ( gridCols >= 1 && gridRows >= 1 );
UASSERT ( imageSize . height > gridRows && imageSize . width > gridCols );
int rowSize = imageSize . height / gridRows ;
int colSize = imageSize . width / gridCols ;
int maxKeypointsPerCell = maxKeypoints / ( gridRows * gridCols );
std :: vector < std :: vector < cv :: KeyPoint > > keypointsPerCell ( gridRows * gridCols );
std :: vector < std :: vector < int > > indexesPerCell ( gridRows * gridCols );
for ( size_t i = 0 ; i < keypoints . size (); ++ i )
{
int cellRow = int ( keypoints [ i ]. pt . y ) / rowSize ;
int cellCol = int ( keypoints [ i ]. pt . x ) / colSize ;
UASSERT ( cellRow >= 0 && cellRow < gridRows );
UASSERT ( cellCol >= 0 && cellCol < gridCols );
keypointsPerCell [ cellRow * gridCols + cellCol ]. push_back ( keypoints [ i ]);
indexesPerCell [ cellRow * gridCols + cellCol ]. push_back ( i );
}
inliers . resize ( keypoints . size (), false );
for ( size_t i = 0 ; i < keypointsPerCell . size (); ++ i )
{
std :: vector < bool > inliersCell ;
2024-05-20 09:21:00 +08:00
limitKeypoints ( keypointsPerCell [ i ], inliersCell , maxKeypointsPerCell , cv :: Size ( colSize , rowSize ), ssc );
2020-05-31 11:22:12 -04:00
for ( size_t j = 0 ; j < inliersCell . size (); ++ j )
{
if ( inliersCell [ j ])
{
inliers . at ( indexesPerCell [ i ][ j ]) = true ;
}
}
}
}
2014-10-13 19:10:22 +00:00
cv :: Rect Feature2D :: computeRoi ( const cv :: Mat & image , const std :: string & roiRatios )
{
2016-08-21 19:33:01 -04:00
return util2d :: computeRoi ( image , roiRatios );
2014-10-13 19:10:22 +00:00
}
cv :: Rect Feature2D :: computeRoi ( const cv :: Mat & image , const std :: vector < float > & roiRatios )
2012-12-11 18:05:05 +00:00
{
2016-08-21 19:33:01 -04:00
return util2d :: computeRoi ( image , roiRatios );
2012-12-11 18:05:05 +00:00
}
2014-06-22 03:33:56 +00:00
/////////////////////
// Feature2D
/////////////////////
2015-04-23 22:01:41 -04:00
Feature2D :: Feature2D ( const ParametersMap & parameters ) :
2015-12-21 17:27:05 -05:00
maxFeatures_ ( Parameters :: defaultKpMaxFeatures ()),
2024-05-20 09:21:00 +08:00
SSC_ ( Parameters :: defaultKpSSC ()),
2016-01-13 09:20:15 -05:00
_maxDepth ( Parameters :: defaultKpMaxDepth ()),
_minDepth ( Parameters :: defaultKpMinDepth ()),
2015-12-21 17:27:05 -05:00
_roiRatios ( std :: vector < float > ( 4 , 0.0f )),
_subPixWinSize ( Parameters :: defaultKpSubPixWinSize ()),
_subPixIterations ( Parameters :: defaultKpSubPixIterations ()),
2017-11-30 16:52:03 -05:00
_subPixEps ( Parameters :: defaultKpSubPixEps ()),
gridRows_ ( Parameters :: defaultKpGridRows ()),
gridCols_ ( Parameters :: defaultKpGridCols ())
2015-04-23 22:01:41 -04:00
{
2015-12-21 17:27:05 -05:00
_stereo = new Stereo ( parameters );
2015-04-29 00:02:10 -04:00
this -> parseParameters ( parameters );
2015-04-23 22:01:41 -04:00
}
2015-12-21 17:27:05 -05:00
Feature2D ::~ Feature2D ()
{
delete _stereo ;
}
2015-04-23 22:01:41 -04:00
void Feature2D :: parseParameters ( const ParametersMap & parameters )
{
2016-01-04 11:41:13 -05:00
uInsert ( parameters_ , parameters );
2015-12-21 17:27:05 -05:00
Parameters :: parse ( parameters , Parameters :: kKpMaxFeatures (), maxFeatures_ );
2024-05-20 09:21:00 +08:00
Parameters :: parse ( parameters , Parameters :: kKpSSC (), SSC_ );
2016-01-13 09:20:15 -05:00
Parameters :: parse ( parameters , Parameters :: kKpMaxDepth (), _maxDepth );
Parameters :: parse ( parameters , Parameters :: kKpMinDepth (), _minDepth );
2015-12-21 17:27:05 -05:00
Parameters :: parse ( parameters , Parameters :: kKpSubPixWinSize (), _subPixWinSize );
Parameters :: parse ( parameters , Parameters :: kKpSubPixIterations (), _subPixIterations );
Parameters :: parse ( parameters , Parameters :: kKpSubPixEps (), _subPixEps );
2017-11-30 16:52:03 -05:00
Parameters :: parse ( parameters , Parameters :: kKpGridRows (), gridRows_ );
Parameters :: parse ( parameters , Parameters :: kKpGridCols (), gridCols_ );
UASSERT ( gridRows_ >= 1 && gridCols_ >= 1 );
2015-12-21 17:27:05 -05:00
// convert ROI from string to vector
ParametersMap :: const_iterator iter ;
if (( iter = parameters . find ( Parameters :: kKpRoiRatios ())) != parameters . end ())
{
std :: list < std :: string > strValues = uSplit ( iter -> second , ' ' );
if ( strValues . size () != 4 )
{
ULOGGER_ERROR ( "The number of values must be 4 (roi= \" %s \" )" , iter -> second . c_str ());
}
else
{
std :: vector < float > tmpValues ( 4 );
unsigned int i = 0 ;
2016-01-05 15:44:34 -05:00
for ( std :: list < std :: string >:: iterator jter = strValues . begin (); jter != strValues . end (); ++ jter )
2015-12-21 17:27:05 -05:00
{
2016-01-05 15:44:34 -05:00
tmpValues [ i ] = uStr2Float ( * jter );
2015-12-21 17:27:05 -05:00
++ i ;
}
if ( tmpValues [ 0 ] >= 0 && tmpValues [ 0 ] < 1 && tmpValues [ 0 ] < 1.0f - tmpValues [ 1 ] &&
tmpValues [ 1 ] >= 0 && tmpValues [ 1 ] < 1 && tmpValues [ 1 ] < 1.0f - tmpValues [ 0 ] &&
tmpValues [ 2 ] >= 0 && tmpValues [ 2 ] < 1 && tmpValues [ 2 ] < 1.0f - tmpValues [ 3 ] &&
tmpValues [ 3 ] >= 0 && tmpValues [ 3 ] < 1 && tmpValues [ 3 ] < 1.0f - tmpValues [ 2 ])
{
_roiRatios = tmpValues ;
}
else
{
ULOGGER_ERROR ( "The roi ratios are not valid (roi= \" %s \" )" , iter -> second . c_str ());
}
}
}
//stereo
UASSERT ( _stereo != 0 );
if (( iter = parameters . find ( Parameters :: kStereoOpticalFlow ())) != parameters . end ())
{
delete _stereo ;
2016-01-04 11:41:13 -05:00
_stereo = Stereo :: create ( parameters_ );
2015-12-21 17:27:05 -05:00
}
else
{
_stereo -> parseParameters ( parameters );
}
2015-04-23 22:01:41 -04:00
}
2015-12-17 14:13:19 -05:00
Feature2D * Feature2D :: create ( const ParametersMap & parameters )
{
int type = Parameters :: defaultKpDetectorStrategy ();
Parameters :: parse ( parameters , Parameters :: kKpDetectorStrategy (), type );
return create (( Feature2D :: Type ) type , parameters );
}
2015-12-11 17:44:15 -05:00
Feature2D * Feature2D :: create ( Feature2D :: Type type , const ParametersMap & parameters )
2014-10-13 19:10:22 +00:00
{
2020-05-09 21:13:41 -04:00
2020-11-01 11:28:59 -05:00
// NONFREE checks
2020-07-02 16:29:23 -04:00
#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION <= 3) || (CV_MAJOR_VERSION == 3 && (CV_MINOR_VERSION < 4 || (CV_MINOR_VERSION==4 && CV_SUBMINOR_VERSION<11)))
2020-11-01 11:28:59 -05:00
#ifndef RTABMAP_NONFREE
if ( type == Feature2D :: kFeatureSurf || type == Feature2D :: kFeatureSift || type == Feature2D :: kFeatureSurfFreak || type == Feature2D :: kFeatureSurfDaisy )
2014-11-21 23:37:23 +00:00
{
2020-11-01 11:28:59 -05:00
#if CV_MAJOR_VERSION < 3
2019-10-13 19:04:11 -04:00
UWARN ( "SURF and SIFT features cannot be used because OpenCV was not built with nonfree module. GFTT/ORB is used instead." );
2020-11-01 11:28:59 -05:00
#else
2019-10-13 19:04:11 -04:00
UWARN ( "SURF and SIFT features cannot be used because OpenCV was not built with xfeatures2d module. GFTT/ORB is used instead." );
2020-11-01 11:28:59 -05:00
#endif
2019-10-13 19:04:11 -04:00
type = Feature2D :: kFeatureGfttOrb ;
2014-11-21 23:37:23 +00:00
}
2020-11-01 11:28:59 -05:00
#endif
2015-06-18 23:00:46 -04:00
2020-07-02 16:29:23 -04:00
#else // >= 4.4.0 >= 3.4.11
2020-05-09 21:13:41 -04:00
2020-11-01 11:28:59 -05:00
#ifndef RTABMAP_NONFREE
if ( type == Feature2D :: kFeatureSurf )
2020-05-09 21:13:41 -04:00
{
2020-05-25 17:29:22 -04:00
UWARN ( "SURF features cannot be used because OpenCV was not built with nonfree module. SIFT is used instead." );
2020-05-09 21:13:41 -04:00
type = Feature2D :: kFeatureSift ;
}
2020-11-01 11:28:59 -05:00
else if ( type == Feature2D :: kFeatureSurfFreak || type == Feature2D :: kFeatureSurfDaisy )
{
UWARN ( "SURF detector cannot be used because OpenCV was not built with nonfree module. GFTT/ORB is used instead." );
type = Feature2D :: kFeatureGfttOrb ;
}
#endif
2020-05-09 21:13:41 -04:00
2020-07-02 16:29:23 -04:00
#endif // >= 4.4.0 >= 3.4.11
2020-05-09 21:13:41 -04:00
2020-11-01 11:28:59 -05:00
#if !defined(HAVE_OPENCV_XFEATURES2D) && CV_MAJOR_VERSION >= 3
if ( type == Feature2D :: kFeatureFastBrief ||
type == Feature2D :: kFeatureFastFreak ||
type == Feature2D :: kFeatureGfttBrief ||
type == Feature2D :: kFeatureGfttFreak ||
type == Feature2D :: kFeatureSurfFreak ||
type == Feature2D :: kFeatureGfttDaisy ||
type == Feature2D :: kFeatureSurfDaisy )
{
UWARN ( "BRIEF, FREAK and DAISY features cannot be used because OpenCV was not built with xfeatures2d module. GFTT/ORB is used instead." );
type = Feature2D :: kFeatureGfttOrb ;
}
#elif CV_MAJOR_VERSION < 3
2017-07-25 16:20:05 -04:00
if ( type == Feature2D :: kFeatureKaze )
2017-02-05 17:24:45 -05:00
{
2020-11-01 11:28:59 -05:00
#ifdef RTABMAP_NONFREE
2017-07-25 16:20:05 -04:00
UWARN ( "KAZE detector/descriptor can be used only with OpenCV3. SURF is used instead." );
type = Feature2D :: kFeatureSurf ;
2020-11-01 11:28:59 -05:00
#else
2019-10-13 19:04:11 -04:00
UWARN ( "KAZE detector/descriptor can be used only with OpenCV3. GFTT/ORB is used instead." );
type = Feature2D :: kFeatureGfttOrb ;
2020-11-01 11:28:59 -05:00
#endif
}
if ( type == Feature2D :: kFeatureGfttDaisy || type == Feature2D :: kFeatureSurfDaisy )
{
UWARN ( "DAISY detector/descriptor can be used only with OpenCV3. GFTT/BRIEF is used instead." );
type = Feature2D :: kFeatureGfttBrief ;
2017-02-05 17:24:45 -05:00
}
#endif
2020-11-01 11:28:59 -05:00
2019-04-18 19:50:58 -04:00
#ifndef RTABMAP_ORB_OCTREE
if ( type == Feature2D :: kFeatureOrbOctree )
{
2019-10-13 19:04:11 -04:00
UWARN ( "ORB OcTree feature cannot be used as RTAB-Map is not built with the option enabled. GFTT/ORB is used instead." );
type = Feature2D :: kFeatureGfttOrb ;
2019-04-18 19:50:58 -04:00
}
#endif
2021-01-17 01:56:27 -05:00
#ifndef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
if ( type == Feature2D :: kFeatureSuperPointTorch )
{
2025-06-15 22:39:52 +03:00
UWARN ( "SuperPoint Torch feature cannot be used as RTAB-Map is not built with the option enabled. GFTT/ORB is used instead." );
2020-04-16 17:59:45 -04:00
type = Feature2D :: kFeatureGfttOrb ;
}
#endif
2014-10-13 19:10:22 +00:00
Feature2D * feature2D = 0 ;
switch ( type )
{
2014-11-21 23:37:23 +00:00
case Feature2D :: kFeatureSurf :
feature2D = new SURF ( parameters );
break ;
2014-10-13 19:10:22 +00:00
case Feature2D :: kFeatureSift :
feature2D = new SIFT ( parameters );
break ;
2014-11-21 23:37:23 +00:00
case Feature2D :: kFeatureOrb :
feature2D = new ORB ( parameters );
break ;
2014-10-13 19:10:22 +00:00
case Feature2D :: kFeatureFastBrief :
feature2D = new FAST_BRIEF ( parameters );
break ;
case Feature2D :: kFeatureFastFreak :
feature2D = new FAST_FREAK ( parameters );
break ;
case Feature2D :: kFeatureGfttFreak :
feature2D = new GFTT_FREAK ( parameters );
break ;
case Feature2D :: kFeatureGfttBrief :
feature2D = new GFTT_BRIEF ( parameters );
break ;
2015-11-25 19:56:14 -05:00
case Feature2D :: kFeatureGfttOrb :
feature2D = new GFTT_ORB ( parameters );
break ;
2014-10-13 19:10:22 +00:00
case Feature2D :: kFeatureBrisk :
feature2D = new BRISK ( parameters );
break ;
2017-07-25 16:20:05 -04:00
case Feature2D :: kFeatureKaze :
feature2D = new KAZE ( parameters );
break ;
2019-04-18 19:50:58 -04:00
case Feature2D :: kFeatureOrbOctree :
feature2D = new ORBOctree ( parameters );
break ;
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2025-11-07 17:22:35 -08:00
case Feature2D :: kFeatureSuperPointTorch :
feature2D = new SuperPointTorch ( parameters );
break ;
#endif
#if defined(RTABMAP_TORCH) && defined(RTABMAP_PYTHON)
case Feature2D :: kFeatureSuperPointRpautrat :
feature2D = new SuperPointRpautrat ( parameters );
break ;
2020-04-16 17:59:45 -04:00
#endif
2020-09-28 10:29:14 -04:00
case Feature2D :: kFeatureSurfFreak :
feature2D = new SURF_FREAK ( parameters );
break ;
2020-11-01 11:28:59 -05:00
case Feature2D :: kFeatureGfttDaisy :
feature2D = new GFTT_DAISY ( parameters );
break ;
case Feature2D :: kFeatureSurfDaisy :
feature2D = new SURF_DAISY ( parameters );
break ;
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_PYTHON
case Feature2D :: kFeaturePyDetector :
feature2D = new PyDetector ( parameters );
break ;
#endif
2016-03-01 14:25:00 -05:00
#ifdef RTABMAP_NONFREE
2014-10-13 19:10:22 +00:00
default :
feature2D = new SURF ( parameters );
type = Feature2D :: kFeatureSurf ;
break ;
2014-11-21 23:37:23 +00:00
#else
default :
feature2D = new ORB ( parameters );
2020-11-01 11:28:59 -05:00
type = Feature2D :: kFeatureGfttOrb ;
2014-11-21 23:37:23 +00:00
break ;
#endif
2014-10-13 19:10:22 +00:00
}
return feature2D ;
}
2015-11-26 13:16:01 -05:00
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > Feature2D :: generateKeypoints ( const cv :: Mat & image , const cv :: Mat & maskIn )
2012-12-11 18:05:05 +00:00
{
2015-12-21 17:27:05 -05:00
UASSERT ( ! image . empty ());
UASSERT ( image . type () == CV_8UC1 );
2016-01-13 09:20:15 -05:00
cv :: Mat mask ;
2016-06-27 11:54:21 -04:00
if ( ! maskIn . empty ())
2016-01-13 09:20:15 -05:00
{
2016-06-27 11:54:21 -04:00
if ( maskIn . type () == CV_16UC1 || maskIn . type () == CV_32FC1 )
2016-01-13 09:20:15 -05:00
{
2016-06-27 11:54:21 -04:00
mask = cv :: Mat :: zeros ( maskIn . rows , maskIn . cols , CV_8UC1 );
for ( int i = 0 ; i < ( int ) mask . total (); ++ i )
2016-02-19 18:10:32 -05:00
{
2016-06-27 11:54:21 -04:00
float value = 0.0f ;
if ( maskIn . type () == CV_16UC1 )
2016-02-19 18:10:32 -05:00
{
2016-06-27 11:54:21 -04:00
if ((( unsigned short * ) maskIn . data )[ i ] > 0 &&
(( unsigned short * ) maskIn . data )[ i ] < std :: numeric_limits < unsigned short >:: max ())
{
value = float ((( unsigned short * ) maskIn . data )[ i ]) * 0.001f ;
}
}
else
{
value = (( float * ) maskIn . data )[ i ];
}
if ( value > _minDepth &&
2019-08-16 16:38:25 -04:00
( _maxDepth == 0.0f || value <= _maxDepth ) &&
uIsFinite ( value ))
2016-06-27 11:54:21 -04:00
{
(( unsigned char * ) mask . data )[ i ] = 255 ; // ORB uses 255 to handle pyramids
2016-02-19 18:10:32 -05:00
}
}
2016-06-27 11:54:21 -04:00
}
else if ( maskIn . type () == CV_8UC1 )
{
// assume a standard mask
mask = maskIn ;
}
else
{
UERROR ( "Wrong mask type (%d)! Should be 8UC1, 16UC1 or 32FC1." , maskIn . type ());
2016-01-13 09:20:15 -05:00
}
}
UASSERT ( mask . empty () || ( mask . cols == image . cols && mask . rows == image . rows ));
2012-12-11 18:05:05 +00:00
std :: vector < cv :: KeyPoint > keypoints ;
2015-12-21 17:27:05 -05:00
UTimer timer ;
2017-11-30 16:52:03 -05:00
cv :: Rect globalRoi = Feature2D :: computeRoi ( image , _roiRatios );
if ( ! ( globalRoi . width && globalRoi . height ))
{
globalRoi = cv :: Rect ( 0 , 0 , image . cols , image . rows );
}
2015-12-21 17:27:05 -05:00
// Get keypoints
2017-11-30 16:52:03 -05:00
int rowSize = globalRoi . height / gridRows_ ;
int colSize = globalRoi . width / gridCols_ ;
2020-05-31 11:22:12 -04:00
int maxFeatures = maxFeatures_ / ( gridRows_ * gridCols_ );
2017-11-30 16:52:03 -05:00
for ( int i = 0 ; i < gridRows_ ; ++ i )
2012-12-11 18:05:05 +00:00
{
2017-11-30 16:52:03 -05:00
for ( int j = 0 ; j < gridCols_ ; ++ j )
2014-06-22 03:33:56 +00:00
{
2017-11-30 16:52:03 -05:00
cv :: Rect roi ( globalRoi . x + j * colSize , globalRoi . y + i * rowSize , colSize , rowSize );
2023-10-27 04:15:34 +08:00
std :: vector < cv :: KeyPoint > subKeypoints ;
subKeypoints = this -> generateKeypointsImpl ( image , roi , mask );
2025-11-07 17:22:35 -08:00
if ( this -> getType () != Feature2D :: Type :: kFeaturePyDetector && this -> getType () != Feature2D :: Type :: kFeatureSuperPointRpautrat )
2024-02-27 00:21:15 +01:00
{
2024-05-20 09:21:00 +08:00
limitKeypoints ( subKeypoints , maxFeatures , roi . size (), this -> getSSC ());
2024-02-27 00:21:15 +01:00
}
2017-11-30 16:52:03 -05:00
if ( roi . x || roi . y )
{
// Adjust keypoint position to raw image
2023-10-27 04:15:34 +08:00
for ( std :: vector < cv :: KeyPoint >:: iterator iter = subKeypoints . begin (); iter != subKeypoints . end (); ++ iter )
2017-11-30 16:52:03 -05:00
{
iter -> pt . x += roi . x ;
iter -> pt . y += roi . y ;
}
}
2023-10-27 04:15:34 +08:00
keypoints . insert ( keypoints . end (), subKeypoints . begin (), subKeypoints . end () );
2014-06-22 03:33:56 +00:00
}
2012-12-11 18:05:05 +00:00
}
2020-05-31 11:22:12 -04:00
UDEBUG ( "Keypoints extraction time = %f s, keypoints extracted = %d (grid=%dx%d, mask empty=%d)" ,
timer . ticks (), keypoints . size (), gridCols_ , gridRows_ , mask . empty () ? 1 : 0 );
2015-12-21 17:27:05 -05:00
2016-03-08 12:31:32 -05:00
if ( keypoints . size () && _subPixWinSize > 0 && _subPixIterations > 0 )
2012-12-11 18:05:05 +00:00
{
2015-12-21 17:27:05 -05:00
std :: vector < cv :: Point2f > corners ;
cv :: KeyPoint :: convert ( keypoints , corners );
cv :: cornerSubPix ( image , corners ,
cv :: Size ( _subPixWinSize , _subPixWinSize ),
cv :: Size ( - 1 , - 1 ),
2026-07-29 22:48:39 -07:00
cv :: TermCriteria ( cv :: TermCriteria :: MAX_ITER | cv :: TermCriteria :: EPS , _subPixIterations , _subPixEps ) );
2015-12-21 17:27:05 -05:00
for ( unsigned int i = 0 ; i < corners . size (); ++ i )
{
keypoints [ i ]. pt = corners [ i ];
}
2016-03-08 12:31:32 -05:00
UDEBUG ( "subpixel time = %f s" , timer . ticks ());
2012-12-11 18:05:05 +00:00
}
2014-06-22 03:33:56 +00:00
return keypoints ;
}
2015-12-21 17:27:05 -05:00
cv :: Mat Feature2D :: generateDescriptors (
const cv :: Mat & image ,
std :: vector < cv :: KeyPoint > & keypoints ) const
2014-06-22 03:33:56 +00:00
{
2017-04-22 16:56:38 -04:00
cv :: Mat descriptors ;
if ( keypoints . size ())
{
UASSERT ( ! image . empty ());
UASSERT ( image . type () == CV_8UC1 );
descriptors = generateDescriptorsImpl ( image , keypoints );
2026-06-01 21:04:08 -07:00
if ( descriptors . rows != ( int ) keypoints . size ())
{
UWARN ( "Descriptor extraction returned %d rows for %d keypoints — "
"clearing keypoints to keep them in sync." ,
descriptors . rows , ( int ) keypoints . size ());
keypoints . clear ();
descriptors = cv :: Mat ();
}
else {
UDEBUG ( "Descriptors extracted = %d, remaining kpts=%d" , descriptors . rows , ( int ) keypoints . size ());
}
2017-04-22 16:56:38 -04:00
}
2014-10-13 19:10:22 +00:00
return descriptors ;
2014-06-22 03:33:56 +00:00
}
2015-12-21 17:27:05 -05:00
std :: vector < cv :: Point3f > Feature2D :: generateKeypoints3D (
const SensorData & data ,
const std :: vector < cv :: KeyPoint > & keypoints ) const
{
std :: vector < cv :: Point3f > keypoints3D ;
2017-04-22 16:56:38 -04:00
if ( keypoints . size ())
2015-12-21 17:27:05 -05:00
{
2022-07-20 15:20:14 -04:00
if ( ! data . rightRaw (). empty () && ! data . imageRaw (). empty () &&
! data . stereoCameraModels (). empty () &&
data . stereoCameraModels ()[ 0 ]. isValidForProjection ())
2015-12-21 17:27:05 -05:00
{
2017-04-22 16:56:38 -04:00
//stereo
2024-09-13 14:22:54 -07:00
cv :: Mat imageLeft = data . imageRaw ();
cv :: Mat imageRight = data . rightRaw ();
#ifdef HAVE_OPENCV_CUDEV
cv :: cuda :: GpuMat d_imageLeft ;
cv :: cuda :: GpuMat d_imageRight ;
if ( _stereo -> isGpuEnabled ())
2017-04-22 16:56:38 -04:00
{
2024-09-13 14:22:54 -07:00
d_imageLeft = data . imageRawGpu ();
if ( d_imageLeft . empty ()) {
d_imageLeft = cv :: cuda :: GpuMat ( imageLeft );
}
2025-03-08 16:40:04 -08:00
// convert to grayscale if not already
2024-09-13 14:22:54 -07:00
if ( d_imageLeft . channels () > 1 ) {
cv :: cuda :: GpuMat tmp ;
cv :: cuda :: cvtColor ( d_imageLeft , tmp , cv :: COLOR_BGR2GRAY );
d_imageLeft = tmp ;
}
2025-03-08 16:40:04 -08:00
2024-09-13 14:22:54 -07:00
d_imageRight = data . depthOrRightRawGpu ();
if ( d_imageRight . empty ()) {
d_imageRight = cv :: cuda :: GpuMat ( imageRight );
}
2025-03-08 16:40:04 -08:00
// convert to grayscale if not already
if ( d_imageRight . channels () > 1 ) {
cv :: cuda :: GpuMat tmp ;
cv :: cuda :: cvtColor ( d_imageRight , tmp , cv :: COLOR_BGR2GRAY );
d_imageRight = tmp ;
}
2017-04-22 16:56:38 -04:00
}
else
2024-09-13 14:22:54 -07:00
#endif
2017-04-22 16:56:38 -04:00
{
2025-03-08 16:40:04 -08:00
// convert to grayscale
2024-09-13 14:22:54 -07:00
if ( imageLeft . channels () > 1 )
{
cv :: cvtColor ( data . imageRaw (), imageLeft , cv :: COLOR_BGR2GRAY );
}
2025-03-08 16:40:04 -08:00
if ( imageRight . channels () > 1 )
{
cv :: cvtColor ( data . rightRaw (), imageRight , cv :: COLOR_BGR2GRAY );
}
2017-04-22 16:56:38 -04:00
}
std :: vector < cv :: Point2f > leftCorners ;
cv :: KeyPoint :: convert ( keypoints , leftCorners );
std :: vector < cv :: Point2f > rightCorners ;
2022-07-20 15:20:14 -04:00
if ( data . stereoCameraModels (). size () == 1 )
{
std :: vector < unsigned char > status ;
2024-09-13 14:22:54 -07:00
#ifdef HAVE_OPENCV_CUDEV
if ( _stereo -> isGpuEnabled ())
{
rightCorners = _stereo -> computeCorrespondences (
d_imageLeft ,
d_imageRight ,
leftCorners ,
status );
}
else
#endif
{
rightCorners = _stereo -> computeCorrespondences (
imageLeft ,
imageRight ,
leftCorners ,
status );
}
2022-07-20 15:20:14 -04:00
if ( ULogger :: level () >= ULogger :: kWarning )
{
int rejected = 0 ;
for ( size_t i = 0 ; i < status . size (); ++ i )
{
if ( status [ i ] == 0 )
{
++ rejected ;
}
}
if ( rejected > ( int ) status . size () / 2 )
{
UWARN ( "A large number (%d/%d) of stereo correspondences are rejected! "
"Optical flow may have failed because images are not calibrated, "
"the background is too far (no disparity between the images), "
"maximum disparity may be too small (%f) or that exposure between "
"left and right images is too different." ,
rejected ,
( int ) status . size (),
_stereo -> maxDisparity ());
}
}
keypoints3D = util3d :: generateKeypoints3DStereo (
leftCorners ,
rightCorners ,
data . stereoCameraModels ()[ 0 ],
status ,
_minDepth ,
_maxDepth );
}
else
{
2024-09-13 14:22:54 -07:00
int subImageWith = imageLeft . cols / data . stereoCameraModels (). size ();
UASSERT ( imageLeft . cols % subImageWith == 0 );
2022-07-20 15:20:14 -04:00
std :: vector < std :: vector < cv :: Point2f > > subLeftCorners ( data . stereoCameraModels (). size ());
std :: vector < std :: vector < int > > subIndex ( data . stereoCameraModels (). size ());
// Assign keypoints per camera
for ( size_t i = 0 ; i < leftCorners . size (); ++ i )
{
int cameraIndex = int ( leftCorners [ i ]. x / subImageWith );
leftCorners [ i ]. x -= cameraIndex * subImageWith ;
subLeftCorners [ cameraIndex ]. push_back ( leftCorners [ i ]);
subIndex [ cameraIndex ]. push_back ( i );
}
keypoints3D . resize ( keypoints . size ());
int total = 0 ;
int rejected = 0 ;
for ( size_t i = 0 ; i < data . stereoCameraModels (). size (); ++ i )
{
if ( ! subLeftCorners [ i ]. empty ())
{
std :: vector < unsigned char > status ;
2024-09-13 14:22:54 -07:00
#ifdef HAVE_OPENCV_CUDEV
if ( _stereo -> isGpuEnabled ())
{
rightCorners = _stereo -> computeCorrespondences (
d_imageLeft . colRange ( cv :: Range ( subImageWith * i , subImageWith * ( i + 1 ))),
d_imageRight . colRange ( cv :: Range ( subImageWith * i , subImageWith * ( i + 1 ))),
2022-07-20 15:20:14 -04:00
subLeftCorners [ i ],
status );
2024-09-13 14:22:54 -07:00
}
else
#endif
{
rightCorners = _stereo -> computeCorrespondences (
imageLeft . colRange ( cv :: Range ( subImageWith * i , subImageWith * ( i + 1 ))),
imageRight . colRange ( cv :: Range ( subImageWith * i , subImageWith * ( i + 1 ))),
subLeftCorners [ i ],
status );
}
2022-07-20 15:20:14 -04:00
std :: vector < cv :: Point3f > subKeypoints3D = util3d :: generateKeypoints3DStereo (
subLeftCorners [ i ],
rightCorners ,
data . stereoCameraModels ()[ i ],
status ,
_minDepth ,
_maxDepth );
if ( ULogger :: level () >= ULogger :: kWarning )
{
for ( size_t i = 0 ; i < status . size (); ++ i )
{
if ( status [ i ] == 0 )
{
++ rejected ;
}
}
total += status . size ();
}
UASSERT ( subIndex [ i ]. size () == subKeypoints3D . size ());
for ( size_t j = 0 ; j < subKeypoints3D . size (); ++ j )
{
keypoints3D [ subIndex [ i ][ j ]] = subKeypoints3D [ j ];
}
}
}
if ( ULogger :: level () >= ULogger :: kWarning )
{
if ( rejected > total / 2 )
{
UWARN ( "A large number (%d/%d) of stereo correspondences are rejected! "
"Optical flow may have failed because images are not calibrated, "
"the background is too far (no disparity between the images), "
"maximum disparity may be too small (%f) or that exposure between "
"left and right images is too different." ,
rejected ,
total ,
_stereo -> maxDisparity ());
}
}
}
2015-12-21 17:27:05 -05:00
}
2017-04-22 16:56:38 -04:00
else if ( ! data . depthRaw (). empty () && data . cameraModels (). size ())
2015-12-21 17:27:05 -05:00
{
2017-04-22 16:56:38 -04:00
keypoints3D = util3d :: generateKeypoints3DDepth (
keypoints ,
data . depthOrRightRaw (),
data . cameraModels (),
_minDepth ,
_maxDepth );
2015-12-21 17:27:05 -05:00
}
}
return keypoints3D ;
}
2014-06-22 03:33:56 +00:00
//////////////////////////
//SURF
//////////////////////////
SURF :: SURF ( const ParametersMap & parameters ) :
2014-08-11 15:23:06 +00:00
hessianThreshold_ ( Parameters :: defaultSURFHessianThreshold ()),
nOctaves_ ( Parameters :: defaultSURFOctaves ()),
nOctaveLayers_ ( Parameters :: defaultSURFOctaveLayers ()),
extended_ ( Parameters :: defaultSURFExtended ()),
upright_ ( Parameters :: defaultSURFUpright ()),
gpuKeypointsRatio_ ( Parameters :: defaultSURFGpuKeypointsRatio ()),
2015-06-18 23:00:46 -04:00
gpuVersion_ ( Parameters :: defaultSURFGpuVersion ())
2014-06-22 03:33:56 +00:00
{
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2012-12-11 18:05:05 +00:00
}
2014-06-22 03:33:56 +00:00
SURF ::~ SURF ()
2012-12-11 18:05:05 +00:00
{
}
2014-08-11 15:23:06 +00:00
void SURF :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-08-11 15:23:06 +00:00
Parameters :: parse ( parameters , Parameters :: kSURFExtended (), extended_ );
Parameters :: parse ( parameters , Parameters :: kSURFHessianThreshold (), hessianThreshold_ );
Parameters :: parse ( parameters , Parameters :: kSURFOctaveLayers (), nOctaveLayers_ );
Parameters :: parse ( parameters , Parameters :: kSURFOctaves (), nOctaves_ );
Parameters :: parse ( parameters , Parameters :: kSURFUpright (), upright_ );
Parameters :: parse ( parameters , Parameters :: kSURFGpuKeypointsRatio (), gpuKeypointsRatio_ );
Parameters :: parse ( parameters , Parameters :: kSURFGpuVersion (), gpuVersion_ );
2016-03-01 14:25:00 -05:00
#ifdef RTABMAP_NONFREE
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
if ( gpuVersion_ && cv :: gpu :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of SURF not available! Using CPU version instead..." );
gpuVersion_ = false ;
2014-08-11 15:23:06 +00:00
}
2015-06-18 23:00:46 -04:00
#else
if ( gpuVersion_ && cv :: cuda :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of SURF not available! Using CPU version instead..." );
gpuVersion_ = false ;
2014-08-11 15:23:06 +00:00
}
2015-06-18 23:00:46 -04:00
#endif
if ( gpuVersion_ )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
_gpuSurf = cv :: Ptr < CV_SURF_GPU > ( new CV_SURF_GPU ( hessianThreshold_ , nOctaves_ , nOctaveLayers_ , extended_ , gpuKeypointsRatio_ , upright_ ));
2014-08-11 15:23:06 +00:00
}
else
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
_surf = cv :: Ptr < CV_SURF > ( new CV_SURF ( hessianThreshold_ , nOctaves_ , nOctaveLayers_ , extended_ , upright_ ));
#else
_surf = CV_SURF :: create ( hessianThreshold_ , nOctaves_ , nOctaveLayers_ , extended_ , upright_ );
#endif
2014-08-11 15:23:06 +00:00
}
2014-11-21 23:37:23 +00:00
#else
2014-12-02 17:01:24 -05:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SURF cannot be used!" );
2014-11-21 23:37:23 +00:00
#endif
2014-08-11 15:23:06 +00:00
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > SURF :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2012-12-11 18:05:05 +00:00
{
2014-06-22 03:33:56 +00:00
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
2012-12-11 18:05:05 +00:00
std :: vector < cv :: KeyPoint > keypoints ;
2014-11-21 23:37:23 +00:00
2016-03-01 14:25:00 -05:00
#ifdef RTABMAP_NONFREE
2014-06-22 03:33:56 +00:00
cv :: Mat imgRoi ( image , roi );
2016-01-13 09:20:15 -05:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2015-06-18 23:00:46 -04:00
if ( gpuVersion_ )
2012-12-11 18:05:05 +00:00
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-06-22 03:33:56 +00:00
cv :: gpu :: GpuMat imgGpu ( imgRoi );
2016-01-13 09:20:15 -05:00
cv :: gpu :: GpuMat maskGpu ( maskRoi );
( * _gpuSurf . obj )( imgGpu , maskGpu , keypoints );
2015-06-18 23:00:46 -04:00
#else
cv :: cuda :: GpuMat imgGpu ( imgRoi );
2016-01-13 09:20:15 -05:00
cv :: cuda :: GpuMat maskGpu ( maskRoi );
( * _gpuSurf . get ())( imgGpu , maskGpu , keypoints );
2015-06-18 23:00:46 -04:00
#endif
2012-12-11 18:05:05 +00:00
}
else
{
2016-01-13 09:20:15 -05:00
_surf -> detect ( imgRoi , keypoints , maskRoi );
2012-12-11 18:05:05 +00:00
}
2014-11-21 23:37:23 +00:00
#else
2014-12-02 17:01:24 -05:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SURF cannot be used!" );
2014-11-21 23:37:23 +00:00
#endif
2012-12-11 18:05:05 +00:00
return keypoints ;
}
2014-06-22 03:33:56 +00:00
cv :: Mat SURF :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2016-03-01 14:25:00 -05:00
#ifdef RTABMAP_NONFREE
2015-06-18 23:00:46 -04:00
if ( gpuVersion_ )
2014-06-22 03:33:56 +00:00
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-06-22 03:33:56 +00:00
cv :: gpu :: GpuMat imgGpu ( image );
cv :: gpu :: GpuMat descriptorsGPU ;
2015-06-18 23:00:46 -04:00
( * _gpuSurf . obj )( imgGpu , cv :: gpu :: GpuMat (), keypoints , descriptorsGPU , true );
#else
cv :: cuda :: GpuMat imgGpu ( image );
cv :: cuda :: GpuMat descriptorsGPU ;
( * _gpuSurf . get ())( imgGpu , cv :: cuda :: GpuMat (), keypoints , descriptorsGPU , true );
#endif
2014-06-22 03:33:56 +00:00
// Download descriptors
if ( descriptorsGPU . empty ())
descriptors = cv :: Mat ();
else
{
UASSERT ( descriptorsGPU . type () == CV_32F );
descriptors = cv :: Mat ( descriptorsGPU . size (), CV_32F );
descriptorsGPU . download ( descriptors );
}
}
else
{
_surf -> compute ( image , keypoints , descriptors );
}
2014-11-21 23:37:23 +00:00
#else
2014-12-02 17:01:24 -05:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SURF cannot be used!" );
2014-11-21 23:37:23 +00:00
#endif
2014-06-22 03:33:56 +00:00
return descriptors ;
}
//////////////////////////
//SIFT
//////////////////////////
SIFT :: SIFT ( const ParametersMap & parameters ) :
2014-08-11 15:23:06 +00:00
nOctaveLayers_ ( Parameters :: defaultSIFTNOctaveLayers ()),
contrastThreshold_ ( Parameters :: defaultSIFTContrastThreshold ()),
edgeThreshold_ ( Parameters :: defaultSIFTEdgeThreshold ()),
2020-05-21 17:09:20 -04:00
sigma_ ( Parameters :: defaultSIFTSigma ()),
2024-09-13 14:22:54 -07:00
preciseUpscale_ ( Parameters :: defaultSIFTPreciseUpscale ()),
rootSIFT_ ( Parameters :: defaultSIFTRootSIFT ()),
gpu_ ( Parameters :: defaultSIFTGpu ()),
2026-03-28 18:47:46 -07:00
gaussianThreshold_ ( Parameters :: defaultSIFTGaussianThreshold ()),
maxGaussianThreshold_ ( Parameters :: defaultSIFTMaxGaussianThreshold ()),
2024-09-13 14:22:54 -07:00
upscale_ ( Parameters :: defaultSIFTUpscale ()),
cudaSiftData_ ( 0 ),
cudaSiftMemory_ ( 0 ),
cudaSiftUpscaling_ ( upscale_ )
2014-06-22 03:33:56 +00:00
{
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2014-06-22 03:33:56 +00:00
}
SIFT ::~ SIFT ()
{
2024-09-13 14:22:54 -07:00
#ifdef RTABMAP_CUDASIFT
if ( cudaSiftData_ ) {
FreeSiftData ( * cudaSiftData_ );
delete cudaSiftData_ ;
}
if ( cudaSiftMemory_ ) {
FreeSiftTempMemory ( cudaSiftMemory_ );
}
#endif
2014-06-22 03:33:56 +00:00
}
2014-08-11 15:23:06 +00:00
void SIFT :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-08-11 15:23:06 +00:00
Parameters :: parse ( parameters , Parameters :: kSIFTContrastThreshold (), contrastThreshold_ );
Parameters :: parse ( parameters , Parameters :: kSIFTEdgeThreshold (), edgeThreshold_ );
Parameters :: parse ( parameters , Parameters :: kSIFTNOctaveLayers (), nOctaveLayers_ );
Parameters :: parse ( parameters , Parameters :: kSIFTSigma (), sigma_ );
2024-09-13 14:22:54 -07:00
Parameters :: parse ( parameters , Parameters :: kSIFTPreciseUpscale (), preciseUpscale_ );
2020-05-21 17:09:20 -04:00
Parameters :: parse ( parameters , Parameters :: kSIFTRootSIFT (), rootSIFT_ );
2024-09-13 14:22:54 -07:00
Parameters :: parse ( parameters , Parameters :: kSIFTGpu (), gpu_ );
2026-03-28 18:47:46 -07:00
Parameters :: parse ( parameters , Parameters :: kSIFTGaussianThreshold (), gaussianThreshold_ );
Parameters :: parse ( parameters , Parameters :: kSIFTMaxGaussianThreshold (), maxGaussianThreshold_ );
2024-09-13 14:22:54 -07:00
Parameters :: parse ( parameters , Parameters :: kSIFTUpscale (), upscale_ );
if ( gpu_ )
{
#ifdef RTABMAP_CUDASIFT
2024-11-12 15:00:19 -08:00
// Check if there is a cuda device
2026-03-28 18:47:46 -07:00
if ( cudaSiftData_ == 0 )
{
if ( InitCuda ( 0 , ULogger :: level () == ULogger :: kDebug )) {
UDEBUG ( "Init SiftData" );
2024-11-12 15:00:19 -08:00
cudaSiftData_ = new SiftData ();
InitSiftData ( * cudaSiftData_ , 8192 , true , true );
}
2026-03-28 18:47:46 -07:00
else {
UWARN ( "No cuda device(s) detected, CudaSift is not available! Using SIFT CPU version instead." );
gpu_ = false ;
}
2024-09-13 14:22:54 -07:00
}
#else
UWARN ( "RTAB-Map is not built with CudaSift so %s cannot be used!" , Parameters :: kSIFTGpu (). c_str ());
gpu_ = false ;
#endif
}
if ( ! gpu_ )
{
#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION <= 3) || (CV_MAJOR_VERSION == 3 && (CV_MINOR_VERSION < 4 || (CV_MINOR_VERSION==4 && CV_SUBMINOR_VERSION<11)))
2016-03-01 14:25:00 -05:00
#ifdef RTABMAP_NONFREE
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2024-09-13 14:22:54 -07:00
sift_ = cv :: Ptr < CV_SIFT > ( new CV_SIFT ( this -> getMaxFeatures (), nOctaveLayers_ , contrastThreshold_ , edgeThreshold_ , sigma_ ));
2015-06-18 23:00:46 -04:00
#else
2024-09-13 14:22:54 -07:00
sift_ = CV_SIFT :: create ( this -> getMaxFeatures (), nOctaveLayers_ , contrastThreshold_ , edgeThreshold_ , sigma_ );
2015-06-18 23:00:46 -04:00
#endif
2014-11-21 23:37:23 +00:00
#else
2024-09-13 14:22:54 -07:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SIFT cannot be used!" );
2014-11-21 23:37:23 +00:00
#endif
2024-09-13 14:22:54 -07:00
#elif CV_MAJOR_VERSION>4 || (CV_MAJOR_VERSION==4 && CV_MINOR_VERSION>=8) // >=4.8
sift_ = CV_SIFT :: create ( this -> getMaxFeatures (), nOctaveLayers_ , contrastThreshold_ , edgeThreshold_ , sigma_ , preciseUpscale_ );
2020-10-17 20:34:24 -04:00
#else // >=4.4, >=3.4.11
2024-09-13 14:22:54 -07:00
sift_ = CV_SIFT :: create ( this -> getMaxFeatures (), nOctaveLayers_ , contrastThreshold_ , edgeThreshold_ , sigma_ );
2020-05-09 21:13:41 -04:00
#endif
2024-09-13 14:22:54 -07:00
}
2014-08-11 15:23:06 +00:00
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > SIFT :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2014-06-22 03:33:56 +00:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
cv :: Mat imgRoi ( image , roi );
2024-12-10 12:36:21 -08:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2024-09-13 14:22:54 -07:00
#ifdef RTABMAP_CUDASIFT
if ( gpu_ )
2016-01-13 09:20:15 -05:00
{
2024-09-13 14:22:54 -07:00
/* Read image using OpenCV and convert to floating point. */
int w = imgRoi . cols ;
int h = imgRoi . rows ;
cv :: Mat img_h ;
imgRoi . convertTo ( img_h , CV_32FC1 );
CudaImage img_d ;
img_d . Allocate ( w , h , iAlignUp ( w , 128 ), false , NULL , ( float * ) img_h . data );
img_d . Download ();
// Compute number of octaves like OpenCV based on resolution
// ref: https://github.com/opencv/opencv/blob/4d665419992dda6e40364f741ae4765176b64bb0/modules/features2d/src/sift.dispatch.cpp#L538
// *** stack smashing detected *** if "-2" term is higher
int numOctaves = cvRound ( std :: log ( ( double ) std :: min ( w * ( upscale_ ? 2 : 1 ), h * ( upscale_ ? 2 : 1 )) ) / std :: log ( 2. ) - ( upscale_ ? 3 : 2 ));
if ( numOctaves < 1 ) {
numOctaves = 1 ;
}
else if ( numOctaves > 7 )
{
numOctaves = 7 ; // hard-coded limit in CudaSift
}
float initBlur = sigma_ ; /* Amount of initial Gaussian blurring in standard deviations */
2026-03-28 18:47:46 -07:00
float thresh = gaussianThreshold_ ; /* Threshold on difference of Gaussians for feature pruning */
2024-09-13 14:22:54 -07:00
float edgeLimit = edgeThreshold_ ;
float minScale = 0.0f ; /* Minimum acceptable scale to remove fine-scale features */
UDEBUG ( "numOctaves=%d initBlur=%f thresh=%f edgeLimit=%f minScale=%f upScale=%s w=%d h=%d" , numOctaves , initBlur , thresh , edgeLimit , minScale , upscale_ ? "true" : "false" , w , h );
if ( cudaSiftMemory_ && ( cudaSiftMemorySize_ != cv :: Size ( w , h ) || cudaSiftUpscaling_ != upscale_ )) {
// Resolution changed, reset buffer
FreeSiftTempMemory ( cudaSiftMemory_ );
cudaSiftMemory_ = 0 ;
}
if ( cudaSiftMemory_ == 0 ) {
cudaSiftMemory_ = AllocSiftTempMemory ( w , h , numOctaves , upscale_ );
UASSERT ( cudaSiftMemory_ != 0 );
cudaSiftMemorySize_ = cv :: Size ( w , h );
cudaSiftUpscaling_ = upscale_ ;
}
ExtractSift ( * cudaSiftData_ , img_d , numOctaves , initBlur , thresh , edgeLimit , minScale , upscale_ , cudaSiftMemory_ );
UDEBUG ( "%d features extracted" , cudaSiftData_ -> numPts );
// Convert CudaSift into OpenCV format
cudaSiftDescriptors_ = cv :: Mat ();
if ( cudaSiftData_ -> numPts )
{
2026-03-28 18:47:46 -07:00
keypoints . resize ( cudaSiftData_ -> numPts );
cudaSiftDescriptors_ = cv :: Mat ( cudaSiftData_ -> numPts , 128 , CV_32FC1 );
size_t k = 0 ;
2024-09-13 14:22:54 -07:00
for ( int i = 0 ; i < cudaSiftData_ -> numPts ; ++ i )
{
// Ignore keypoints with invalid descriptors
float * desc = cudaSiftData_ -> h_data [ i ]. data ;
if ( desc [ 0 ] != 0 && desc [ 0 ] == desc [ 63 ] && desc [ 0 ] == desc [ 127 ])
{
//UWARN("Invalid decsriptor? skipping: %f,%f,%f", cudaSiftData_->h_data[i].xpos, cudaSiftData_->h_data[i].ypos, cudaSiftData_->h_data[i].scale);
//std::cout << cv::Mat(1, 128*4, CV_8UC1, desc) << std::endl;
continue ;
}
2024-12-10 12:36:21 -08:00
// Ignore keypoints not in the mask
if ( ! maskRoi . empty () && maskRoi . at < unsigned char > ( cudaSiftData_ -> h_data [ i ]. ypos , cudaSiftData_ -> h_data [ i ]. xpos ) == 0 )
{
continue ;
}
2026-03-28 18:47:46 -07:00
if ( i > 0 &&
cudaSiftData_ -> h_data [ i ]. subsampling == cudaSiftData_ -> h_data [ i - 1 ]. subsampling &&
fabs ( cudaSiftData_ -> h_data [ i ]. xpos - cudaSiftData_ -> h_data [ i - 1 ]. xpos ) +
fabs ( cudaSiftData_ -> h_data [ i ]. xpos - cudaSiftData_ -> h_data [ i - 1 ]. ypos ) < 0.1f )
{
// Same feature, skip doubles
continue ;
}
2024-09-13 14:22:54 -07:00
2026-03-28 18:47:46 -07:00
float response = abs ( cudaSiftData_ -> h_data [ i ]. sharpness );
if ( maxGaussianThreshold_ > gaussianThreshold_ && response > maxGaussianThreshold_ )
{
continue ;
}
2024-09-13 14:22:54 -07:00
cv :: Mat ( 1 , 128 , CV_32FC1 , desc ). copyTo ( cudaSiftDescriptors_ . row ( k ));
keypoints [ k ]. pt . x = cudaSiftData_ -> h_data [ i ]. xpos ;
keypoints [ k ]. pt . y = cudaSiftData_ -> h_data [ i ]. ypos ;
keypoints [ k ]. size = 2.0f * cudaSiftData_ -> h_data [ i ]. scale ; // x2 because the scale is more like a radius than a diameter, see CudaSift's ExtractSiftDescriptors function to see how they convert scale to patch size
keypoints [ k ]. angle = cudaSiftData_ -> h_data [ i ]. orientation ;
2026-03-28 18:47:46 -07:00
keypoints [ k ]. response = response ;
2024-09-13 14:22:54 -07:00
keypoints [ k ]. octave = log2 ( cudaSiftData_ -> h_data [ i ]. subsampling ) - ( upscale_ ? 1 : 0 );
2026-03-28 18:47:46 -07:00
++ k ;
}
if ( k < keypoints . size ())
{
UDEBUG ( "keypoints extracted = %d, valid=%d" , keypoints . size (), k );
keypoints . resize ( k );
cudaSiftDescriptors_ . resize ( k );
}
if ( this -> getMaxFeatures () != 0 && this -> getMaxFeatures () < ( int ) keypoints . size ())
{
// Call limitKeypoints() now to filter the descriptors.
this -> limitKeypoints ( keypoints , cudaSiftDescriptors_ , this -> getMaxFeatures (), cv :: Size ( w , h ), this -> getSSC ());
2024-09-13 14:22:54 -07:00
}
}
2016-01-13 09:20:15 -05:00
}
2024-09-13 14:22:54 -07:00
else
#endif
{
2020-10-17 20:34:24 -04:00
#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION <= 3) || (CV_MAJOR_VERSION == 3 && (CV_MINOR_VERSION < 4 || (CV_MINOR_VERSION==4 && CV_SUBMINOR_VERSION<11)))
#ifdef RTABMAP_NONFREE
2024-09-13 14:22:54 -07:00
sift_ -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
2014-11-21 23:37:23 +00:00
#else
2024-09-13 14:22:54 -07:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SIFT cannot be used!" );
2020-10-17 20:34:24 -04:00
#endif
#else // >=4.4, >=3.4.11
2024-09-13 14:22:54 -07:00
sift_ -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
2014-11-21 23:37:23 +00:00
#endif
2024-09-13 14:22:54 -07:00
}
2014-06-22 03:33:56 +00:00
return keypoints ;
}
cv :: Mat SIFT :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
2026-03-28 18:47:46 -07:00
cv :: Mat descriptors ;
2024-09-13 14:22:54 -07:00
#ifdef RTABMAP_CUDASIFT
if ( gpu_ )
{
if (( int ) keypoints . size () == cudaSiftDescriptors_ . rows )
{
2026-03-28 18:47:46 -07:00
descriptors = cudaSiftDescriptors_ . clone ();
2024-09-13 14:22:54 -07:00
}
else
{
UERROR ( "CudaSift: keypoints size %ld is not equal to extracted descriptors size %d" , keypoints . size (), cudaSiftDescriptors_ . rows );
return cv :: Mat ();
}
}
2026-03-28 18:47:46 -07:00
else
{
2024-09-13 14:22:54 -07:00
#endif
2026-03-28 18:47:46 -07:00
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
2020-10-17 20:34:24 -04:00
#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION <= 3) || (CV_MAJOR_VERSION == 3 && (CV_MINOR_VERSION < 4 || (CV_MINOR_VERSION==4 && CV_SUBMINOR_VERSION<11)))
#ifdef RTABMAP_NONFREE
2026-03-28 18:47:46 -07:00
sift_ -> compute ( image , keypoints , descriptors );
2020-10-17 20:34:24 -04:00
#else
2026-03-28 18:47:46 -07:00
UWARN ( "RTAB-Map is not built with OpenCV nonfree module so SIFT cannot be used!" );
2020-10-17 20:34:24 -04:00
#endif
#else // >=4.4, >=3.4.11
2026-03-28 18:47:46 -07:00
sift_ -> compute ( image , keypoints , descriptors );
2020-10-17 20:34:24 -04:00
#endif
2026-03-28 18:47:46 -07:00
#ifdef RTABMAP_CUDASIFT
}
#endif
2020-05-21 17:09:20 -04:00
if ( rootSIFT_ && ! descriptors . empty ())
{
UDEBUG ( "Performing RootSIFT..." );
// see http://www.pyimagesearch.com/2015/04/13/implementing-rootsift-in-python-and-opencv/
// apply the Hellinger kernel by first L1-normalizing and taking the
// square-root
for ( int i = 0 ; i < descriptors . rows ; ++ i )
{
// By taking the L1 norm, followed by the square-root, we have
// already L2 normalized the feature vector and further normalization
// is not needed.
descriptors . row ( i ) = descriptors . row ( i ) / cv :: sum ( descriptors . row ( i ))[ 0 ];
cv :: sqrt ( descriptors . row ( i ), descriptors . row ( i ));
}
}
2014-06-22 03:33:56 +00:00
return descriptors ;
}
//////////////////////////
//ORB
//////////////////////////
ORB :: ORB ( const ParametersMap & parameters ) :
2014-08-11 15:23:06 +00:00
scaleFactor_ ( Parameters :: defaultORBScaleFactor ()),
nLevels_ ( Parameters :: defaultORBNLevels ()),
edgeThreshold_ ( Parameters :: defaultORBEdgeThreshold ()),
firstLevel_ ( Parameters :: defaultORBFirstLevel ()),
WTA_K_ ( Parameters :: defaultORBWTA_K ()),
scoreType_ ( Parameters :: defaultORBScoreType ()),
patchSize_ ( Parameters :: defaultORBPatchSize ()),
gpu_ ( Parameters :: defaultORBGpu ()),
fastThreshold_ ( Parameters :: defaultFASTThreshold ()),
2015-06-18 23:00:46 -04:00
nonmaxSuppresion_ ( Parameters :: defaultFASTNonmaxSuppression ())
2014-06-22 03:33:56 +00:00
{
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2014-06-22 03:33:56 +00:00
}
ORB ::~ ORB ()
{
}
2014-08-11 15:23:06 +00:00
void ORB :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-08-11 15:23:06 +00:00
Parameters :: parse ( parameters , Parameters :: kORBScaleFactor (), scaleFactor_ );
Parameters :: parse ( parameters , Parameters :: kORBNLevels (), nLevels_ );
Parameters :: parse ( parameters , Parameters :: kORBEdgeThreshold (), edgeThreshold_ );
Parameters :: parse ( parameters , Parameters :: kORBFirstLevel (), firstLevel_ );
Parameters :: parse ( parameters , Parameters :: kORBWTA_K (), WTA_K_ );
Parameters :: parse ( parameters , Parameters :: kORBScoreType (), scoreType_ );
Parameters :: parse ( parameters , Parameters :: kORBPatchSize (), patchSize_ );
Parameters :: parse ( parameters , Parameters :: kORBGpu (), gpu_ );
Parameters :: parse ( parameters , Parameters :: kFASTThreshold (), fastThreshold_ );
Parameters :: parse ( parameters , Parameters :: kFASTNonmaxSuppression (), nonmaxSuppresion_ );
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2015-06-18 23:00:46 -04:00
if ( gpu_ && cv :: gpu :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of ORB not available! Using CPU version instead..." );
gpu_ = false ;
2014-08-11 15:23:06 +00:00
}
2016-01-21 17:11:57 -05:00
#else
if ( gpu_ )
{
UWARN ( "GPU version of ORB not available (OpenCV not built with gpu/cuda module)! Using CPU version instead..." );
gpu_ = false ;
}
#endif
2015-06-18 23:00:46 -04:00
#else
#ifndef HAVE_OPENCV_CUDAFEATURES2D
if ( gpu_ )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of ORB not available (OpenCV cudafeatures2d module)! Using CPU version instead..." );
gpu_ = false ;
2014-08-11 15:23:06 +00:00
}
2015-06-18 23:00:46 -04:00
#endif
2018-07-11 18:33:31 +02:00
if ( gpu_ && cv :: cuda :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2018-07-11 18:33:31 +02:00
UWARN ( "GPU version of ORB not available (no GPU found)! Using CPU version instead..." );
gpu_ = false ;
2015-06-18 23:00:46 -04:00
}
#endif
if ( gpu_ )
{
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2015-12-21 17:27:05 -05:00
_gpuOrb = cv :: Ptr < CV_ORB_GPU > ( new CV_ORB_GPU ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , edgeThreshold_ , firstLevel_ , WTA_K_ , scoreType_ , patchSize_ ));
2014-08-11 15:23:06 +00:00
_gpuOrb -> setFastParams ( fastThreshold_ , nonmaxSuppresion_ );
2016-01-21 17:11:57 -05:00
#else
UFATAL ( "not supposed to be here" );
#endif
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
2018-07-11 18:33:31 +02:00
_gpuOrb = CV_ORB_GPU :: create ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , edgeThreshold_ , firstLevel_ , WTA_K_ , scoreType_ , patchSize_ , fastThreshold_ );
2015-06-18 23:00:46 -04:00
#endif
#endif
2014-08-11 15:23:06 +00:00
}
else
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-03-08 12:31:32 -05:00
_orb = cv :: Ptr < CV_ORB > ( new CV_ORB ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , edgeThreshold_ , firstLevel_ , WTA_K_ , scoreType_ , patchSize_ , parameters ));
2018-10-21 13:35:03 -04:00
#elif CV_MAJOR_VERSION > 3
_orb = CV_ORB :: create ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , edgeThreshold_ , firstLevel_ , WTA_K_ , ( cv :: ORB :: ScoreType ) scoreType_ , patchSize_ , fastThreshold_ );
2015-06-18 23:00:46 -04:00
#else
2016-03-08 12:31:32 -05:00
_orb = CV_ORB :: create ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , edgeThreshold_ , firstLevel_ , WTA_K_ , scoreType_ , patchSize_ , fastThreshold_ );
2015-06-18 23:00:46 -04:00
#endif
2014-08-11 15:23:06 +00:00
}
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > ORB :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2014-06-22 03:33:56 +00:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
cv :: Mat imgRoi ( image , roi );
2016-01-13 09:20:15 -05:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2015-06-18 23:00:46 -04:00
if ( gpu_ )
2014-06-22 03:33:56 +00:00
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2014-06-22 03:33:56 +00:00
cv :: gpu :: GpuMat imgGpu ( imgRoi );
2016-01-13 09:20:15 -05:00
cv :: gpu :: GpuMat maskGpu ( maskRoi );
( * _gpuOrb . obj )( imgGpu , maskGpu , keypoints );
2016-01-21 17:11:57 -05:00
#else
UERROR ( "Cannot use ORBGPU because OpenCV is not built with gpu module." );
#endif
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
2018-07-11 18:33:31 +02:00
cv :: cuda :: GpuMat d_image ( imgRoi );
cv :: cuda :: GpuMat d_mask ( maskRoi );
try {
_gpuOrb -> detectAndCompute ( d_image , d_mask , keypoints , cv :: cuda :: GpuMat (), false );
} catch ( cv :: Exception & e ) {
const char * err_msg = e . what ();
UWARN ( "OpenCV exception caught: %s" , err_msg );
}
2015-06-18 23:00:46 -04:00
#endif
#endif
2014-06-22 03:33:56 +00:00
}
else
{
2016-01-13 09:20:15 -05:00
_orb -> detect ( imgRoi , keypoints , maskRoi );
2014-06-22 03:33:56 +00:00
}
return keypoints ;
}
cv :: Mat ORB :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
if ( image . empty ())
{
ULOGGER_ERROR ( "Image is null ?!?" );
return descriptors ;
}
2015-06-18 23:00:46 -04:00
if ( gpu_ )
2014-06-22 03:33:56 +00:00
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2014-06-22 03:33:56 +00:00
cv :: gpu :: GpuMat imgGpu ( image );
cv :: gpu :: GpuMat descriptorsGPU ;
2015-06-18 23:00:46 -04:00
( * _gpuOrb . obj )( imgGpu , cv :: gpu :: GpuMat (), keypoints , descriptorsGPU );
2014-06-22 03:33:56 +00:00
// Download descriptors
if ( descriptorsGPU . empty ())
descriptors = cv :: Mat ();
else
{
UASSERT ( descriptorsGPU . type () == CV_32F );
descriptors = cv :: Mat ( descriptorsGPU . size (), CV_32F );
descriptorsGPU . download ( descriptors );
}
2016-01-21 17:11:57 -05:00
#else
UERROR ( "GPU version of ORB not available (OpenCV not built with gpu/cuda module)! Using CPU version instead..." );
#endif
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
2018-07-11 18:33:31 +02:00
cv :: cuda :: GpuMat d_image ( image );
cv :: cuda :: GpuMat d_descriptors ;
try {
_gpuOrb -> detectAndCompute ( d_image , cv :: cuda :: GpuMat (), keypoints , d_descriptors , true );
} catch ( cv :: Exception & e ) {
const char * err_msg = e . what ();
UWARN ( "OpenCV exception caught: %s" , err_msg );
}
// Download descriptors
if ( d_descriptors . empty ())
descriptors = cv :: Mat ();
else
{
UASSERT ( d_descriptors . type () == CV_32F || d_descriptors . type () == CV_8U );
d_descriptors . download ( descriptors );
}
2016-01-21 17:11:57 -05:00
#endif
#endif
2014-06-22 03:33:56 +00:00
}
else
{
_orb -> compute ( image , keypoints , descriptors );
}
return descriptors ;
}
//////////////////////////
//FAST
//////////////////////////
FAST :: FAST ( const ParametersMap & parameters ) :
2014-08-11 15:23:06 +00:00
threshold_ ( Parameters :: defaultFASTThreshold ()),
nonmaxSuppression_ ( Parameters :: defaultFASTNonmaxSuppression ()),
gpu_ ( Parameters :: defaultFASTGpu ()),
2015-11-25 19:56:14 -05:00
gpuKeypointsRatio_ ( Parameters :: defaultFASTGpuKeypointsRatio ()),
minThreshold_ ( Parameters :: defaultFASTMinThreshold ()),
maxThreshold_ ( Parameters :: defaultFASTMaxThreshold ()),
gridRows_ ( Parameters :: defaultFASTGridRows ()),
2019-08-14 12:09:56 -04:00
gridCols_ ( Parameters :: defaultFASTGridCols ()),
fastCV_ ( Parameters :: defaultFASTCV ()),
fastCVinit_ ( false ),
fastCVMaxFeatures_ ( 10000 ),
fastCVLastImageHeight_ ( 0 )
2014-06-22 03:33:56 +00:00
{
2019-08-14 12:09:56 -04:00
#ifdef RTABMAP_FASTCV
char sVersion [ 128 ] = { 0 };
fcvGetVersion ( sVersion , 128 );
UINFO ( "fastcv version = %s" , sVersion );
int ix ;
if (( ix = fcvSetOperationMode ( FASTCV_OP_PERFORMANCE )))
{
UERROR ( "fcvSetOperationMode return=%d, OpenCV FAST will be used instead!" , ix );
fastCV_ = 0 ;
}
else
{
fcvMemInit ();
if ( ! ( fastCVCorners_ = ( uint32_t * ) fcvMemAlloc ( fastCVMaxFeatures_ * sizeof ( uint32_t ) * 2 , 16 )) ||
! ( fastCVCornerScores_ = ( uint32_t * ) fcvMemAlloc ( fastCVMaxFeatures_ * sizeof ( uint32_t ), 16 )))
{
UERROR ( "could not alloc fastcv mem, using opencv fast instead!" );
if ( fastCVCorners_ )
{
fcvMemFree ( fastCVCorners_ );
fastCVCorners_ = NULL ;
}
if ( fastCVCornerScores_ )
{
fcvMemFree ( fastCVCornerScores_ );
fastCVCornerScores_ = NULL ;
}
}
else
{
fastCVinit_ = true ;
}
}
#endif
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2014-06-22 03:33:56 +00:00
}
FAST ::~ FAST ()
{
2019-08-14 12:09:56 -04:00
#ifdef RTABMAP_FASTCV
if ( fastCVinit_ )
{
fcvMemDeInit ();
if ( fastCVCorners_ )
fcvMemFree ( fastCVCorners_ );
if ( fastCVCornerScores_ )
fcvMemFree ( fastCVCornerScores_ );
if ( fastCVTempBuf_ )
fcvMemFree ( fastCVTempBuf_ );
}
#endif
2014-06-22 03:33:56 +00:00
}
2014-08-11 15:23:06 +00:00
void FAST :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-08-11 15:23:06 +00:00
Parameters :: parse ( parameters , Parameters :: kFASTThreshold (), threshold_ );
Parameters :: parse ( parameters , Parameters :: kFASTNonmaxSuppression (), nonmaxSuppression_ );
Parameters :: parse ( parameters , Parameters :: kFASTGpu (), gpu_ );
Parameters :: parse ( parameters , Parameters :: kFASTGpuKeypointsRatio (), gpuKeypointsRatio_ );
2015-11-25 19:56:14 -05:00
Parameters :: parse ( parameters , Parameters :: kFASTMinThreshold (), minThreshold_ );
Parameters :: parse ( parameters , Parameters :: kFASTMaxThreshold (), maxThreshold_ );
Parameters :: parse ( parameters , Parameters :: kFASTGridRows (), gridRows_ );
Parameters :: parse ( parameters , Parameters :: kFASTGridCols (), gridCols_ );
2019-08-14 12:09:56 -04:00
Parameters :: parse ( parameters , Parameters :: kFASTCV (), fastCV_ );
UASSERT ( fastCV_ == 0 || fastCV_ == 9 || fastCV_ == 10 );
2015-11-25 19:56:14 -05:00
UASSERT_MSG ( threshold_ >= minThreshold_ , uFormat ( "%d vs %d" , threshold_ , minThreshold_ ). c_str ());
UASSERT_MSG ( threshold_ <= maxThreshold_ , uFormat ( "%d vs %d" , threshold_ , maxThreshold_ ). c_str ());
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2015-06-18 23:00:46 -04:00
if ( gpu_ && cv :: gpu :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of FAST not available! Using CPU version instead..." );
gpu_ = false ;
2014-08-11 15:23:06 +00:00
}
2015-06-18 23:00:46 -04:00
#else
2016-01-21 17:11:57 -05:00
if ( gpu_ )
{
UWARN ( "GPU version of FAST not available (OpenCV not built with gpu/cuda module)! Using CPU version instead..." );
gpu_ = false ;
}
#endif
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
2015-06-18 23:00:46 -04:00
if ( gpu_ && cv :: cuda :: getCudaEnabledDeviceCount () == 0 )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of FAST not available! Using CPU version instead..." );
gpu_ = false ;
2014-08-11 15:23:06 +00:00
}
2016-01-21 17:11:57 -05:00
#else
2015-06-18 23:00:46 -04:00
if ( gpu_ )
2014-08-11 15:23:06 +00:00
{
2015-06-18 23:00:46 -04:00
UWARN ( "GPU version of FAST not available (OpenCV cudafeatures2d module)! Using CPU version instead..." );
gpu_ = false ;
}
#endif
#endif
if ( gpu_ )
{
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2015-06-18 23:00:46 -04:00
_gpuFast = new CV_FAST_GPU ( threshold_ , nonmaxSuppression_ , gpuKeypointsRatio_ );
2016-01-21 17:11:57 -05:00
#else
UFATAL ( "not supposed to be here!" );
#endif
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
UFATAL ( "not implemented" );
#endif
#endif
2014-08-11 15:23:06 +00:00
}
else
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2015-11-25 19:56:14 -05:00
if ( gridRows_ > 0 && gridCols_ > 0 )
{
2016-03-08 12:31:32 -05:00
UDEBUG ( "grid max features = %d" , this -> getMaxFeatures ());
2015-11-25 19:56:14 -05:00
cv :: Ptr < cv :: FeatureDetector > fastAdjuster = cv :: Ptr < cv :: FastAdjuster > ( new cv :: FastAdjuster ( threshold_ , nonmaxSuppression_ , minThreshold_ , maxThreshold_ ));
2015-12-21 17:27:05 -05:00
_fast = cv :: Ptr < cv :: FeatureDetector > ( new cv :: GridAdaptedFeatureDetector ( fastAdjuster , this -> getMaxFeatures (), gridRows_ , gridCols_ ));
2015-11-25 19:56:14 -05:00
}
else
{
if ( gridRows_ > 0 )
{
UWARN ( "Parameter \" %s \" is set (value=%d) but not \" %s \" ! Grid adaptor will not be added." ,
Parameters :: kFASTGridRows (). c_str (), gridRows_ , Parameters :: kFASTGridCols (). c_str ());
}
else if ( gridCols_ > 0 )
{
UWARN ( "Parameter \" %s \" is set (value=%d) but not \" %s \" ! Grid adaptor will not be added." ,
Parameters :: kFASTGridCols (). c_str (), gridCols_ , Parameters :: kFASTGridRows (). c_str ());
}
_fast = cv :: Ptr < cv :: FeatureDetector > ( new CV_FAST ( threshold_ , nonmaxSuppression_ ));
}
2015-06-18 23:00:46 -04:00
#else
_fast = CV_FAST :: create ( threshold_ , nonmaxSuppression_ );
#endif
2014-08-11 15:23:06 +00:00
}
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > FAST :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2014-06-22 03:33:56 +00:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
2019-08-14 12:09:56 -04:00
#ifdef RTABMAP_FASTCV
if ( fastCV_ > 0 )
{
// Note: mask not supported, it should be the inverse of the current mask used (0=where to extract)
uint32_t nCorners = 0 ;
UASSERT ( fastCVCorners_ != NULL && fastCVCornerScores_ != NULL );
if ( nonmaxSuppression_ )
{
if ( fastCVTempBuf_ == NULL || ( fastCVTempBuf_ != NULL && fastCVLastImageHeight_ != image . rows ))
{
if ( fastCVTempBuf_ )
{
fcvMemFree ( fastCVTempBuf_ );
fastCVTempBuf_ = NULL ;
}
if ( ! ( fastCVTempBuf_ = ( uint32_t * ) fcvMemAlloc ( ( 3 * fastCVMaxFeatures_ + image . rows + 1 ) * 4 , 16 )))
{
UERROR ( "could not alloc fastcv mem for temp buf (%s=true)" , Parameters :: kFASTNonmaxSuppression (). c_str ());
fastCVLastImageHeight_ = 0 ;
return keypoints ;
}
fastCVLastImageHeight_ = image . rows ;
}
}
// image.data should be 128 bits aligned
UDEBUG ( "%dx%d (step=%d) thr=%d maxFeatures=%d" , image . cols , image . rows , image . step1 (), threshold_ , fastCVMaxFeatures_ );
if ( fastCV_ == 10 )
{
fcvCornerFast10Scoreu8 ( image . data , image . cols , image . rows , 0 , threshold_ , 0 , fastCVCorners_ , fastCVCornerScores_ , fastCVMaxFeatures_ , & nCorners , nonmaxSuppression_ ? 1 : 0 , fastCVTempBuf_ );
}
else
{
fcvCornerFast9Scoreu8_v2 ( image . data , image . cols , image . rows , image . step1 (), threshold_ , 0 , fastCVCorners_ , fastCVCornerScores_ , fastCVMaxFeatures_ , & nCorners , nonmaxSuppression_ ? 1 : 0 , fastCVTempBuf_ );
}
UDEBUG ( "number of corners found = %d:" , nCorners );
keypoints . resize ( nCorners );
for ( uint32_t i = 0 ; i < nCorners ; i ++ )
{
keypoints [ i ]. pt . x = fastCVCorners_ [ i * 2 ];
keypoints [ i ]. pt . y = fastCVCorners_ [( i * 2 ) + 1 ];
keypoints [ i ]. size = 3 ;
keypoints [ i ]. response = fastCVCornerScores_ [ i ];
}
if ( this -> getMaxFeatures () > 0 )
{
this -> limitKeypoints ( keypoints , this -> getMaxFeatures ());
}
return keypoints ;
}
#endif
if ( fastCV_ > 0 )
{
UWARN ( "RTAB-Map is not built with FastCV support. OpenCV's FAST is used instead. "
"Please set %s to 0. This message will only appear once." ,
Parameters :: kFASTCV (). c_str ());
fastCV_ = 0 ;
}
2014-06-22 03:33:56 +00:00
cv :: Mat imgRoi ( image , roi );
2016-01-13 09:20:15 -05:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2015-06-18 23:00:46 -04:00
if ( gpu_ )
2014-06-22 03:33:56 +00:00
{
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2016-01-21 17:11:57 -05:00
#ifdef HAVE_OPENCV_GPU
2014-06-22 03:33:56 +00:00
cv :: gpu :: GpuMat imgGpu ( imgRoi );
2016-01-13 09:20:15 -05:00
cv :: gpu :: GpuMat maskGpu ( maskRoi );
( * _gpuFast . obj )( imgGpu , maskGpu , keypoints );
2016-01-21 17:11:57 -05:00
#else
UERROR ( "Cannot use FAST GPU because OpenCV is not built with gpu module." );
#endif
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
UFATAL ( "not implemented" );
#endif
#endif
2014-06-22 03:33:56 +00:00
}
else
{
2016-01-13 09:20:15 -05:00
_fast -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
2014-06-22 03:33:56 +00:00
}
return keypoints ;
}
//////////////////////////
//FAST-BRIEF
//////////////////////////
FAST_BRIEF :: FAST_BRIEF ( const ParametersMap & parameters ) :
FAST ( parameters ),
2015-06-18 23:00:46 -04:00
bytes_ ( Parameters :: defaultBRIEFBytes ())
2014-06-22 03:33:56 +00:00
{
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2014-06-22 03:33:56 +00:00
}
FAST_BRIEF ::~ FAST_BRIEF ()
{
}
2014-08-11 15:23:06 +00:00
void FAST_BRIEF :: parseParameters ( const ParametersMap & parameters )
{
FAST :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kBRIEFBytes (), bytes_ );
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
_brief = cv :: Ptr < CV_BRIEF > ( new CV_BRIEF ( bytes_ ));
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_brief = CV_BRIEF :: create ( bytes_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Brief cannot be used!" );
#endif
#endif
2014-08-11 15:23:06 +00:00
}
2014-06-22 03:33:56 +00:00
cv :: Mat FAST_BRIEF :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-06-22 03:33:56 +00:00
_brief -> compute ( image , keypoints , descriptors );
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_brief -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Brief cannot be used!" );
#endif
#endif
2014-06-22 03:33:56 +00:00
return descriptors ;
}
//////////////////////////
//FAST-FREAK
//////////////////////////
FAST_FREAK :: FAST_FREAK ( const ParametersMap & parameters ) :
FAST ( parameters ),
2014-08-11 15:23:06 +00:00
orientationNormalized_ ( Parameters :: defaultFREAKOrientationNormalized ()),
scaleNormalized_ ( Parameters :: defaultFREAKScaleNormalized ()),
patternScale_ ( Parameters :: defaultFREAKPatternScale ()),
2015-06-18 23:00:46 -04:00
nOctaves_ ( Parameters :: defaultFREAKNOctaves ())
2014-06-22 03:33:56 +00:00
{
2014-08-11 15:23:06 +00:00
parseParameters ( parameters );
2014-06-22 03:33:56 +00:00
}
FAST_FREAK ::~ FAST_FREAK ()
{
}
2014-08-11 15:23:06 +00:00
void FAST_FREAK :: parseParameters ( const ParametersMap & parameters )
{
FAST :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kFREAKOrientationNormalized (), orientationNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKScaleNormalized (), scaleNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKPatternScale (), patternScale_ );
Parameters :: parse ( parameters , Parameters :: kFREAKNOctaves (), nOctaves_ );
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
_freak = cv :: Ptr < CV_FREAK > ( new CV_FREAK ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ ));
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak = CV_FREAK :: create ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
2014-08-11 15:23:06 +00:00
}
2014-06-22 03:33:56 +00:00
cv :: Mat FAST_FREAK :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-06-22 03:33:56 +00:00
_freak -> compute ( image , keypoints , descriptors );
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
2014-06-22 03:33:56 +00:00
return descriptors ;
}
2014-08-20 23:05:06 +00:00
//////////////////////////
//GFTT
//////////////////////////
GFTT :: GFTT ( const ParametersMap & parameters ) :
_qualityLevel ( Parameters :: defaultGFTTQualityLevel ()),
_minDistance ( Parameters :: defaultGFTTMinDistance ()),
_blockSize ( Parameters :: defaultGFTTBlockSize ()),
_useHarrisDetector ( Parameters :: defaultGFTTUseHarrisDetector ()),
2024-09-13 14:22:54 -07:00
_k ( Parameters :: defaultGFTTK ()),
_gpu ( Parameters :: defaultGFTTGpu ())
2014-08-20 23:05:06 +00:00
{
parseParameters ( parameters );
}
GFTT ::~ GFTT ()
{
}
void GFTT :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-08-20 23:05:06 +00:00
Parameters :: parse ( parameters , Parameters :: kGFTTQualityLevel (), _qualityLevel );
Parameters :: parse ( parameters , Parameters :: kGFTTMinDistance (), _minDistance );
Parameters :: parse ( parameters , Parameters :: kGFTTBlockSize (), _blockSize );
Parameters :: parse ( parameters , Parameters :: kGFTTUseHarrisDetector (), _useHarrisDetector );
Parameters :: parse ( parameters , Parameters :: kGFTTK (), _k );
2024-09-13 14:22:54 -07:00
Parameters :: parse ( parameters , Parameters :: kGFTTGpu (), _gpu );
2014-08-20 23:05:06 +00:00
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2024-09-13 14:22:54 -07:00
if ( _gpu )
{
UWARN ( "GPU version of GFTT is not implemented for OpenCV<3! Using CPU version instead..." );
_gpu = false ;
}
2015-06-18 23:00:46 -04:00
#endif
2024-09-13 14:22:54 -07:00
#ifdef HAVE_OPENCV_CUDAIMGPROC
if ( _gpu && cv :: cuda :: getCudaEnabledDeviceCount () == 0 )
{
UWARN ( "GPU version of GFTT not available! Using CPU version instead..." );
_gpu = false ;
}
#else
if ( _gpu )
{
UWARN ( "GPU version of GFTT not available (OpenCV cudaimageproc module)! Using CPU version instead..." );
_gpu = false ;
}
#endif
if ( _gpu )
{
#ifdef HAVE_OPENCV_CUDAIMGPROC
_gpuGftt = cv :: cuda :: createGoodFeaturesToTrackDetector ( CV_8UC1 , this -> getMaxFeatures (), _qualityLevel , _minDistance , _blockSize , _useHarrisDetector , _k );
#else
UFATAL ( "not supposed to be here!" );
#endif
}
else
{
#if CV_MAJOR_VERSION < 3
_gftt = cv :: Ptr < CV_GFTT > ( new CV_GFTT ( this -> getMaxFeatures (), _qualityLevel , _minDistance , _blockSize , _useHarrisDetector , _k ));
#else
_gftt = CV_GFTT :: create ( this -> getMaxFeatures (), _qualityLevel , _minDistance , _blockSize , _useHarrisDetector , _k );
#endif
}
2014-08-20 23:05:06 +00:00
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > GFTT :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2014-08-20 23:05:06 +00:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
cv :: Mat imgRoi ( image , roi );
2016-01-13 09:20:15 -05:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2024-09-13 14:22:54 -07:00
#if CV_MAJOR_VERSION >= 3 && defined(HAVE_OPENCV_CUDAIMGPROC)
if ( _gpu )
{
cv :: cuda :: GpuMat imgGpu ( imgRoi );
cv :: cuda :: GpuMat maskGpu ( maskRoi );
cv :: cuda :: GpuMat cornersGpu ;
_gpuGftt -> detect ( imgGpu , cornersGpu , maskGpu );
std :: vector < cv :: Point2f > corners ( cornersGpu . cols );
cv :: Mat cornersMat ( 1 , cornersGpu . cols , CV_32FC2 , ( void * ) & corners [ 0 ]);
cornersGpu . download ( cornersMat );
cv :: KeyPoint :: convert ( corners , keypoints , _blockSize );
}
else
#endif
{
_gftt -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
}
2014-08-20 23:05:06 +00:00
return keypoints ;
}
//////////////////////////
//FAST-BRIEF
//////////////////////////
GFTT_BRIEF :: GFTT_BRIEF ( const ParametersMap & parameters ) :
GFTT ( parameters ),
2015-06-18 23:00:46 -04:00
bytes_ ( Parameters :: defaultBRIEFBytes ())
2014-08-20 23:05:06 +00:00
{
parseParameters ( parameters );
}
GFTT_BRIEF ::~ GFTT_BRIEF ()
{
}
void GFTT_BRIEF :: parseParameters ( const ParametersMap & parameters )
{
GFTT :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kBRIEFBytes (), bytes_ );
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
_brief = cv :: Ptr < CV_BRIEF > ( new CV_BRIEF ( bytes_ ));
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_brief = CV_BRIEF :: create ( bytes_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Brief cannot be used!" );
#endif
#endif
2014-08-20 23:05:06 +00:00
}
cv :: Mat GFTT_BRIEF :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-08-20 23:05:06 +00:00
_brief -> compute ( image , keypoints , descriptors );
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_brief -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Brief cannot be used!" );
#endif
#endif
2014-08-20 23:05:06 +00:00
return descriptors ;
}
//////////////////////////
2015-11-11 12:08:20 -05:00
//GFTT-FREAK
2014-08-20 23:05:06 +00:00
//////////////////////////
GFTT_FREAK :: GFTT_FREAK ( const ParametersMap & parameters ) :
GFTT ( parameters ),
orientationNormalized_ ( Parameters :: defaultFREAKOrientationNormalized ()),
scaleNormalized_ ( Parameters :: defaultFREAKScaleNormalized ()),
patternScale_ ( Parameters :: defaultFREAKPatternScale ()),
2015-06-18 23:00:46 -04:00
nOctaves_ ( Parameters :: defaultFREAKNOctaves ())
2014-08-20 23:05:06 +00:00
{
parseParameters ( parameters );
}
GFTT_FREAK ::~ GFTT_FREAK ()
{
}
void GFTT_FREAK :: parseParameters ( const ParametersMap & parameters )
{
GFTT :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kFREAKOrientationNormalized (), orientationNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKScaleNormalized (), scaleNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKPatternScale (), patternScale_ );
Parameters :: parse ( parameters , Parameters :: kFREAKNOctaves (), nOctaves_ );
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
_freak = cv :: Ptr < CV_FREAK > ( new CV_FREAK ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ ));
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak = CV_FREAK :: create ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
2014-08-20 23:05:06 +00:00
}
cv :: Mat GFTT_FREAK :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2015-06-18 23:00:46 -04:00
#if CV_MAJOR_VERSION < 3
2014-08-20 23:05:06 +00:00
_freak -> compute ( image , keypoints , descriptors );
2015-06-18 23:00:46 -04:00
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
2014-08-20 23:05:06 +00:00
return descriptors ;
}
2020-09-28 10:29:14 -04:00
//////////////////////////
//SURF-FREAK
//////////////////////////
SURF_FREAK :: SURF_FREAK ( const ParametersMap & parameters ) :
SURF ( parameters ),
orientationNormalized_ ( Parameters :: defaultFREAKOrientationNormalized ()),
scaleNormalized_ ( Parameters :: defaultFREAKScaleNormalized ()),
patternScale_ ( Parameters :: defaultFREAKPatternScale ()),
nOctaves_ ( Parameters :: defaultFREAKNOctaves ())
{
parseParameters ( parameters );
}
SURF_FREAK ::~ SURF_FREAK ()
{
}
void SURF_FREAK :: parseParameters ( const ParametersMap & parameters )
{
SURF :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kFREAKOrientationNormalized (), orientationNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKScaleNormalized (), scaleNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKPatternScale (), patternScale_ );
Parameters :: parse ( parameters , Parameters :: kFREAKNOctaves (), nOctaves_ );
#if CV_MAJOR_VERSION < 3
_freak = cv :: Ptr < CV_FREAK > ( new CV_FREAK ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ ));
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak = CV_FREAK :: create ( orientationNormalized_ , scaleNormalized_ , patternScale_ , nOctaves_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
}
cv :: Mat SURF_FREAK :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
#if CV_MAJOR_VERSION < 3
_freak -> compute ( image , keypoints , descriptors );
#else
#ifdef HAVE_OPENCV_XFEATURES2D
_freak -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so Freak cannot be used!" );
#endif
#endif
return descriptors ;
}
2015-11-11 12:08:20 -05:00
//////////////////////////
//GFTT-ORB
//////////////////////////
GFTT_ORB :: GFTT_ORB ( const ParametersMap & parameters ) :
GFTT ( parameters ),
_orb ( parameters )
{
parseParameters ( parameters );
}
GFTT_ORB ::~ GFTT_ORB ()
{
}
void GFTT_ORB :: parseParameters ( const ParametersMap & parameters )
{
GFTT :: parseParameters ( parameters );
_orb . parseParameters ( parameters );
}
cv :: Mat GFTT_ORB :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
return _orb . generateDescriptors ( image , keypoints );
}
2014-10-06 19:41:45 +00:00
//////////////////////////
//BRISK
//////////////////////////
BRISK :: BRISK ( const ParametersMap & parameters ) :
thresh_ ( Parameters :: defaultBRISKThresh ()),
octaves_ ( Parameters :: defaultBRISKOctaves ()),
2015-06-18 23:00:46 -04:00
patternScale_ ( Parameters :: defaultBRISKPatternScale ())
2014-10-06 19:41:45 +00:00
{
parseParameters ( parameters );
}
BRISK ::~ BRISK ()
{
}
void BRISK :: parseParameters ( const ParametersMap & parameters )
{
2015-04-29 00:02:10 -04:00
Feature2D :: parseParameters ( parameters );
2014-10-06 19:41:45 +00:00
Parameters :: parse ( parameters , Parameters :: kBRISKThresh (), thresh_ );
Parameters :: parse ( parameters , Parameters :: kBRISKOctaves (), octaves_ );
Parameters :: parse ( parameters , Parameters :: kBRISKPatternScale (), patternScale_ );
2026-07-29 22:48:39 -07:00
#if CV_MAJOR_VERSION > 4
#ifdef HAVE_OPENCV_XFEATURES2D
brisk_ = CV_BRISK :: create ( thresh_ , octaves_ , patternScale_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so BRISK cannot be used!" );
#endif
#elif CV_MAJOR_VERSION < 3
2015-06-18 23:00:46 -04:00
brisk_ = cv :: Ptr < CV_BRISK > ( new CV_BRISK ( thresh_ , octaves_ , patternScale_ ));
#else
brisk_ = CV_BRISK :: create ( thresh_ , octaves_ , patternScale_ );
#endif
2014-10-06 19:41:45 +00:00
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > BRISK :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2014-10-06 19:41:45 +00:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
2026-07-29 22:48:39 -07:00
#if CV_MAJOR_VERSION < 5 || (CV_MAJOR_VERSION > 4 && defined(HAVE_OPENCV_XFEATURES2D))
2014-10-06 19:41:45 +00:00
cv :: Mat imgRoi ( image , roi );
2016-01-13 09:20:15 -05:00
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
brisk_ -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
2026-07-29 22:48:39 -07:00
#else
UWARN ( "RTAB-Map is not built with BRISK feature support!" );
#endif
2014-10-06 19:41:45 +00:00
return keypoints ;
}
cv :: Mat BRISK :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2026-07-29 22:48:39 -07:00
#if CV_MAJOR_VERSION < 5 || (CV_MAJOR_VERSION > 4 && defined(HAVE_OPENCV_XFEATURES2D))
2014-10-06 19:41:45 +00:00
brisk_ -> compute ( image , keypoints , descriptors );
2026-07-29 22:48:39 -07:00
#else
UWARN ( "RTAB-Map is not built with BRISK feature support!" );
#endif
2014-10-06 19:41:45 +00:00
return descriptors ;
}
2017-01-30 22:24:51 -05:00
//////////////////////////
2017-07-25 16:20:05 -04:00
//KAZE
2017-01-30 22:24:51 -05:00
//////////////////////////
2017-07-25 16:20:05 -04:00
KAZE :: KAZE ( const ParametersMap & parameters ) :
extended_ ( Parameters :: defaultKAZEExtended ()),
upright_ ( Parameters :: defaultKAZEUpright ()),
threshold_ ( Parameters :: defaultKAZEThreshold ()),
nOctaves_ ( Parameters :: defaultKAZENOctaves ()),
nOctaveLayers_ ( Parameters :: defaultKAZENOctaveLayers ()),
diffusivity_ ( Parameters :: defaultKAZEDiffusivity ())
2017-01-30 22:24:51 -05:00
{
parseParameters ( parameters );
}
2017-07-25 16:20:05 -04:00
KAZE ::~ KAZE ()
2017-01-30 22:24:51 -05:00
{
}
2017-07-25 16:20:05 -04:00
void KAZE :: parseParameters ( const ParametersMap & parameters )
2017-01-30 22:24:51 -05:00
{
2017-08-31 16:41:01 -04:00
Feature2D :: parseParameters ( parameters );
2017-07-25 16:20:05 -04:00
Parameters :: parse ( parameters , Parameters :: kKAZEExtended (), extended_ );
Parameters :: parse ( parameters , Parameters :: kKAZEUpright (), upright_ );
Parameters :: parse ( parameters , Parameters :: kKAZEThreshold (), threshold_ );
Parameters :: parse ( parameters , Parameters :: kKAZENOctaves (), nOctaves_ );
Parameters :: parse ( parameters , Parameters :: kKAZENOctaveLayers (), nOctaveLayers_ );
Parameters :: parse ( parameters , Parameters :: kKAZEDiffusivity (), diffusivity_ );
2017-01-30 22:24:51 -05:00
2026-07-29 22:48:39 -07:00
#if CV_MAJOR_VERSION > 4
#ifdef HAVE_OPENCV_XFEATURES2D
kaze_ = CV_KAZE :: create ( extended_ , upright_ , threshold_ , nOctaves_ , nOctaveLayers_ , ( CV_KAZE :: DiffusivityType ) diffusivity_ );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so KAZE cannot be used!" );
#endif
#elif CV_MAJOR_VERSION > 3
kaze_ = CV_KAZE :: create ( extended_ , upright_ , threshold_ , nOctaves_ , nOctaveLayers_ , ( CV_KAZE :: DiffusivityType ) diffusivity_ );
2018-10-21 13:35:03 -04:00
#elif CV_MAJOR_VERSION > 2
2026-07-29 22:48:39 -07:00
kaze_ = CV_KAZE :: create ( extended_ , upright_ , threshold_ , nOctaves_ , nOctaveLayers_ , diffusivity_ );
2017-01-30 22:24:51 -05:00
#else
2017-07-25 16:20:05 -04:00
UWARN ( "RTAB-Map is not built with OpenCV3 so Kaze feature cannot be used!" );
2017-01-30 22:24:51 -05:00
#endif
}
2019-04-18 19:50:58 -04:00
std :: vector < cv :: KeyPoint > KAZE :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
2017-01-30 22:24:51 -05:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
std :: vector < cv :: KeyPoint > keypoints ;
2026-07-29 22:48:39 -07:00
#if (CV_MAJOR_VERSION > 2 && CV_MAJOR_VERSION < 5) || (CV_MAJOR_VERSION > 4 && defined(HAVE_OPENCV_XFEATURES2D))
2017-01-30 22:24:51 -05:00
cv :: Mat imgRoi ( image , roi );
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
2017-07-25 16:20:05 -04:00
kaze_ -> detect ( imgRoi , keypoints , maskRoi ); // Opencv keypoints
2017-01-30 22:24:51 -05:00
#else
2026-07-29 22:48:39 -07:00
UWARN ( "RTAB-Map is not built with Kaze feature support!" );
2017-01-30 22:24:51 -05:00
#endif
return keypoints ;
}
2017-07-25 16:20:05 -04:00
cv :: Mat KAZE :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
2017-01-30 22:24:51 -05:00
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
2026-07-29 22:48:39 -07:00
#if (CV_MAJOR_VERSION > 2 && CV_MAJOR_VERSION < 5) || (CV_MAJOR_VERSION > 4 && defined(HAVE_OPENCV_XFEATURES2D))
2017-07-25 16:20:05 -04:00
kaze_ -> compute ( image , keypoints , descriptors );
2017-01-30 22:24:51 -05:00
#else
2026-07-29 22:48:39 -07:00
UWARN ( "RTAB-Map is not built with Kaze feature support!" );
2017-01-30 22:24:51 -05:00
#endif
return descriptors ;
}
2019-04-18 19:50:58 -04:00
//////////////////////////
//ORBOctree
//////////////////////////
ORBOctree :: ORBOctree ( const ParametersMap & parameters ) :
scaleFactor_ ( Parameters :: defaultORBScaleFactor ()),
nLevels_ ( Parameters :: defaultORBNLevels ()),
2020-11-01 11:28:59 -05:00
patchSize_ ( Parameters :: defaultORBPatchSize ()),
edgeThreshold_ ( Parameters :: defaultORBEdgeThreshold ()),
fastThreshold_ ( Parameters :: defaultFASTThreshold ()),
fastMinThreshold_ ( Parameters :: defaultFASTMinThreshold ())
2019-04-18 19:50:58 -04:00
{
parseParameters ( parameters );
}
ORBOctree ::~ ORBOctree ()
{
}
void ORBOctree :: parseParameters ( const ParametersMap & parameters )
{
Feature2D :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kORBScaleFactor (), scaleFactor_ );
Parameters :: parse ( parameters , Parameters :: kORBNLevels (), nLevels_ );
2020-11-01 11:28:59 -05:00
Parameters :: parse ( parameters , Parameters :: kORBPatchSize (), patchSize_ );
Parameters :: parse ( parameters , Parameters :: kORBEdgeThreshold (), edgeThreshold_ );
2019-04-18 19:50:58 -04:00
Parameters :: parse ( parameters , Parameters :: kFASTThreshold (), fastThreshold_ );
Parameters :: parse ( parameters , Parameters :: kFASTMinThreshold (), fastMinThreshold_ );
#ifdef RTABMAP_ORB_OCTREE
2020-11-01 11:28:59 -05:00
_orb = cv :: Ptr < ORBextractor > ( new ORBextractor ( this -> getMaxFeatures (), scaleFactor_ , nLevels_ , fastThreshold_ , fastMinThreshold_ , patchSize_ , edgeThreshold_ ));
2019-04-18 19:50:58 -04:00
#else
UWARN ( "RTAB-Map is not built with ORB OcTree option enabled so ORB OcTree feature cannot be used!" );
#endif
}
std :: vector < cv :: KeyPoint > ORBOctree :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
{
std :: vector < cv :: KeyPoint > keypoints ;
descriptors_ = cv :: Mat ();
#ifdef RTABMAP_ORB_OCTREE
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat imgRoi ( image , roi );
cv :: Mat maskRoi ;
if ( ! mask . empty ())
{
maskRoi = cv :: Mat ( mask , roi );
}
( * _orb )( imgRoi , maskRoi , keypoints , descriptors_ );
2023-10-27 04:15:34 +08:00
// OrbOctree ignores the mask, so we have to apply it manually here
if ( ! keypoints . empty () && ! maskRoi . empty ())
{
std :: vector < cv :: KeyPoint > validKeypoints ;
validKeypoints . reserve ( keypoints . size ());
cv :: Mat validDescriptors ;
for ( size_t i = 0 ; i < keypoints . size (); ++ i )
{
if ( maskRoi . at < unsigned char > ( keypoints [ i ]. pt . y + roi . y , keypoints [ i ]. pt . x + roi . x ) != 0 )
{
validKeypoints . push_back ( keypoints [ i ]);
validDescriptors . push_back ( descriptors_ . row ( i ));
}
}
keypoints = validKeypoints ;
descriptors_ = validDescriptors ;
}
2019-04-18 19:50:58 -04:00
if (( int ) keypoints . size () > this -> getMaxFeatures ())
{
2024-05-20 09:21:00 +08:00
limitKeypoints ( keypoints , descriptors_ , this -> getMaxFeatures (), roi . size (), this -> getSSC ());
2019-04-18 19:50:58 -04:00
}
#else
UWARN ( "RTAB-Map is not built with ORB OcTree option enabled so ORB OcTree feature cannot be used!" );
#endif
return keypoints ;
}
cv :: Mat ORBOctree :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
#ifdef RTABMAP_ORB_OCTREE
UASSERT_MSG (( int ) keypoints . size () == descriptors_ . rows , uFormat ( "keypoints=%d descriptors=%d" , ( int ) keypoints . size (), descriptors_ . rows ). c_str ());
#else
UWARN ( "RTAB-Map is not built with ORB OcTree option enabled so ORB OcTree feature cannot be used!" );
#endif
return descriptors_ ;
}
2020-04-16 17:59:45 -04:00
//////////////////////////
//SuperPointTorch
//////////////////////////
SuperPointTorch :: SuperPointTorch ( const ParametersMap & parameters ) :
2020-05-09 21:13:41 -04:00
path_ ( Parameters :: defaultSuperPointModelPath ()),
threshold_ ( Parameters :: defaultSuperPointThreshold ()),
nms_ ( Parameters :: defaultSuperPointNMS ()),
minDistance_ ( Parameters :: defaultSuperPointNMSRadius ()),
cuda_ ( Parameters :: defaultSuperPointCuda ())
2020-04-16 17:59:45 -04:00
{
parseParameters ( parameters );
}
SuperPointTorch ::~ SuperPointTorch ()
{
}
void SuperPointTorch :: parseParameters ( const ParametersMap & parameters )
{
Feature2D :: parseParameters ( parameters );
std :: string previousPath = path_ ;
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
bool previousCuda = cuda_ ;
2020-05-09 21:13:41 -04:00
#endif
Parameters :: parse ( parameters , Parameters :: kSuperPointModelPath (), path_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointThreshold (), threshold_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointNMS (), nms_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointNMSRadius (), minDistance_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointCuda (), cuda_ );
2020-04-16 17:59:45 -04:00
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
if ( superPoint_ . get () == 0 || path_ . compare ( previousPath ) != 0 || previousCuda != cuda_ )
{
superPoint_ = cv :: Ptr < SPDetector > ( new SPDetector ( path_ , threshold_ , nms_ , minDistance_ , cuda_ ));
}
else
{
superPoint_ -> setThreshold ( threshold_ );
superPoint_ -> SetNMS ( nms_ );
superPoint_ -> setMinDistance ( minDistance_ );
}
#else
2021-01-17 01:56:27 -05:00
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Torch feature cannot be used!" );
2020-04-16 17:59:45 -04:00
#endif
}
std :: vector < cv :: KeyPoint > SuperPointTorch :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
{
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
2021-11-14 20:23:09 -05:00
if ( roi . x != 0 || roi . y != 0 )
{
UERROR ( "SuperPoint: Not supporting ROI (%d,%d,%d,%d). Make sure %s, %s, %s, %s, %s, %s are all set to default values." ,
roi . x , roi . y , roi . width , roi . height ,
Parameters :: kKpRoiRatios (). c_str (),
Parameters :: kVisRoiRatios (). c_str (),
Parameters :: kVisGridRows (). c_str (),
Parameters :: kVisGridCols (). c_str (),
Parameters :: kKpGridRows (). c_str (),
Parameters :: kKpGridCols (). c_str ());
return std :: vector < cv :: KeyPoint > ();
}
2020-05-09 21:13:41 -04:00
return superPoint_ -> detect ( image , mask );
2020-04-16 17:59:45 -04:00
#else
2021-01-17 01:56:27 -05:00
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Torch feature cannot be used!" );
2020-04-16 17:59:45 -04:00
return std :: vector < cv :: KeyPoint > ();
#endif
}
cv :: Mat SuperPointTorch :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
2021-01-17 01:56:27 -05:00
#ifdef RTABMAP_TORCH
2020-04-16 17:59:45 -04:00
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
2026-06-07 13:19:31 -07:00
cv :: Mat descriptors ;
if ( ! keypoints . empty ())
{
descriptors = superPoint_ -> compute ( keypoints );
if ( descriptors . empty ())
{
// superpoint may have been reset between keypoint detection and now,
// re-detect features to re-inialize the descriptors matrix, then
// re-extract descriptors with original keypoints.
UWARN ( "Re-initializing superpoint on that image to extract descriptors" );
if ( ! superPoint_ -> detect ( image ). empty ())
{
descriptors = superPoint_ -> compute ( keypoints );
if ( descriptors . rows == ( int ) keypoints . size ())
{
UWARN ( "Sucessfully re-initialized superpoint, returning %d descriptors." , descriptors . rows );
}
}
else
{
UWARN ( "Failed to re-initialize superpoint on that image, returning empty descriptors." );
}
}
}
return descriptors ;
2020-04-16 17:59:45 -04:00
#else
2021-01-17 01:56:27 -05:00
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Torch feature cannot be used!" );
2020-04-16 17:59:45 -04:00
return cv :: Mat ();
#endif
2025-11-07 17:22:35 -08:00
}
//////////////////////////
//SuperPointRpautrat
//////////////////////////
SuperPointRpautrat :: SuperPointRpautrat ( const ParametersMap & parameters ) :
superpointWeightsPath_ ( Parameters :: defaultSuperPointRpautratWeightsPath ()),
superpointModelPath_ ( Parameters :: defaultSuperPointRpautratModelPath ()),
outputDir_ ( "" ),
threshold_ ( Parameters :: defaultSuperPointRpautratThreshold ()),
nms_ ( Parameters :: defaultSuperPointRpautratNMS ()),
minDistance_ ( Parameters :: defaultSuperPointRpautratNMSRadius ()),
cuda_ ( Parameters :: defaultSuperPointRpautratCuda ())
{
parseParameters ( parameters );
}
SuperPointRpautrat ::~ SuperPointRpautrat ()
{
}
void SuperPointRpautrat :: parseParameters ( const ParametersMap & parameters )
{
Feature2D :: parseParameters ( parameters );
#if defined(RTABMAP_TORCH) && defined(RTABMAP_PYTHON)
std :: string previousWeightsPath = superpointWeightsPath_ ;
std :: string previousModelPath = superpointModelPath_ ;
bool previousCuda = cuda_ ;
float previousThreshold = threshold_ ;
bool previousNms = nms_ ;
int previousMinDistance = minDistance_ ;
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratWeightsPath (), superpointWeightsPath_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratModelPath (), superpointModelPath_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratThreshold (), threshold_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratNMS (), nms_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratNMSRadius (), minDistance_ );
Parameters :: parse ( parameters , Parameters :: kSuperPointRpautratCuda (), cuda_ );
Parameters :: parse ( parameters , Parameters :: kRtabmapWorkingDirectory (), outputDir_ );
2025-11-16 11:37:03 -08:00
2025-11-07 17:22:35 -08:00
// If working directory is not set, use the default
if ( outputDir_ . empty ())
{
outputDir_ = Parameters :: createDefaultWorkingDirectory ();
}
2025-11-16 11:37:03 -08:00
// Reinitialize detector if model-affecting parameters changed
2025-11-07 17:22:35 -08:00
if ( superPoint_ . get () == 0 ||
superpointWeightsPath_ . compare ( previousWeightsPath ) != 0 ||
superpointModelPath_ . compare ( previousModelPath ) != 0 ||
previousCuda != cuda_ ||
previousThreshold != threshold_ ||
previousNms != nms_ ||
previousMinDistance != minDistance_ )
{
2025-11-16 11:37:03 -08:00
superPoint_ = cv :: Ptr < SPDetectorRpautrat > ( new SPDetectorRpautrat ( superpointWeightsPath_ , superpointModelPath_ , outputDir_ , threshold_ , nms_ , minDistance_ , cuda_ , this -> getMaxFeatures (), this -> getSSC ()));
}
else if ( superPoint_ . get () != 0 )
{
// Update post-processing parameters without reinitializing
superPoint_ -> setMaxFeatures ( this -> getMaxFeatures ());
superPoint_ -> setSSC ( this -> getSSC ());
2025-11-07 17:22:35 -08:00
}
#else
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Rpautrat feature cannot be used!" );
#endif
}
std :: vector < cv :: KeyPoint > SuperPointRpautrat :: generateKeypointsImpl ( const cv :: Mat & image , const cv :: Rect & roi , const cv :: Mat & mask )
{
#if defined(RTABMAP_TORCH) && defined(RTABMAP_PYTHON)
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
if ( roi . x != 0 || roi . y != 0 )
{
UERROR ( "SuperPoint Rpautrat: Not supporting ROI (%d,%d,%d,%d). Make sure %s, %s, %s, %s, %s, %s are all set to default values." ,
roi . x , roi . y , roi . width , roi . height ,
Parameters :: kKpRoiRatios (). c_str (),
Parameters :: kVisRoiRatios (). c_str (),
Parameters :: kVisGridRows (). c_str (),
Parameters :: kVisGridCols (). c_str (),
Parameters :: kKpGridRows (). c_str (),
Parameters :: kKpGridCols (). c_str ());
return std :: vector < cv :: KeyPoint > ();
}
return superPoint_ -> detect ( image , mask );
#else
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Rpautrat feature cannot be used!" );
return std :: vector < cv :: KeyPoint > ();
#endif
}
cv :: Mat SuperPointRpautrat :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
#if defined(RTABMAP_TORCH) && defined(RTABMAP_PYTHON)
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
2026-06-07 13:19:31 -07:00
cv :: Mat descriptors ;
if ( ! keypoints . empty ())
{
descriptors = superPoint_ -> compute ( keypoints );
if ( descriptors . empty ())
{
// superpoint may have been reset between keypoint detection and now,
// re-detect features to re-inialize the descriptors matrix, then
// re-extract descriptors with original keypoints.
UWARN ( "Re-initializing superpoint on that image to extract descriptors" );
if ( ! superPoint_ -> detect ( image ). empty ())
{
descriptors = superPoint_ -> compute ( keypoints );
if ( descriptors . rows == ( int ) keypoints . size ())
{
UWARN ( "Sucessfully re-initialized superpoint, returning %d descriptors." , descriptors . rows );
}
}
else
{
UWARN ( "Failed to re-initialize superpoint on that image, returning empty descriptors." );
}
}
}
return descriptors ;
2025-11-07 17:22:35 -08:00
#else
UWARN ( "RTAB-Map is not built with Torch support so SuperPoint Rpautrat feature cannot be used!" );
return cv :: Mat ();
#endif
2020-04-16 17:59:45 -04:00
}
2020-11-01 11:28:59 -05:00
//////////////////////////
//GFTT-DAISY
//////////////////////////
GFTT_DAISY :: GFTT_DAISY ( const ParametersMap & parameters ) :
GFTT ( parameters ),
orientationNormalized_ ( Parameters :: defaultFREAKOrientationNormalized ()),
scaleNormalized_ ( Parameters :: defaultFREAKScaleNormalized ()),
patternScale_ ( Parameters :: defaultFREAKPatternScale ()),
nOctaves_ ( Parameters :: defaultFREAKNOctaves ())
{
parseParameters ( parameters );
}
GFTT_DAISY ::~ GFTT_DAISY ()
{
}
void GFTT_DAISY :: parseParameters ( const ParametersMap & parameters )
{
GFTT :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kFREAKOrientationNormalized (), orientationNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKScaleNormalized (), scaleNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKPatternScale (), patternScale_ );
Parameters :: parse ( parameters , Parameters :: kFREAKNOctaves (), nOctaves_ );
#ifdef HAVE_OPENCV_XFEATURES2D
_daisy = CV_DAISY :: create ();
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so DAISY cannot be used!" );
#endif
}
cv :: Mat GFTT_DAISY :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
#ifdef HAVE_OPENCV_XFEATURES2D
_daisy -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so DAISY cannot be used!" );
#endif
return descriptors ;
}
//////////////////////////
//SURF-DAISY
//////////////////////////
SURF_DAISY :: SURF_DAISY ( const ParametersMap & parameters ) :
SURF ( parameters ),
orientationNormalized_ ( Parameters :: defaultFREAKOrientationNormalized ()),
scaleNormalized_ ( Parameters :: defaultFREAKScaleNormalized ()),
patternScale_ ( Parameters :: defaultFREAKPatternScale ()),
nOctaves_ ( Parameters :: defaultFREAKNOctaves ())
{
parseParameters ( parameters );
}
SURF_DAISY ::~ SURF_DAISY ()
{
}
void SURF_DAISY :: parseParameters ( const ParametersMap & parameters )
{
SURF :: parseParameters ( parameters );
Parameters :: parse ( parameters , Parameters :: kFREAKOrientationNormalized (), orientationNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKScaleNormalized (), scaleNormalized_ );
Parameters :: parse ( parameters , Parameters :: kFREAKPatternScale (), patternScale_ );
Parameters :: parse ( parameters , Parameters :: kFREAKNOctaves (), nOctaves_ );
#ifdef HAVE_OPENCV_XFEATURES2D
_daisy = CV_DAISY :: create ();
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so DAISY cannot be used!" );
#endif
}
cv :: Mat SURF_DAISY :: generateDescriptorsImpl ( const cv :: Mat & image , std :: vector < cv :: KeyPoint > & keypoints ) const
{
UASSERT ( ! image . empty () && image . channels () == 1 && image . depth () == CV_8U );
cv :: Mat descriptors ;
#ifdef HAVE_OPENCV_XFEATURES2D
_daisy -> compute ( image , keypoints , descriptors );
#else
UWARN ( "RTAB-Map is not built with OpenCV xfeatures2d module so DAISY cannot be used!" );
#endif
return descriptors ;
}
2012-12-11 18:05:05 +00:00
}