vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:11:13 +08:00
commit 12022378a3
3872 changed files with 2513409 additions and 0 deletions
@@ -0,0 +1,364 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2014, Biagio Montesano, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's 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.
//
// * The name of the copyright holders may not 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 Intel Corporation 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.
//
//M*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
/****************************************************************************************\
* Regression tests for line detector comparing keylines. *
\****************************************************************************************/
const std::string LINE_DESCRIPTOR_DIR = "line_descriptor";
const std::string IMAGE_FILENAME = "cameraman.jpg";
template<class Distance>
class CV_BD_DescriptorsTest : public cvtest::BaseTest
{
public:
typedef typename Distance::ValueType ValueType;
typedef typename Distance::ResultType DistanceType;
CV_BD_DescriptorsTest( std::string fs, DistanceType _maxDist ): maxDist(_maxDist)
{
bd = BinaryDescriptor::createBinaryDescriptor();
fs_name = fs;
}
protected:
// void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors );
// void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );
// virtual bool writeDescriptors( Mat& descs );
// virtual Mat readDescriptors();
// void emptyDataTest();
// void regressionTest();
// virtual void run( int );
Ptr<BinaryDescriptor> bd;
std::string fs_name;
const DistanceType maxDist;
Distance distance;
//};
void compareDescriptors( const Mat& validDescriptors, const Mat& calcDescriptors )
{
if( validDescriptors.size != calcDescriptors.size || validDescriptors.type() != calcDescriptors.type() )
{
ts->printf( cvtest::TS::LOG, "Valid and computed descriptors matrices must have the same size and type.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
CV_Assert( validDescriptors.type() == CV_8U );
int dimension = validDescriptors.cols;
DistanceType curMaxDist = std::numeric_limits<DistanceType>::min();
for ( int y = 0; y < validDescriptors.rows; y++ )
{
DistanceType dist = distance( validDescriptors.ptr<ValueType>( y ), calcDescriptors.ptr<ValueType>( y ), dimension );
if( dist > curMaxDist )
curMaxDist = dist;
}
EXPECT_LT(curMaxDist, maxDist) << "Max distance between valid and computed descriptors";
}
Mat readDescriptors()
{
Mat descriptors;
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name, FileStorage::READ );
fs["descriptors"] >> descriptors;
return descriptors;
}
bool writeDescriptors( Mat& descs )
{
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name, FileStorage::WRITE );
fs << "descriptors" << descs;
return true;
}
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output )
{
for ( int i = 0; i < inputMat.rows; i++ )
{
std::vector<float> tempFloat;
KeyLine kl;
float* pointerToRow = inputMat.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
tempFloat.push_back( *pointerToRow );
pointerToRow++;
}
kl.angle = tempFloat[0];
kl.class_id = (int) tempFloat[1];
kl.ePointInOctaveX = tempFloat[2];
kl.ePointInOctaveY = tempFloat[3];
kl.endPointX = tempFloat[4];
kl.endPointY = tempFloat[5];
kl.lineLength = tempFloat[6];
kl.numOfPixels = (int) tempFloat[7];
kl.octave = (int) tempFloat[8];
kl.pt.x = tempFloat[9];
kl.pt.y = tempFloat[10];
kl.response = tempFloat[11];
kl.sPointInOctaveX = tempFloat[12];
kl.sPointInOctaveY = tempFloat[13];
kl.size = tempFloat[14];
kl.startPointX = tempFloat[15];
kl.startPointY = tempFloat[16];
output.push_back( kl );
}
}
void emptyDataTest()
{
assert( bd );
// One image.
Mat image;
std::vector<KeyLine> keypoints;
Mat descriptors;
try
{
bd->compute( image, keypoints, descriptors );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on empty image and empty keypoints must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
image.create( 50, 50, CV_8UC3 );
try
{
bd->compute( image, keypoints, descriptors );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on nonempty image and empty keylines must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
// Several images.
std::vector<Mat> images;
std::vector<std::vector<KeyLine> > keylinesCollection;
std::vector<Mat> descriptorsCollection;
try
{
bd->compute( images, keylinesCollection, descriptorsCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "compute() on empty images and empty keylines collection must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
}
}
void regressionTest()
{
assert( bd );
// Read the test image.
std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;
Mat img = imread( imgFilename );
if( img.empty() )
{
ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
std::vector<KeyLine> keylines;
FileStorage fs( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/edl_detector_keylines_cameraman.yaml", FileStorage::READ );
if( fs.isOpened() )
{
//read( fs.getFirstTopLevelNode(), keypoints );
/* load keylines */
Mat loadedKeylines;
fs["keylines"] >> loadedKeylines;
createVecFromMat( loadedKeylines, keylines );
/* compute descriptors */
Mat calcDescriptors;
double t = (double) getTickCount();
bd->compute( img, keylines, calcDescriptors );
t = getTickCount() - t;
ts->printf( cvtest::TS::LOG, "\nAverage time of computing one descriptor = %g ms.\n",
t / ( (double) getTickFrequency() * 1000. ) / calcDescriptors.rows );
ASSERT_EQ((int)keylines.size(), calcDescriptors.rows)
<< "Count of computed descriptors and keylines count must be equal";
ASSERT_EQ(bd->descriptorSize() / 8, calcDescriptors.cols);
ASSERT_EQ(bd->descriptorType(), calcDescriptors.type());
// TODO read and write descriptor extractor parameters and check them
Mat validDescriptors = readDescriptors();
if( !validDescriptors.empty() )
compareDescriptors( validDescriptors, calcDescriptors );
else
{
if( !writeDescriptors( calcDescriptors ) )
{
ts->printf( cvtest::TS::LOG, "Descriptors can not be written.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
}
}
else
{
ts->printf( cvtest::TS::LOG, "Compute and write keylines.\n" );
fs.open( std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/edl_detector_keylines_cameraman.yaml", FileStorage::WRITE );
if( fs.isOpened() )
{
bd->detect( img, keylines );
Mat keyLinesToYaml;
createMatFromVec( keylines, keyLinesToYaml );
fs << "keylines" << keyLinesToYaml;
}
else
{
ts->printf( cvtest::TS::LOG, "File for writting keylines can not be opened.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
}
}
void run( int )
{
if( !bd )
{
ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
emptyDataTest();
regressionTest();
ts->set_failed_test_info( cvtest::TS::OK );
}
private:
CV_BD_DescriptorsTest& operator=( const CV_BD_DescriptorsTest& )
{
return *this;
}
};
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Descriptors, regression )
{
CV_BD_DescriptorsTest<Hamming> test( std::string( "lbd_descriptors_cameraman" ), 1 );
test.safe_run();
}
/****************************************************************************************\
* Other tests *
\****************************************************************************************/
TEST( BinaryDescriptor, no_lines_found )
{
Mat Image = Mat::zeros(100, 100, CV_8U);
Ptr<line_descriptor::BinaryDescriptor> binDescriptor =
line_descriptor::BinaryDescriptor::createBinaryDescriptor();
std::vector<cv::line_descriptor::KeyLine> keyLines;
binDescriptor->detect(Image, keyLines);
ASSERT_EQ(keyLines.size(), 0u);
}
}} // namespace
@@ -0,0 +1,340 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2014, Biagio Montesano, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's 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.
//
// * The name of the copyright holders may not 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 Intel Corporation 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.
//
//M*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
/****************************************************************************************\
* Regression tests for line detector comparing keylines. *
\****************************************************************************************/
const std::string LINE_DESCRIPTOR_DIR = "line_descriptor";
const std::string IMAGE_FILENAME = "cameraman.jpg";
class CV_BinaryDescriptorDetectorTest : public cvtest::BaseTest
{
public:
CV_BinaryDescriptorDetectorTest( std::string fs )
{
bd = BinaryDescriptor::createBinaryDescriptor();
fs_name = fs;
}
protected:
bool isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 );
void compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines );
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
void createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output );
void emptyDataTest();
void regressionTest();
virtual void run( int );
Ptr<BinaryDescriptor> bd;
std::string fs_name;
};
void CV_BinaryDescriptorDetectorTest::emptyDataTest()
{
/* one image */
Mat image;
std::vector<KeyLine> keylines;
try
{
bd->detect( image, keylines );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
if( !keylines.empty() )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image must return empty keylines vector (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
/* more than one image */
std::vector<Mat> images;
std::vector<std::vector<KeyLine> > keylineCollection;
try
{
bd->detect( images, keylineCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "detect() on empty image vector must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
void CV_BinaryDescriptorDetectorTest::createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
void CV_BinaryDescriptorDetectorTest::createVecFromMat( Mat& inputMat, std::vector<KeyLine>& output )
{
for ( int i = 0; i < inputMat.rows; i++ )
{
std::vector<float> tempFloat;
KeyLine kl;
float* pointerToRow = inputMat.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
tempFloat.push_back( *pointerToRow );
pointerToRow++;
}
kl.angle = tempFloat[0];
kl.class_id = (int) tempFloat[1];
kl.ePointInOctaveX = tempFloat[2];
kl.ePointInOctaveY = tempFloat[3];
kl.endPointX = tempFloat[4];
kl.endPointY = tempFloat[5];
kl.lineLength = tempFloat[6];
kl.numOfPixels = (int) tempFloat[7];
kl.octave = (int) tempFloat[8];
kl.pt.x = tempFloat[9];
kl.pt.y = tempFloat[10];
kl.response = tempFloat[11];
kl.sPointInOctaveX = tempFloat[12];
kl.sPointInOctaveY = tempFloat[13];
kl.size = tempFloat[14];
kl.startPointX = tempFloat[15];
kl.startPointY = tempFloat[16];
output.push_back( kl );
}
}
bool CV_BinaryDescriptorDetectorTest::isSimilarKeylines( const KeyLine& k1, const KeyLine& k2 )
{
const float maxPtDif = 1.f;
const float maxSizeDif = 1.f;
const float maxAngleDif = 2.f;
const float maxResponseDif = 0.1f;
float dist = (float)cv::norm(k1.pt - k2.pt);
return ( dist < maxPtDif && fabs( k1.size - k2.size ) < maxSizeDif && abs( k1.angle - k2.angle ) < maxAngleDif
&& abs( k1.response - k2.response ) < maxResponseDif && k1.octave == k2.octave && k1.class_id == k2.class_id );
}
void CV_BinaryDescriptorDetectorTest::compareKeylineSets( const std::vector<KeyLine>& validKeylines, const std::vector<KeyLine>& calcKeylines )
{
const float maxCountRatioDif = 0.01f;
// Compare counts of validation and calculated keylines.
float countRatio = (float) validKeylines.size() / (float) calcKeylines.size();
if( countRatio < 1 - maxCountRatioDif || countRatio > 1.f + maxCountRatioDif )
{
ts->printf( cvtest::TS::LOG, "Bad keylines count ratio (validCount = %d, calcCount = %d).\n", validKeylines.size(), calcKeylines.size() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
return;
}
int progress = 0;
int progressCount = (int) ( validKeylines.size() * calcKeylines.size() );
int badLineCount = 0;
int commonLineCount = max( (int) validKeylines.size(), (int) calcKeylines.size() );
for ( size_t v = 0; v < validKeylines.size(); v++ )
{
int nearestIdx = -1;
float minDist = std::numeric_limits<float>::max();
for ( size_t c = 0; c < calcKeylines.size(); c++ )
{
progress = update_progress( progress, (int) ( v * calcKeylines.size() + c ), progressCount, 0 );
float curDist = (float)cv::norm(calcKeylines[c].pt - validKeylines[v].pt);
if( curDist < minDist )
{
minDist = curDist;
nearestIdx = (int) c;
}
}
assert( minDist >= 0 );
if( !isSimilarKeylines( validKeylines[v], calcKeylines[nearestIdx] ) )
badLineCount++;
}
ts->printf( cvtest::TS::LOG, "badLineCount = %d; validLineCount = %d; calcLineCount = %d\n", badLineCount, validKeylines.size(),
calcKeylines.size() );
if( badLineCount > 0.9 * commonLineCount )
{
ts->printf( cvtest::TS::LOG, " - Bad accuracy!\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
return;
}
ts->printf( cvtest::TS::LOG, " - OK\n" );
}
void CV_BinaryDescriptorDetectorTest::regressionTest()
{
assert( bd );
std::string imgFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + IMAGE_FILENAME;
std::string resFilename = std::string( ts->get_data_path() ) + LINE_DESCRIPTOR_DIR + "/" + fs_name + ".yaml";
// Read the test image.
Mat image = imread( imgFilename );
if( image.empty() )
{
ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
// open a storage for reading
FileStorage fs( resFilename, FileStorage::READ );
// Compute keylines.
std::vector<KeyLine> calcKeylines;
bd->detect( image, calcKeylines );
if( fs.isOpened() ) // Compare computed and valid keylines.
{
// Read validation keylines set.
std::vector<KeyLine> validKeylines;
Mat storedKeylines;
fs["keylines"] >> storedKeylines;
createVecFromMat( storedKeylines, validKeylines );
if( validKeylines.empty() )
{
ts->printf( cvtest::TS::LOG, "keylines can not be read.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
compareKeylineSets( validKeylines, calcKeylines );
}
else // Write detector parameters and computed keylines as validation data.
{
fs.open( resFilename, FileStorage::WRITE );
if( !fs.isOpened() )
{
ts->printf( cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
else
{
fs << "detector_params" << "{";
bd->write( fs );
fs << "}";
Mat lines;
createMatFromVec( calcKeylines, lines );
fs << "keylines" << lines;
}
}
}
void CV_BinaryDescriptorDetectorTest::run( int )
{
if( !bd )
{
ts->printf( cvtest::TS::LOG, "Feature detector is empty.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return;
}
emptyDataTest();
regressionTest();
ts->set_failed_test_info( cvtest::TS::OK );
}
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Detector, regression )
{
CV_BinaryDescriptorDetectorTest test( std::string( "edl_detector_keylines_cameraman" ) );
test.safe_run();
}
}} // namespace
@@ -0,0 +1,6 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
CV_TEST_MAIN("cv")
@@ -0,0 +1,580 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2014, Biagio Montesano, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's 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.
//
// * The name of the copyright holders may not 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 Intel Corporation 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.
//
//M*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
class CV_BinaryDescriptorMatcherTest : public cvtest::BaseTest
{
public:
CV_BinaryDescriptorMatcherTest( float _badPart ) :
badPart( _badPart )
{
dmatcher = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
}
protected:
static const int dim = 32;
static const int queryDescCount = 300; // must be even number because we split train data in some cases in two
static const int countFactor = 4; // do not change it
const float badPart;
virtual void run( int );
void generateData( Mat& query, Mat& train );
uchar invertSingleBits( uchar dividend_char, int numBits );
void emptyDataTest();
void matchTest( const Mat& query, const Mat& train );
void knnMatchTest( const Mat& query, const Mat& train );
void radiusMatchTest( const Mat& query, const Mat& train );
std::string name;
Ptr<BinaryDescriptorMatcher> dmatcher;
private:
CV_BinaryDescriptorMatcherTest& operator=( const CV_BinaryDescriptorMatcherTest& )
{
return *this;
}
};
/* invert numBits bits in input char */
uchar CV_BinaryDescriptorMatcherTest::invertSingleBits( uchar dividend_char, int numBits )
{
std::vector<int> bin_vector;
long dividend;
long bin_num;
/* convert input char to a long */
dividend = (long) dividend_char;
/*if a 0 has been obtained, just generate a 8-bit long vector of zeros */
if( dividend == 0 )
bin_vector = std::vector<int>( 8, 0 );
/* else, apply classic decimal to binary conversion */
else
{
while ( dividend >= 1 )
{
bin_num = dividend % 2;
dividend /= 2;
bin_vector.push_back( bin_num );
}
}
/* ensure that binary vector always has length 8 */
if( bin_vector.size() < 8 )
{
std::vector<int> zeros( 8 - bin_vector.size(), 0 );
bin_vector.insert( bin_vector.end(), zeros.begin(), zeros.end() );
}
/* invert numBits bits */
for ( int index = 0; index < numBits; index++ )
{
if( bin_vector[index] == 0 )
bin_vector[index] = 1;
else
bin_vector[index] = 0;
}
/* reconvert to decimal */
uchar result = 0;
for ( int i = (int) bin_vector.size() - 1; i >= 0; i-- )
result += (uchar) ( bin_vector[i] * ( 1 << i ) );
return result;
}
void CV_BinaryDescriptorMatcherTest::emptyDataTest()
{
Mat queryDescriptors, trainDescriptors, mask;
std::vector<Mat> trainDescriptorCollection, masks;
std::vector<DMatch> matches;
std::vector<std::vector<DMatch> > vmatches;
try
{
dmatcher->match( queryDescriptors, trainDescriptors, matches, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->knnMatch( queryDescriptors, trainDescriptors, vmatches, 2, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->radiusMatch( queryDescriptors, trainDescriptors, vmatches, 10.f, mask );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->add( trainDescriptorCollection );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "add() on empty descriptors must not generate exception.\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->match( queryDescriptors, matches, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "match() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->knnMatch( queryDescriptors, vmatches, 2, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "knnMatch() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
try
{
dmatcher->radiusMatch( queryDescriptors, vmatches, 10.f, masks );
}
catch ( ... )
{
ts->printf( cvtest::TS::LOG, "radiusMatch() on empty descriptors must not generate exception (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
void CV_BinaryDescriptorMatcherTest::generateData( Mat& query, Mat& train )
{
RNG& rng = theRNG();
/* Generate query descriptors randomly.
Descriptor vector elements are binary values. */
Mat buf( queryDescCount, dim, CV_8UC1 );
rng.fill( buf, RNG::UNIFORM, Scalar( 0 ), Scalar( 255 ) );
buf.convertTo( query, CV_8UC1 );
for ( int i = 0; i < query.rows; i++ )
{
for ( int j = 0; j < countFactor; j++ )
{
train.push_back( query.row( i ) );
int randCol = rand() % 32;
uchar u = query.at<uchar>( i, randCol );
uchar modified_u = invertSingleBits( u, j + 1 );
train.at<uchar>( i * countFactor + j, randCol ) = modified_u;
}
}
}
void CV_BinaryDescriptorMatcherTest::matchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of match()
{
std::vector<DMatch> matches;
dmatcher->match( query, train, matches );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
badCount++;
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
// test const version of match() for the same query and test descriptors
{
std::vector<DMatch> matches;
dmatcher->match( query, query, matches );
if( (int) matches.size() != query.rows )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function for the same query and test descriptors (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( match.queryIdx != (int) i || match.trainIdx != (int) i || std::abs( match.distance ) > FLT_EPSILON )
{
ts->printf(
cvtest::TS::LOG,
"Bad match (i=%d, queryIdx=%d, trainIdx=%d, distance=%f) while test match() function for the same query and test descriptors (1).\n", i,
match.queryIdx, match.trainIdx, match.distance );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
}
// test version of match() with add()
{
dmatcher->clear();
std::vector<DMatch> matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks (make first nearest match illegal)
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
masks[mi] = Mat::ones( query.rows, 1/*train.rows / 2*/, CV_8UC1 );
dmatcher->match( query, matches, masks );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test match() function (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
DMatch& match = matches[i];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor /*+ shift*/) || ( match.imgIdx > 1 ) )
badCount++;
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test match() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
}
void CV_BinaryDescriptorMatcherTest::knnMatchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of knnMatch()
{
const int knn = 3;
std::vector<std::vector<DMatch> > matches;
dmatcher->knnMatch( query, train, matches, knn );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != knn )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < knn; k++ )
{
DMatch& match = matches[i][k];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
// // test version of knnMatch() with add()
{
const int knn = 2;
std::vector<std::vector<DMatch> > matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks (make first nearest match illegal)
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
{
masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
}
dmatcher->knnMatch( query, matches, knn, masks );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test knnMatch() function (2).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != knn )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < knn; k++ )
{
DMatch& match = matches[i][k];
{
if( i < queryDescCount / 2 )
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
else
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
localBadCount++;
}
}
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test knnMatch() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
}
void CV_BinaryDescriptorMatcherTest::radiusMatchTest( const Mat& query, const Mat& train )
{
dmatcher->clear();
// test const version of match()
{
const float radius = 1;
std::vector<std::vector<DMatch> > matches;
dmatcher->radiusMatch( query, train, matches, radius );
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
else
{
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != 1 )
{
badCount++;
}
else
{
DMatch& match = matches[i][0];
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor ) || ( match.imgIdx != 0 ) )
badCount++;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (1).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
}
}
{
const float radius = 3;
std::vector<std::vector<DMatch> > matches;
// make add() twice to test such case
dmatcher->add( std::vector<Mat>( 1, train.rowRange( 0, train.rows / 2 ) ) );
dmatcher->add( std::vector<Mat>( 1, train.rowRange( train.rows / 2, train.rows ) ) );
// prepare masks
std::vector<Mat> masks( 2 );
for ( int mi = 0; mi < 2; mi++ )
masks[mi] = Mat::ones( query.rows, 1, CV_8UC1 );
dmatcher->radiusMatch( query, matches, radius, masks );
//int curRes = cvtest::TS::OK;
if( (int) matches.size() != queryDescCount )
{
ts->printf( cvtest::TS::LOG, "Incorrect matches count while test radiusMatch() function (1).\n" );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
}
int badCount = 0;
for ( size_t i = 0; i < matches.size(); i++ )
{
if( (int) matches[i].size() != radius )
badCount++;
else
{
int localBadCount = 0;
for ( int k = 0; k < radius; k++ )
{
DMatch& match = matches[i][k];
{
if( i < queryDescCount / 2 )
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 0 ) )
localBadCount++;
}
else
{
if( ( match.queryIdx != (int) i ) || ( match.trainIdx != (int) i * countFactor + k ) || ( match.imgIdx != 1 ) )
localBadCount++;
}
}
}
badCount += localBadCount > 0 ? 1 : 0;
}
}
if( (float) badCount > (float) queryDescCount * badPart )
{
//curRes = cvtest::TS::FAIL_INVALID_OUTPUT;
ts->printf( cvtest::TS::LOG, "%f - too large bad matches part while test radiusMatch() function (2).\n",
(float) badCount / (float) queryDescCount );
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
}
}
}
void CV_BinaryDescriptorMatcherTest::run( int )
{
Mat query, train;
emptyDataTest();
generateData( query, train );
matchTest( query, train );
knnMatchTest( query, train );
radiusMatchTest( query, train );
}
/****************************************************************************************\
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Matcher, regression)
{
CV_BinaryDescriptorMatcherTest test( 0.01f );
test.safe_run();
}
}} // namespace
@@ -0,0 +1,14 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/line_descriptor.hpp"
namespace opencv_test {
using namespace cv::line_descriptor;
}
#endif