Commit 92761997 authored by biagio montesano's avatar biagio montesano

Added perf and accuracy tests

parent 30ed7277
/*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 "perf_precomp.hpp"
using namespace cv;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"line_descriptor_data/cameraman.jpg", "line_descriptor_data/lena.bmp"
PERF_TEST_P(file_str, descriptors, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat descriptors;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
bd->compute( frame, keylines, descriptors );
}
SANITY_CHECK( descriptors );
}
/*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 "perf_precomp.hpp"
using namespace cv;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"line_descriptor_data/cameraman.jpg", "line_descriptor_data/lena.bmp"
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
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++;
}
}
}
PERF_TEST_P(file_str, detect, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
createMatFromVec( keylines, lines );
}
SANITY_CHECK( lines );
}
PERF_TEST_P(file_str, detect_lsd, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
std::cout << filename.c_str() << std::endl;
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
TEST_CYCLE()
{
lsd->detect( frame, keylines, 2, 1 );
createMatFromVec( keylines, lines );
}
SANITY_CHECK( lines );
}
/*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 "perf_precomp.hpp"
CV_PERF_TEST_MAIN( line_descriptor )
/*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 "perf_precomp.hpp"
using namespace cv;
using namespace std;
using namespace perf;
using std::tr1::make_tuple;
using std::tr1::get;
#define QUERY_DES_COUNT 300
#define DIM 32
#define COUNT_FACTOR 4
#define RADIUS 3
void generateData( Mat& query, Mat& train );
uchar invertSingleBits( uchar dividend_char, int numBits );
/* invert numBits bits in input char */
uchar 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] * pow( 2, i ) );
return result;
}
void generateData( Mat& query, Mat& train )
{
RNG& rng = theRNG();
Mat buf( QUERY_DES_COUNT, 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 < COUNT_FACTOR; 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 * COUNT_FACTOR + j, randCol ) = modified_u;
}
}
}
PERF_TEST(matching, single_match)
{
Mat query, train;
std::vector<DMatch> dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
bd->match( query, train, dm );
SANITY_CHECK_MATCHES( dm );
}
PERF_TEST(knn_matching, knn_match_distances_test)
{
Mat query, train, distances;
std::vector<std::vector<DMatch> > dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
{
bd->knnMatch( query, train, dm, QUERY_DES_COUNT );
for ( int i = 0; i < (int) dm.size(); i++ )
{
for ( int j = 0; j < (int) dm[i].size(); j++ )
distances.push_back( dm[i][j].distance );
}
}
SANITY_CHECK( distances );
}
PERF_TEST(radius_match, radius_match_distances_test)
{
Mat query, train, distances;
std::vector<std::vector<DMatch> > dm;
Ptr<BinaryDescriptorMatcher> bd = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
generateData( query, train );
TEST_CYCLE()
{
bd->radiusMatch( query, train, dm, RADIUS );
for ( int i = 0; i < (int) dm.size(); i++ )
{
for ( int j = 0; j < (int) dm[i].size(); j++ )
distances.push_back( dm[i][j].distance );
}
}
SANITY_CHECK( distances );
}
/*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*/
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
# if defined __clang__ || defined __APPLE__
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
# pragma GCC diagnostic ignored "-Wextra"
# endif
#endif
#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/line_descriptor.hpp"
#ifdef GTEST_CREATE_SHARED_LIBRARY
#error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined
#endif
#endif
......@@ -84,6 +84,7 @@ int main( int argc, char** argv )
/* get path to image */
std::stringstream image_path;
image_path << pathToImages << images[i];
std::cout << image_path.str().c_str() << std::endl;
/* load image */
Mat loadedImage = imread( image_path.str().c_str(), 1 );
......@@ -127,5 +128,15 @@ int main( int argc, char** argv )
/* compute matches */
std::vector<std::vector<DMatch> > matches;
bdm->radiusMatch( queries, matches, 30 );
std::cout << "size matches sample " << matches.size() << std::endl;
for ( int i = 0; i < matches.size(); i++ )
{
for ( int j = 0; j < matches[i].size(); j++ )
{
std::cout << "match: " << matches[i][j].queryIdx << " " << matches[i][j].trainIdx << " " << matches[i][j].distance << std::endl;
}
}
}
......@@ -119,6 +119,12 @@ void BinaryDescriptorMatcher::checkKDistances( UINT32 * numres, int k, std::vect
void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks )
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: query descriptors'matrix is empty" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but match function received " << masks.size()
......@@ -139,7 +145,6 @@ void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DM
/* execute query */
dataset->batchquery( results, numres, queryDescriptors, queryDescriptors.rows, queryDescriptors.cols );
/* compose matches */
for ( int counter = 0; counter < queryDescriptors.rows; counter++ )
{
......@@ -149,16 +154,14 @@ void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, std::vector<DM
/* get info about original image of each returned descriptor */
itup = indexesMap.upper_bound( results[counter] - 1 );
itup--;
/* data validity check */
if( !masks.empty() && ( masks[itup->second].rows != queryDescriptors.rows || masks[itup->second].cols != 1 ) )
{
std::stringstream ss;
ss << "Error: mask " << itup->second << " in knnMatch function " << "should have " << queryDescriptors.rows << " and "
<< "1 column. Program will be terminated";
throw std::runtime_error( ss.str() );
//throw std::runtime_error( ss.str() );
}
/* create a DMatch object if required by mask or if there is
no mask at all */
else if( masks.empty() || masks[itup->second].at<uchar>( counter ) != 0 )
......@@ -187,6 +190,12 @@ void BinaryDescriptorMatcher::match( const Mat& queryDescriptors, const Mat& tra
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows && mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
......@@ -243,6 +252,12 @@ void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, const Mat&
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows || mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
......@@ -318,6 +333,12 @@ void BinaryDescriptorMatcher::knnMatch( const Mat& queryDescriptors, std::vector
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrix cannot be void" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but knnMatch function received " << masks.size()
......@@ -402,6 +423,12 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Ma
{
/* check data validity */
if( queryDescriptors.rows == 0 || trainDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( !mask.empty() && ( mask.rows != queryDescriptors.rows && mask.cols != 1 ) )
{
std::cout << "Error: input mask should have " << queryDescriptors.rows << " rows and 1 column. " << "Program will be terminated" << std::endl;
......@@ -413,7 +440,8 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Ma
Mihasher* mh = new Mihasher( 256, 32 );
/* populate Mihasher */
Mat copy = queryDescriptors.clone();
//Mat copy = queryDescriptors.clone();
Mat copy = trainDescriptors.clone();
mh->populate( copy, copy.rows, copy.cols );
/* set K */
......@@ -434,15 +462,16 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Ma
checkKDistances( numres, trainDescriptors.rows, k_distances, i, 256 );
std::vector<DMatch> tempVector;
for ( int j = 0; j < index + trainDescriptors.rows; j++ )
for ( int j = index; j < index + trainDescriptors.rows; j++ )
{
if( numres[j] <= maxDistance )
// if( numres[j] <= maxDistance )
if( k_distances[j - index] <= maxDistance )
{
if( mask.empty() || mask.at<uchar>( i ) != 0 )
{
DMatch dm;
dm.queryIdx = i;
dm.trainIdx = results[j] - 1;
dm.trainIdx = (int) ( results[j] - 1 );
dm.imgIdx = 0;
dm.distance = (float) k_distances[j - index];
......@@ -466,13 +495,19 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, const Ma
delete numres;
}
/* for every input desciptor, find all the ones falling in a
certaing atching radius (from one image to a set) */
/* for every input descriptor, find all the ones falling in a
certain matching radius (from one image to a set) */
void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
const std::vector<Mat>& masks, bool compactResult )
{
/* check data validity */
if( queryDescriptors.rows == 0 )
{
std::cout << "Error: descriptors matrices cannot be void" << std::endl;
return;
}
if( masks.size() != 0 && (int) masks.size() != numImages )
{
std::cout << "Error: the number of images in dataset is " << numImages << " but radiusMatch function received " << masks.size()
......@@ -528,7 +563,7 @@ void BinaryDescriptorMatcher::radiusMatch( const Mat& queryDescriptors, std::vec
dm.queryIdx = counter;
dm.trainIdx = results[j] - 1;
dm.imgIdx = itup->second;
dm.distance = (float)k_distances[j - index];
dm.distance = (float) k_distances[j - index];
tempVector.push_back( dm );
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment