Files
rtabmap/corelib/test/test_vwdictionary.cpp

848 lines
28 KiB
C++
Raw Normal View History

Adding doc and tests (#1492) * added doc and tests for util2d.h * updated cmake-ros ci * Added util3d.h doc and tests * util3d_transforms.h: Added doc and tests * util3d_filtering.h: started doc and test * util3d_filtering.h: more tests and doc * Added more doc/tests * finished util3d_filtering doc and tests * added test for util2d::depthBleedingFiltering * Added util3d_registration tests * Added util3d_features.h doc/tests * added doc/tests for util3d_correspondences.h * added doc/gtest for util3d_mapping.h (missing hpp functions) * finished testing util3d_mapping.hpp * Added util3d_motion_estimation.h tests (2D->3D done) * finished util3d_motion_estimation.h tests * minimal util3d_surface.h * Added Transform and VisualWord tests * Added doc for CameraModel and StereoCameraModel * Added more logs in ros ci * Passing tests on fical * improved all devcontainer * added devcontainer kilted, fixed source setup.bash, removed ldconfig in ros-cmake workflow * cleanup * source ros * Added utilite tests * Added testing to appveyor, github actions cancellable on re-commit on same branch * appveyor testing without all targets * appveyor: specifying ALL_BUILD target * Fixed Util2dTest.NMSImageBoundsRespected test * Fixing PCL Indices error on old pcl * Added VWDictionary tests and doc. Fixed LSH not working (fix from https://github.com/flann-lib/flann/pull/472 * fixing some appveyor CI errors, added test to check dictionary serialization against all type * Added StereoDense, StereoBM and StereoSGBM doc and tests * Added Stereo tests * Added CameraModel and StereoCameraModel tests * Added doc and test for Statistics * Added doc/tests for Signature * Added doc/test for SensorEvent, added doc for SensorCaptureInfo * Added doc to SensorData * Added SensorData tests * Added SensorCapture and SensorCaptureThread doc and tests * fixed sensordata test * updated SSC test and doc * Added doc and tests for BayesFilter class * Enabled testing on mac, updated windows testing like on linux * added test_link * fixed unresolved on windows * fixed ThreadHandle error on macos ci * Added GPS and GeodeticCoords tests * Added tests for compression * Added Odometry tests (base class only) * Added DBDriver tests * Added coverage report * uniformized test names * fixing concurancy and coverage ci * dont built tools, examples and app for coverage build * fixed report tool rebuilt without qt compilation error * updated coverage option * updated coverage config * added doc CI job * fixing windows and mac ci errors * Added DBDriverSqlite3 tests * Added IMU tests * Added Graph tests * fixing flaky macos test * Added IMUThread and IMUFilter tests * Added Landmarks tests * Added LASWriter tests * fixing seed flaky test * fixing flaky macos timing tests * Added LocalGrid tests * Added LocalGridMaker tests * fixing ci errors * Added GlobalMap tests * Added doc for EnvSensor * Added Features2D tests * Added Registration tests * Added RegistrationVis tests * Added doc for Rtabmap and Memory classes * Added Memory and Rtabmap tests * making some tests less flaky * lcov 1.14 support * updated compatible tool arguments * Added integration tests (RGB-D, Stereo, Lidar2d, Lidar3d) * More octomap checks * Refactored how/when python interpretor is created to simplify library usage * Added python tests * fixed some flaky tests * suppressed some third party related warnings * fixed ceres tests * more flaky fixes * Fixing tests without libpointmatcher * Added RANSAC rejection filter to PCL ICP * fixing multi platform flakiness * Added test to detect regression * Fixing windows pcl link error * fixed some macos flakiness * bigger 2D2D registration error on opencv 4.6.0 * flakiness * fixing flaky tests on windows and mac * flaky thread test on slow mac VM * windows slow test * fixing more ci erros * fxing temp dir on windows * Added Optimizer tests and discovered some bugs (fixed) * fixing flaky tests in mac and windows * Added Optimizer doc * Added GTSAM BA, updated Ceres to use g2o ba parameters. Renamed g2o's ba related parameters to Optimizer group and used by both gtsam and ceres. * fixing build without gtsam * fixing home dir * fixing python ci isssues * Added multicam ba tests * Added Ceres multicam BA support * Aligned BundleAdjustment parameters with Optimizer/Strategy to avoid confusion in the code * Added BA integration test * Added robust graph optimization integration test * Added loop3it test * Added stereo20Hz test * Added smartfactor gtsam * Fixed bugged check and warn if python didn't return any descriptors * Fixing gtsam version build issues * fixing tilt on windows ci * loosing ceres integration test for ci * mac ci flakiness * updating missing param in gui * updating test bound for mac * added appearance-based tests, set min gftt quality to quality level * testing more stuff * improving features2d tests * ci flakiness * fixing flaky ci * ci fixes * flaky fixes * Added RegistrationIcp tests * Added icp integration test with real-worl corridor like env * intermediate nodes * fixing enum * Updated test to catch #1714 * Fixed 2d corridor failing on pcl * flaky pnp test * flaky brisk test * Set rtabmap_integration test as long * updating loop closure test * flaky ci tests * TEsting roundtrip g2o/toro save/load * loosing test bound * fixed cuda capable checks * flaky tests * Debugging test hanging * more debugging stuff * updating limit * windows: disabled cuda on ci to avoid incompatible driver issue. Fixing a bad test mem allocation * trying fixing cuda hanging issue * fixing ci flakyness * flaky tests * Updated BOW flaky tests by checking min precision/recall instead of recall@100precision. Fixed signature test * CameraModel::load() test initRectificationMap param * test dbdriver load dictionary idsOnly * Memory: test keepLinkedInDb param * added dummyDictionary tests * test intermediate nodes count * Added MarkerDetector tests * reverted breaking change of UMutex and USemaphore * Features2d: fixed compiltion warnings with clang about override * clang warnings * fixing test build with pcl 1.8 * g2o and gtsam build errors on android * opencv5 test fixes * disabled testing for ios and android builds * normalized endline characters for easier diff * added LF CRLF rule * bump 0.23.10. fixing doc version * Publish rtabmap website doc from ci * fixing MSCVC build error * macos icp flaky test * fixing ceres macos test bound * ficing more flaky tests * fixing opencv5 related test errors. Also fixed an actual bug in ENU_WGS84ToGeocentric_WGS84() * added comment about mrpt change * removed rosdoc2 (will add it for rtabmap_ros later) * fixing website style * updated download links * locally deployable website with api * sweep doxygen issues * improved/revised doxygen main pages * removed examples empty page * Updated doxygen style * more concise doxygen groups * added api link on main readme * fixing utilite test error * fixing CommonFilteringGroundNormalsUp test * updated precisionRecall test bounds for Freak and brief descriptors * fixing scale check in ba tests * disabled tests on windows cuda build (missing dlls amd runner cannot test cuda anyway) * ceres: missing suitesparse dep in windows ci * adjusting recall thr for fast/freak * ficing more flaky tests * fixing flaky tests * disabled coverage in ros ci * Enable integration tests for ros ci jobs * loosing up some threshold for failing tests * trigger cache * fixing test data in ros ci. Updated flaky test for mac * slaking some test limit * Fixed rtabmap-detectMoreLoopClosures inverted output value * loosing up sift recall on mac * optimizer re-ordered distribution for reproducible results (mac g2o) * macos dump test crash log * combining all tests to save time on shared library reload. Also fixed Logs with missing arguments. * Added ENABLE_FORMAT_ERRORS cmake option * do test only one time * fixed all format warnings * format security android build errors * less verbose tests * updated ImuUThread test * fixed a log * Fixed libpointmatcher 2d normals eigen issue * Fixing libpointmatcher conversion issues * fixing libpointmatcher test on windows ci * cleanup comments, relax some test thr * disabled sequoia-intel ci build (too flaky, would need extensive testing directly on that machine)
2026-08-06 13:32:20 -07:00
#include <gtest/gtest.h>
#include <opencv2/core.hpp>
#if CV_MAJOR_VERSION < 3
#ifdef HAVE_OPENCV_GPU
#include <opencv2/gpu/gpu.hpp>
#endif
#else
#include <opencv2/core/cuda.hpp>
#endif
#include "rtabmap/core/VWDictionary.h"
#include "rtabmap/core/VisualWord.h"
#include "rtabmap/core/Parameters.h"
#include "rtabmap/utilite/UFile.h"
#include <vector>
#include <list>
#include <set>
#include <iterator>
#include <fstream>
#include <cstdio>
using namespace rtabmap;
class VWDictionaryTest : public ::testing::Test {
protected:
void SetUp() override {
// Create a dictionary with default parameters
dict = new VWDictionary();
}
void TearDown() override {
delete dict;
}
VWDictionary* dict;
};
TEST_F(VWDictionaryTest, Constructor)
{
EXPECT_TRUE(dict != nullptr);
EXPECT_TRUE(dict->isIncremental());
EXPECT_EQ(dict->getVisualWords().size(), 0u);
EXPECT_EQ(dict->getTotalActiveReferences(), 0);
EXPECT_EQ(dict->getIndexedWordsCount(), 0u);
}
TEST_F(VWDictionaryTest, AddNewWordsIncremental)
{
// Test incremental mode - NNDR is applied, new words created if NNDR fails
// Test with all NNStrategy values
VWDictionary::NNStrategy strategies[] = {
VWDictionary::kNNFlannNaive,
VWDictionary::kNNFlannKdTree,
VWDictionary::kNNFlannLSH,
VWDictionary::kNNBruteForce,
VWDictionary::kNNBruteForceGPU
};
// That will mke logic below works with numbers chosen
ParametersMap params;
params.insert(ParametersPair(Parameters::kKpNndrRatio(), "0.4"));
dict->parseParameters(params);
for(VWDictionary::NNStrategy strategy : strategies)
{
if(strategy == VWDictionary::kNNBruteForceGPU)
{
#if CV_MAJOR_VERSION < 3
#ifdef HAVE_OPENCV_GPU
if(cv::gpu::getCudaEnabledDeviceCount() <= 0)
{
strategy = VWDictionary::kNNBruteForce;
}
#else
strategy = VWDictionary::kNNBruteForce;
#endif
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
if(cv::cuda::getCudaEnabledDeviceCount() <= 0)
{
strategy = VWDictionary::kNNBruteForce;
}
#else
strategy = VWDictionary::kNNBruteForce;
#endif
#endif
}
// Reset dictionary for each strategy
dict->clear();
dict->setNNStrategy(strategy);
EXPECT_TRUE(dict->isIncremental());
EXPECT_EQ(dict->getNNStrategy(), strategy);
// Add initial words to dictionary (2D descriptors)
// Word 1: (0, 0)
// Word 2: (15, 0)
// Word 3: (0, 255)
// Using dimension 8 to support LSH
cv::Mat initialDescriptors = (cv::Mat_<float>(3, 8) <<
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 255.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
// Convert to binary if using LSH strategy
if(strategy == VWDictionary::kNNFlannLSH)
{
// Convert float descriptors to binary
initialDescriptors = VWDictionary::convert32FToBin(initialDescriptors, true);
std::cout << initialDescriptors << std::endl;
}
std::list<int> addedIds = dict->addNewWords(initialDescriptors, 1);
dict->update();
unsigned int initialWordCount = dict->getVisualWords().size();
EXPECT_FALSE(addedIds.empty()) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
EXPECT_EQ(initialWordCount, 3u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
EXPECT_EQ(addedIds.back(), dict->getVisualWords().rbegin()->first) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Get the maximum initial word ID
int maxInitialId = dict->getVisualWords().rbegin()->first;
// Create query descriptors with known distances
// Query 1: (1, 0) - very close to Word 1 (0,0), far from others
// Distance to Word 1: sqrt(1^2 + 0^2) ≈ 1 (LSH 1)
// Distance to Word 2: sqrt(6^2 + 0^2) ≈ 6 (LSH 4)
// Ratio: 1 / 6 ≈ 0.16 < NNDR threshold (typically 0.4) - should PASS NNDR
// LSH Ratio: 1/4 = 0.25 < NNDR
//
// Query 2: (9, 0) - "equidistant" from Word 1 and Word 2
// Distance to Word 1: 9.0 (LSH 1)
// Distance to Word 2: 6.0 (LSH 2)
// Ratio: 36 / 81 = 0.44 > NNDR threshold - should FAIL NNDR (new word created)
// LSH Ratio: 1/2 = 0.5 > NNDR
cv::Mat queryDescriptors = (cv::Mat_<float>(2, 8) <<
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Should match Word 1 (passes NNDR)
9.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); // Should create new word (fails NNDR)
// Convert to binary if using LSH strategy
if(strategy == VWDictionary::kNNFlannLSH)
{
// Convert float descriptors to binary
queryDescriptors = VWDictionary::convert32FToBin(queryDescriptors, true);
std::cout << queryDescriptors << std::endl;
}
int signatureId = 2;
std::list<int> wordIds = dict->addNewWords(queryDescriptors, signatureId);
// In incremental mode, valid word IDs are returned only if NNDR validation passes
// Otherwise, new words are created
EXPECT_EQ(wordIds.size(), 2u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// First query should match existing word (NNDR passed)
int firstId = *wordIds.begin();
EXPECT_EQ(firstId, VWDictionary::ID_START) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
EXPECT_NE(dict->getWord(firstId), nullptr) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Second query should create a new word (NNDR failed)
int secondId = *std::next(wordIds.begin());
EXPECT_GT(secondId, maxInitialId) << "Strategy: " << VWDictionary::nnStrategyName(strategy); // Should be a new word ID
EXPECT_NE(dict->getWord(secondId), nullptr) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Total words should increase by 1 (one new word created)
EXPECT_EQ(dict->getVisualWords().size(), initialWordCount + 1) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
}
}
TEST_F(VWDictionaryTest, AddNewWordsFixed)
{
// Test fixed mode - NNDR is not applied, closest match is always returned
// Create a temporary dictionary file
std::string dictFile = "test_vwdictionary_fixed_dict.txt";
// Write dictionary file in expected format:
// First line: dimension
// Subsequent lines: word_id descriptor_value1 descriptor_value2 ...
std::ofstream file(dictFile);
ASSERT_TRUE(file.is_open());
// Write header with dimension
file << "2" << std::endl;
// Write words: Word 1: (0, 0), Word 2: (10, 5), Word 3: (0, 100)
file << "1 0.0 0.0" << std::endl;
file << "2 10 0.0" << std::endl;
file << "3 0.0 100.0" << std::endl;
file.close();
// Load fixed dictionary from file
dict->setFixedDictionary(dictFile);
EXPECT_FALSE(dict->isIncremental());
dict->update();
unsigned int initialWordCount = dict->getVisualWords().size();
EXPECT_EQ(initialWordCount, 3u);
// Create query descriptors with known distances
// Query 1: (0.5, 0.5) - closest to Word 1 (0,0), distance ≈ 0.707
// Query 2: (5.1, 0) - slighlty closer to Word 2 (10,5) than Word 1 (0,0), with distances 4.9 and 5.0 respectively
cv::Mat queryDescriptors = (cv::Mat_<float>(2, 2) <<
0.5f, 0.5f, // Closest to Word 1
5.1f, 0.0f); // Closest to Word2 but would not pass NNDR (4.9/5 = 0.98 > 0.8 default NNDR)
int signatureId = 2;
std::list<int> wordIds = dict->addNewWords(queryDescriptors, signatureId);
// In fixed mode, closest visual word ID is always returned (no NNDR check)
EXPECT_EQ(wordIds.size(), 2u);
// First query should match Word 1 (closest match: distance 0.707 to Word 1 vs 9.513 to Word 2)
int firstId = *wordIds.begin();
EXPECT_EQ(firstId, VWDictionary::ID_START);
EXPECT_NE(dict->getWord(firstId), nullptr);
// Second query should also match an existing word (closest match, no NNDR check)
// Query (5.1, 0) is slighlty closer to Word 2
int secondId = *std::next(wordIds.begin());
EXPECT_EQ(secondId, 2);
EXPECT_NE(dict->getWord(secondId), nullptr);
// In fixed mode, no new words should be created
EXPECT_EQ(dict->getVisualWords().size(), initialWordCount);
// Cleanup: remove temporary dictionary file
UFile::erase(dictFile);
}
TEST_F(VWDictionaryTest, AddWord)
{
cv::Mat descriptor = (cv::Mat_<float>(1, 64) <<
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
VisualWord* word = new VisualWord(100, descriptor, 10);
dict->addWord(word);
EXPECT_EQ(dict->getVisualWords().size(), 1u);
const VisualWord* retrieved = dict->getWord(100);
EXPECT_NE(retrieved, nullptr);
EXPECT_EQ(retrieved->id(), 100);
}
TEST_F(VWDictionaryTest, FindNNIncremental)
{
// Test incremental mode - NNDR is applied
EXPECT_TRUE(dict->isIncremental());
// Add initial words to dictionary (2D descriptors)
// Word 1: (0, 0)
// Word 2: (10, 0)
// Word 3: (0, 100)
cv::Mat initialDescriptors = (cv::Mat_<float>(3, 2) <<
0.0f, 0.0f,
10.0f, 0.0f,
0.0f, 100.0f);
dict->addNewWords(initialDescriptors, 1);
dict->update();
// Create query descriptors with known distances
// Query 1: (0.5, 0.5) - very close to Word 1 (0,0), far from others
// Distance to Word 1: sqrt(0.5^2 + 0.5^2) ≈ 0.707
// Distance to Word 2: sqrt(9.5^2 + 0.5^2) ≈ 9.513
// Ratio: 0.707 / 9.513 ≈ 0.074 < NNDR threshold (typically 0.8) - should PASS NNDR
//
// Query 2: (5, 0) - equidistant from Word 1 and Word 2
// Distance to Word 1: 5.0
// Distance to Word 2: 5.0
// Ratio: 5.0 / 5.0 = 1.0 > NNDR threshold - should FAIL NNDR (return ID_INVALID)
cv::Mat queryDescriptors = (cv::Mat_<float>(2, 2) <<
0.5f, 0.5f, // Should match Word 1 (passes NNDR)
5.0f, 0.0f); // Should fail NNDR (returns ID_INVALID)
std::vector<int> matches = dict->findNN(queryDescriptors);
EXPECT_EQ(matches.size(), 2u);
// First query should match existing word (NNDR passed)
int firstId = matches[0];
EXPECT_EQ(firstId, VWDictionary::ID_START);
EXPECT_NE(dict->getWord(firstId), nullptr);
// Second query should fail NNDR (returns ID_INVALID)
int secondId = matches[1];
EXPECT_EQ(secondId, VWDictionary::ID_INVALID);
}
TEST_F(VWDictionaryTest, FindNNFixed)
{
// Test fixed mode - NNDR is not applied, closest match is always returned
// Create a temporary dictionary file
std::string dictFile = "test_vwdictionary_fixed_dict_findnn.txt";
// Write dictionary file in expected format:
// First line: dimension
// Subsequent lines: word_id descriptor_value1 descriptor_value2 ...
std::ofstream file(dictFile);
ASSERT_TRUE(file.is_open());
// Write header with dimension
file << "2" << std::endl;
// Write words: Word 1: (0, 0), Word 2: (10, 0), Word 3: (0, 100)
file << "1 0.0 0.0" << std::endl;
file << "2 10 0.0" << std::endl;
file << "3 0.0 100.0" << std::endl;
file.close();
// Load fixed dictionary from file
dict->setFixedDictionary(dictFile);
EXPECT_FALSE(dict->isIncremental());
dict->update();
unsigned int initialWordCount = dict->getVisualWords().size();
EXPECT_EQ(initialWordCount, 3u);
// Create query descriptors with known distances
// Query 1: (0.5, 0.5) - closest to Word 1 (0,0), distance ≈ 0.707
// Query 2: (5.1, 0) - slightly closer to Word 2 (10,0) than Word 1 (0,0), with distances 4.9 and 5.0 respectively
cv::Mat queryDescriptors = (cv::Mat_<float>(2, 2) <<
0.5f, 0.5f, // Closest to Word 1
5.1f, 0.0f); // Closest to Word 2 but would not pass NNDR (4.9/5 = 0.98 > 0.8 default NNDR)
std::vector<int> matches = dict->findNN(queryDescriptors);
EXPECT_EQ(matches.size(), 2u);
// First query should match Word 1 (closest match: distance 0.707 to Word 1 vs 9.513 to Word 2)
int firstId = matches[0];
EXPECT_EQ(firstId, VWDictionary::ID_START);
EXPECT_NE(dict->getWord(firstId), nullptr);
// Second query should also match an existing word (closest match, no NNDR check)
// Query (5.1, 0) is slightly closer to Word 2
int secondId = matches[1];
EXPECT_EQ(secondId, 2);
EXPECT_NE(dict->getWord(secondId), nullptr);
// Cleanup: remove temporary dictionary file
UFile::erase(dictFile);
}
TEST_F(VWDictionaryTest, AddWordRef)
{
cv::Mat descriptor = cv::Mat::ones(1, 32, CV_32F);
std::list<int> wordIds = dict->addNewWords(descriptor, 1);
ASSERT_EQ(wordIds.size(), 1u);
int wordId = wordIds.front();
dict->addWordRef(wordId, 2);
dict->addWordRef(wordId, 3);
EXPECT_EQ(dict->getTotalActiveReferences(), 3); // 1 from addNewWords + 2 from addWordRef
const VisualWord* word = dict->getWord(wordId);
EXPECT_NE(word, nullptr);
EXPECT_EQ(word->getTotalReferences(), 3);
}
TEST_F(VWDictionaryTest, RemoveAllWordRef)
{
cv::Mat descriptor = cv::Mat::ones(1, 32, CV_32F);
std::list<int> wordIds = dict->addNewWords(descriptor, 1);
ASSERT_EQ(wordIds.size(), 1u);
int wordId = wordIds.front();
dict->addWordRef(wordId, 2);
dict->addWordRef(wordId, 3);
EXPECT_EQ(dict->getTotalActiveReferences(), 3);
dict->removeAllWordRef(wordId, 1);
EXPECT_EQ(dict->getTotalActiveReferences(), 2);
dict->removeAllWordRef(wordId, 2);
dict->removeAllWordRef(wordId, 3);
// Word should now be unused
EXPECT_EQ(dict->getTotalActiveReferences(), 0);
EXPECT_EQ(dict->getUnusedWordsSize(), 1u);
}
TEST_F(VWDictionaryTest, GetWord)
{
cv::Mat descriptor = cv::Mat::ones(1, 32, CV_32F);
std::list<int> wordIds = dict->addNewWords(descriptor, 1);
ASSERT_EQ(wordIds.size(), 1u);
int wordId = wordIds.front();
const VisualWord* word = dict->getWord(wordId);
EXPECT_NE(word, nullptr);
EXPECT_EQ(word->id(), wordId);
EXPECT_EQ(word->getDescriptor().cols, 32);
// Test with invalid ID
const VisualWord* invalid = dict->getWord(99999);
EXPECT_EQ(invalid, nullptr);
}
TEST_F(VWDictionaryTest, GetUnusedWords)
{
cv::Mat descriptor = cv::Mat::ones(1, 32, CV_32F);
std::list<int> wordIds = dict->addNewWords(descriptor, 1);
ASSERT_EQ(wordIds.size(), 1u);
int wordId = wordIds.front();
// Initially word has a reference, so it's not unused
EXPECT_EQ(dict->getUnusedWordsSize(), 0u);
// Remove all references
dict->removeAllWordRef(wordId, 1);
EXPECT_EQ(dict->getUnusedWordsSize(), 1u);
std::vector<VisualWord*> unused = dict->getUnusedWords();
EXPECT_EQ(unused.size(), 1u);
EXPECT_EQ(unused[0]->id(), wordId);
std::vector<int> unusedIds = dict->getUnusedWordIds();
EXPECT_EQ(unusedIds.size(), 1u);
EXPECT_EQ(unusedIds[0], wordId);
}
TEST_F(VWDictionaryTest, ConvertBinTo32FByteToFloat)
{
// Test byteToFloat = true (simple conversion)
cv::Mat input(2, 10, CV_8UC1);
cv::randu(input, cv::Scalar(0), cv::Scalar(255));
cv::Mat output = VWDictionary::convertBinTo32F(input, true);
EXPECT_EQ(output.type(), CV_32FC1);
EXPECT_EQ(output.rows, 2);
EXPECT_EQ(output.cols, 10); // Same dimensions
}
TEST_F(VWDictionaryTest, ConvertBinTo32FBitExpansion)
{
// Test byteToFloat = false (bit expansion)
cv::Mat input(1, 4, CV_8UC1);
input.at<unsigned char>(0, 0) = 0b10101010; // 170
input.at<unsigned char>(0, 1) = 0b01010101; // 85
input.at<unsigned char>(0, 2) = 0b11110000; // 240
input.at<unsigned char>(0, 3) = 0b00001111; // 15
cv::Mat output = VWDictionary::convertBinTo32F(input, false);
EXPECT_EQ(output.type(), CV_32FC1);
EXPECT_EQ(output.rows, 1);
EXPECT_EQ(output.cols, 32); // 4 bytes * 8 bits = 32 floats
// Check first byte expansion (10101010)
EXPECT_FLOAT_EQ(output.at<float>(0, 0), 0.0f); // bit 0
EXPECT_FLOAT_EQ(output.at<float>(0, 1), 1.0f); // bit 1
EXPECT_FLOAT_EQ(output.at<float>(0, 2), 0.0f); // bit 2
EXPECT_FLOAT_EQ(output.at<float>(0, 3), 1.0f); // bit 3
}
TEST_F(VWDictionaryTest, Convert32FToBinByteToFloat)
{
// Test byteToFloat = true (simple conversion)
cv::Mat input(2, 10, CV_32FC1);
cv::randu(input, cv::Scalar(0), cv::Scalar(255));
cv::Mat output = VWDictionary::convert32FToBin(input, true);
EXPECT_EQ(output.type(), CV_8UC1);
EXPECT_EQ(output.rows, 2);
EXPECT_EQ(output.cols, 10); // Same dimensions
}
TEST_F(VWDictionaryTest, Convert32FToBinBitPacking)
{
// Test byteToFloat = false (bit packing)
cv::Mat input(1, 32, CV_32FC1);
// Set first 8 floats to represent 10101010
input.at<float>(0, 0) = 0.0f; // bit 0
input.at<float>(0, 1) = 1.0f; // bit 1
input.at<float>(0, 2) = 0.0f; // bit 2
input.at<float>(0, 3) = 1.0f; // bit 3
input.at<float>(0, 4) = 0.0f; // bit 4
input.at<float>(0, 5) = 1.0f; // bit 5
input.at<float>(0, 6) = 0.0f; // bit 6
input.at<float>(0, 7) = 1.0f; // bit 7
// Rest set to 0
for(int i = 8; i < 32; ++i) {
input.at<float>(0, i) = 0.0f;
}
cv::Mat output = VWDictionary::convert32FToBin(input, false);
EXPECT_EQ(output.type(), CV_8UC1);
EXPECT_EQ(output.rows, 1);
EXPECT_EQ(output.cols, 4); // 32 floats / 8 = 4 bytes
EXPECT_EQ(output.at<unsigned char>(0, 0), 0b10101010);
}
TEST_F(VWDictionaryTest, ConvertRoundTrip)
{
// Test round trip conversion with bit expansion
cv::Mat original(1, 4, CV_8UC1);
cv::randu(original, cv::Scalar(0), cv::Scalar(255));
cv::Mat expanded = VWDictionary::convertBinTo32F(original, false);
cv::Mat packed = VWDictionary::convert32FToBin(expanded, false);
EXPECT_EQ(packed.rows, original.rows);
EXPECT_EQ(packed.cols, original.cols);
EXPECT_EQ(packed.type(), original.type());
for(int i = 0; i < original.cols; ++i) {
EXPECT_EQ(packed.at<unsigned char>(0, i), original.at<unsigned char>(0, i));
}
}
TEST_F(VWDictionaryTest, SetNNStrategy)
{
// Test resetting same strategy
bool reinit = dict->setNNStrategy(dict->getNNStrategy());
EXPECT_FALSE(reinit);
// Test changing strategy
reinit = dict->setNNStrategy(VWDictionary::kNNBruteForce);
EXPECT_TRUE(reinit);
// Add some words and index them
cv::Mat descriptors(5, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
dict->addNewWords(descriptors, 1);
dict->update();
// Change strategy should reinitialize
reinit = dict->setNNStrategy(VWDictionary::kNNFlannKdTree);
EXPECT_TRUE(reinit);
}
TEST_F(VWDictionaryTest, NNStrategyName)
{
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNFlannNaive), "FLANN NAIVE");
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNFlannKdTree), "FLANN KD-TREE");
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNFlannLSH), "FLANN LSH");
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNBruteForce), "BRUTE FORCE");
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNBruteForceGPU), "BRUTE FORCE GPU");
EXPECT_EQ(VWDictionary::nnStrategyName(VWDictionary::kNNUndef), "Unknown");
}
TEST_F(VWDictionaryTest, IncrementalDictionary)
{
EXPECT_TRUE(dict->isIncremental());
dict->setIncrementalDictionary();
EXPECT_TRUE(dict->isIncremental());
}
TEST_F(VWDictionaryTest, GetNndrRatio)
{
float ratio = dict->getNndrRatio();
EXPECT_GT(ratio, 0.0f);
EXPECT_LE(ratio, 1.0f);
}
TEST_F(VWDictionaryTest, Update)
{
// Add words without updating index
cv::Mat descriptors(3, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
dict->addNewWords(descriptors, 1);
unsigned int notIndexed = dict->getNotIndexedWordsCount();
EXPECT_GT(notIndexed, 0u);
dict->update();
// After update, words should be indexed
EXPECT_GT(dict->getIndexedWordsCount(), 0u);
}
TEST_F(VWDictionaryTest, Clear)
{
// Add some words
cv::Mat descriptors(5, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
dict->addNewWords(descriptors, 1);
EXPECT_GT(dict->getVisualWords().size(), 0u);
dict->clear();
EXPECT_EQ(dict->getVisualWords().size(), 0u);
EXPECT_EQ(dict->getTotalActiveReferences(), 0);
EXPECT_EQ(dict->getIndexedWordsCount(), 0u);
}
TEST_F(VWDictionaryTest, MemoryUsed)
{
unsigned long memBefore = dict->getMemoryUsed();
// Add some words
cv::Mat descriptors(10, 128, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
dict->addNewWords(descriptors, 1);
dict->update();
unsigned long memAfter = dict->getMemoryUsed();
EXPECT_GT(memAfter, memBefore);
unsigned int indexMem = dict->getIndexMemoryUsed();
EXPECT_GT(indexMem, 0u);
}
TEST_F(VWDictionaryTest, SerializeDeserializeIndex)
{
// Test with all NNStrategy values
VWDictionary::NNStrategy strategies[] = {
VWDictionary::kNNFlannNaive,
VWDictionary::kNNFlannKdTree,
VWDictionary::kNNFlannLSH,
VWDictionary::kNNBruteForce,
VWDictionary::kNNBruteForceGPU
};
for(VWDictionary::NNStrategy strategy : strategies)
{
if(strategy == VWDictionary::kNNBruteForceGPU)
{
#if CV_MAJOR_VERSION < 3
#ifdef HAVE_OPENCV_GPU
if(cv::gpu::getCudaEnabledDeviceCount() <= 0)
{
continue; // Skip if no GPU available
}
#else
continue; // Skip if GPU support not compiled
#endif
#else
#ifdef HAVE_OPENCV_CUDAFEATURES2D
if(cv::cuda::getCudaEnabledDeviceCount() <= 0)
{
continue; // Skip if no GPU available
}
#else
continue; // Skip if GPU support not compiled
#endif
#endif
}
// Reset dictionary for each strategy
dict->clear();
dict->setNNStrategy(strategy);
// Add words and build index
cv::Mat descriptors(5, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
// Convert to binary if using LSH strategy
if(strategy == VWDictionary::kNNFlannLSH)
{
// Convert float descriptors to binary
descriptors = VWDictionary::convert32FToBin(descriptors, true);
}
dict->addNewWords(descriptors, 1);
dict->update();
// Serialize
std::vector<unsigned char> data = dict->serializeIndex();
#ifdef _WIN32
// FlannIndex::serializeIndex() is not implemented on Windows
// (see corelib/src/FlannIndex.cpp), so it always returns empty
// data regardless of the strategy. Skip the rest of the
// round-trip assertions on Windows.
EXPECT_EQ(data.size(), 0u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
continue;
#else
if(strategy < VWDictionary::kNNBruteForce)
{
// flann strategies
EXPECT_GT(data.size(), 0u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
}
else {
EXPECT_EQ(data.size(), 0u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
}
#endif
// Create new dictionary and deserialize
VWDictionary dict2;
dict2.setNNStrategy(strategy);
cv::Mat descriptors2(5, 32, CV_32F);
cv::randu(descriptors2, cv::Scalar(0), cv::Scalar(1));
// Convert to binary if using LSH strategy
if(strategy == VWDictionary::kNNFlannLSH)
{
descriptors2 = VWDictionary::convert32FToBin(descriptors2, true);
}
dict2.addNewWords(descriptors2, 1);
// Deserialize should fail because we are not using the same descriptors
bool success = dict2.deserializeIndex(data);
EXPECT_FALSE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
success = dict2.deserializeIndex(data.data(), data.size());
EXPECT_FALSE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Same descriptors
VWDictionary dict3;
dict3.setNNStrategy(strategy);
dict3.addNewWords(descriptors, 1);
success = dict3.deserializeIndex(data);
if(strategy < VWDictionary::kNNBruteForce)
{
// flann strategies
EXPECT_TRUE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Index should be loaded
EXPECT_GT(dict3.getIndexedWordsCount(), 0u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
}
else
{
EXPECT_FALSE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
continue;
}
// Should fail if index is already built
success = dict3.deserializeIndex(data);
EXPECT_FALSE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// raw bytes
VWDictionary dict4;
dict4.setNNStrategy(strategy);
dict4.addNewWords(descriptors, 1);
success = dict4.deserializeIndex(data.data(), data.size());
EXPECT_TRUE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Index should be loaded
EXPECT_GT(dict4.getIndexedWordsCount(), 0u) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
// Should fail if index is already built
success = dict4.deserializeIndex(data.data(), data.size());
EXPECT_FALSE(success) << "Strategy: " << VWDictionary::nnStrategyName(strategy);
}
}
TEST_F(VWDictionaryTest, IsModified)
{
EXPECT_TRUE(dict->isModified());
// Adding words should keep it modified
cv::Mat descriptors(3, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
dict->addNewWords(descriptors, 1);
EXPECT_TRUE(dict->isModified());
}
TEST_F(VWDictionaryTest, SetLastWordId)
{
dict->setLastWordId(100);
// Add a word - should get ID > 100
cv::Mat descriptor = cv::Mat::ones(1, 32, CV_32F);
std::list<int> wordIds = dict->addNewWords(descriptor, 1);
ASSERT_EQ(wordIds.size(), 1u);
EXPECT_GT(wordIds.front(), 100);
}
TEST_F(VWDictionaryTest, DeleteUnusedWords)
{
// Add words (orthogonal descriptors so each gets its own visual word)
cv::Mat descriptors(3, 32, CV_32F, cv::Scalar(0));
for(int i = 0; i < 3; ++i)
{
descriptors.at<float>(i, i) = 1.0f;
}
std::list<int> wordIds = dict->addNewWords(descriptors, 1);
ASSERT_EQ(wordIds.size(), 3u);
std::set<int> uniqueWordIds(wordIds.begin(), wordIds.end());
ASSERT_EQ(uniqueWordIds.size(), 3u);
// Remove references to make words unused
for(int id : uniqueWordIds) {
dict->removeAllWordRef(id, 1);
}
EXPECT_EQ(dict->getUnusedWordsSize(), 3u);
// Delete unused words
dict->deleteUnusedWords();
EXPECT_EQ(dict->getUnusedWordsSize(), 0u);
EXPECT_EQ(dict->getVisualWords().size(), 0u);
}
TEST_F(VWDictionaryTest, FindNNWithVisualWords)
{
// Add words
cv::Mat descriptors(5, 32, CV_32F);
cv::randu(descriptors, cv::Scalar(0), cv::Scalar(1));
std::list<int> wordIds = dict->addNewWords(descriptors, 1);
dict->update();
// Create list of visual words to match (create new words with same descriptors)
std::list<VisualWord*> vws;
for(int id : wordIds) {
const VisualWord* word = dict->getWord(id);
// Create a new VisualWord with the same descriptor for testing
VisualWord* testWord = new VisualWord(id + 1000, word->getDescriptor().clone());
vws.push_back(testWord);
}
// That will force to accept close matches
ParametersMap params;
params.insert(ParametersPair(Parameters::kKpNndrRatio(), "1"));
dict->parseParameters(params);
std::vector<int> matches = dict->findNN(vws);
EXPECT_EQ(matches.size(), vws.size());
// Each word should match itself
int i =0;
for(VisualWord * vw: vws) {
EXPECT_EQ(vw->id()-1000, matches[i++]);
}
// Cleanup
for(VisualWord* vw : vws) {
delete vw;
}
}