Commit 9a52f462 authored by biagio montesano's avatar biagio montesano

Created nested classes. Removed some useless headers.

parent e39065bf
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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*/
#ifndef __OPENCV_ARRAY32_HPP
#define __OPENCV_ARRAY32_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#include "types.hpp"
class Array32
{
private:
static double ARRAY_RESIZE_FACTOR;
static double ARRAY_RESIZE_ADD_FACTOR;
public:
/* set ARRAY_RESIZE_FACTOR */
static void setArrayResizeFactor( double arf );
/* constructor */
Array32();
/* destructor */
~Array32();
/* cleaning function used in destructor */
void cleanup();
/* push data */
void push( UINT32 data );
/* insert data at given index */
void insert( UINT32 index, UINT32 data );
/* return data */
UINT32* data();
/* return data size */
UINT32 size();
/* return capacity */
UINT32 capacity();
/* definition of operator = */
void operator=( const Array32& );
/* print data */
void print();
/* initializer */
void init( int size );
/* data */
UINT32 *arr;
};
#endif
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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*/
#ifndef __OPENCV_BUCKET_GROUP_HPP
#define __OPENCV_BUCKET_GROUP_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#include "types.hpp"
#include "array32.hpp"
#include "bitarray.hpp"
class BucketGroup
{
public:
/* constructor */
BucketGroup();
/* destructor */
~BucketGroup();
/* insert data into the bucket */
void insert( int subindex, UINT32 data );
/* perform a query to the bucket */
UINT32* query( int subindex, int *size );
/* data fields */
UINT32 empty;
Array32 *group;
};
#endif
/*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) 2011-2012, Lilian Zhang, all rights reserved.
Copyright (C) 2013, Manuele Tamburrano, Stefano Fabri, all rights reserved.
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:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* 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.
*/
#ifndef __OPENCV_LINE_STRUCTURE_HH_
#define __OPENCV_LINE_STRUCTURE_HH_
#include <vector>
/* struct to represent lines extracted from an octave */
struct OctaveLine
{
unsigned int octaveCount; //the octave which this line is detected
unsigned int lineIDInOctave; //the line ID in that octave image
unsigned int lineIDInScaleLineVec; //the line ID in Scale line vector
float lineLength; //the length of line in original image scale
};
// A 2D line (normal equation parameters).
struct SingleLine
{
//note: rho and theta are based on coordinate origin, i.e. the top-left corner of image
double rho; //unit: pixel length
double theta; //unit: rad
double linePointX; // = rho * cos(theta);
double linePointY; // = rho * sin(theta);
//for EndPoints, the coordinate origin is the top-left corner of image.
double startPointX;
double startPointY;
double endPointX;
double endPointY;
//direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
double direction;
//mean gradient magnitude
double gradientMagnitude;
//mean gray value of pixels in dark side of line
double darkSideGrayValue;
//mean gray value of pixels in light side of line
double lightSideGrayValue;
//the length of line
double lineLength;
//the width of line;
double width;
//number of pixels
int numOfPixels;
//the decriptor of line
std::vector<double> descriptor;
};
// Specifies a vector of lines.
typedef std::vector<SingleLine> Lines_list;
struct OctaveSingleLine
{
/*endPoints, the coordinate origin is the top-left corner of the original image.
*startPointX = sPointInOctaveX * (factor)^octaveCount; */
float startPointX;
float startPointY;
float endPointX;
float endPointY;
//endPoints, the coordinate origin is the top-left corner of the octave image.
float sPointInOctaveX;
float sPointInOctaveY;
float ePointInOctaveX;
float ePointInOctaveY;
//direction of a line, the angle between positive line direction (dark side is in the left) and positive X axis.
float direction;
//the summation of gradient magnitudes of pixels on lines
float salience;
//the length of line
float lineLength;
//number of pixels
unsigned int numOfPixels;
//the octave which this line is detected
unsigned int octaveCount;
//the decriptor of line
std::vector<float> descriptor;
};
// Specifies a vector of lines.
typedef std::vector<OctaveSingleLine> LinesVec;
// each element in ScaleLines is a vector of lines
// which corresponds the same line detected in different octave images.
typedef std::vector<LinesVec> ScaleLines;
#endif
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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*/
#ifndef __OPENCV_MIHASHER_HPP
#define __OPENCV_MIHASHER_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#include "types.hpp"
#include "bitops.hpp"
#include "sparse_hashtable.hpp"
#include "bitarray.hpp"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
class Mihasher
{
private:
/* Bits per code */
int B;
/* B/8 */
int B_over_8;
/* Bits per chunk (must be less than 64) */
int b;
/* Number of chunks */
int m;
/* Number of chunks with b bits (have 1 bit more than others) */
int mplus;
/* Maximum hamming search radius (we use B/2 by default) */
int D;
/* Maximum hamming search radius per substring */
int d;
/* Maximum results to return */
int K;
/* Number of codes */
UINT64 N;
/* Table of original full-length codes */
cv::Mat codes;
/* Counter for eliminating duplicate results (it is not thread safe) */
bitarray *counter;
/* Array of m hashtables */
SparseHashtable *H;
/* Volume of a b-bit Hamming ball with radius s (for s = 0 to d) */
UINT32 *xornum;
/* Used within generation of binary codes at a certain Hamming distance */
int power[100];
public:
/* constructor */
Mihasher();
/* desctructor */
~Mihasher();
/* constructor 2 */
Mihasher( int B, int m );
/* K setter */
void setK( int K );
/* populate tables */
void populate( cv::Mat & codes, UINT32 N, int dim1codes );
/* execute a batch query */
void batchquery( UINT32 * results, UINT32 *numres/*, qstat *stats*/, const cv::Mat & q, UINT32 numq, int dim1queries );
private:
/* execute a single query */
void query( UINT32 * results, UINT32* numres/*, qstat *stats*/, UINT8 *q, UINT64 * chunks, UINT32 * res );
};
#endif
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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*/
#ifndef __OPENCV_SPARSE_HASHTABLE_HPP
#define __OPENCV_SPARSE_HASHTABLE_HPP
#ifdef _WIN32
#pragma warning( disable : 4267 )
#endif
#include "types.hpp"
#include "bucket_group.hpp"
class SparseHashtable
{
private:
/* Maximum bits per key before folding the table */
static const int MAX_B;
/* Bins (each bin is an Array object for duplicates of the same key) */
BucketGroup *table;
public:
/* constructor */
SparseHashtable();
/* destructor */
~SparseHashtable();
/* initializer */
int init( int _b );
/* insert data */
void insert( UINT64 index, UINT32 data );
/* query data */
UINT32* query( UINT64 index, int* size );
/* Bits per index */
int b;
/* Number of bins */
UINT64 size;
};
#endif
......@@ -62,22 +62,22 @@ static const char* keys =
static void help()
{
std::cout << "\nThis example shows the functionalities of radius matching " << "Please, run this sample using a command in the form\n"
<< "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
<< "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
CommandLineParser parser( argc, argv, keys );
String pathToImages = parser.get<String>( 0 );
String pathToImages = parser.get < String > ( 0 );
/* create structures for hosting KeyLines and descriptors */
int num_elements = sizeof ( images ) / sizeof ( images[0] );
std::vector<Mat> descriptorsMat;
std::vector<std::vector<KeyLine> > linesMat;
std::vector < Mat > descriptorsMat;
std::vector < std::vector<KeyLine> > linesMat;
/*create a pointer to a BinaryDescriptor object */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
Ptr < BinaryDescriptor > bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines and descriptors */
for ( int i = 0; i < num_elements; i++ )
......@@ -97,7 +97,7 @@ int main( int argc, char** argv )
}
/* compute lines and descriptors */
std::vector<KeyLine> lines;
std::vector < KeyLine > lines;
Mat computedDescr;
bd->detect( loadedImage, lines );
bd->compute( loadedImage, lines, computedDescr );
......@@ -121,19 +121,19 @@ int main( int argc, char** argv )
std::cout << "It has been generated a matrix of " << queries.rows << " descriptors" << std::endl;
/* create a BinaryDescriptorMatcher object */
Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
Ptr < BinaryDescriptorMatcher > bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
/* populate matcher */
bdm->add( descriptorsMat );
/* compute matches */
std::vector<std::vector<DMatch> > 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 i = 0; i < (int) matches.size(); i++ )
{
for ( int j = 0; j < matches[i].size(); j++ )
for ( int j = 0; j < (int) matches[i].size(); j++ )
{
std::cout << "match: " << matches[i][j].queryIdx << " " << matches[i][j].trainIdx << " " << matches[i][j].distance << std::endl;
}
......
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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 "precomp.hpp"
/* no need for the static keyword in the definition */
double Array32::ARRAY_RESIZE_FACTOR = 1.1; // minimum is 1.0
double Array32::ARRAY_RESIZE_ADD_FACTOR = 4; // minimum is 1
/* set ARRAY_RESIZE_FACTOR */
void Array32::setArrayResizeFactor( double arf )
{
ARRAY_RESIZE_FACTOR = arf;
}
/* constructor */
Array32::Array32()
{
arr = NULL;
}
/* definition of operator =
Array32& Array32::operator = (const Array32 &rhs) */
void Array32::operator =( const Array32 &rhs )
{
if( &rhs != this )
this->arr = rhs.arr;
}
/* destructor */
Array32::~Array32()
{
cleanup();
}
/* cleaning function used in destructor */
void Array32::cleanup()
{
free( arr );
}
/* push data */
void Array32::push( UINT32 Data )
{
if( arr )
{
if( arr[0] == arr[1] )
{
arr[1] = (UINT32) std::max( ceil( arr[1] * ARRAY_RESIZE_FACTOR ), arr[1] + ARRAY_RESIZE_ADD_FACTOR );
UINT32* new_Data = static_cast<UINT32*>( realloc( arr, sizeof(UINT32) * ( 2 + arr[1] ) ) );
if( new_Data == NULL )
{
/* could not realloc, but orig still valid */
std::cout << "ALERT!!!! Not enough memory, operation aborted!" << std::endl;
exit( 0 );
}
else
{
arr = new_Data;
}
}
arr[2 + arr[0]] = Data;
arr[0]++;
}
else
{
arr = (UINT32*) malloc( (size_t) ( 2 + ARRAY_RESIZE_ADD_FACTOR ) * sizeof(UINT32) );
arr[0] = 1;
arr[1] = 1;
arr[2] = Data;
}
}
/* insert data at given index */
void Array32::insert( UINT32 index, UINT32 Data )
{
if( arr )
{
if( arr[0] == arr[1] )
{
arr[1] = (UINT32) ceil( arr[0] * 1.1 );
UINT32* new_data = static_cast<UINT32*>( realloc( arr, sizeof(UINT32) * ( 2 + arr[1] ) ) );
if( new_data == NULL )
{
// could not realloc, but orig still valid
std::cout << "ALERT!!!! Not enough memory, operation aborted!" << std::endl;
exit( 0 );
}
else
{
arr = new_data;
}
}
memmove( arr + ( 2 + index ) + 1, arr + ( 2 + index ), ( arr[0] - index ) * sizeof ( *arr ) );
arr[2 + index] = Data;
arr[0]++;
}
else
{
arr = (UINT32*) malloc( 3 * sizeof(UINT32) );
arr[0] = 1;
arr[1] = 1;
arr[2] = Data;
}
}
/* return data */
UINT32* Array32::data()
{
return arr ? arr + 2 : NULL;
}
/* return data size */
UINT32 Array32::size()
{
return arr ? arr[0] : 0;
}
/* return capacity */
UINT32 Array32::capacity()
{
return arr ? arr[1] : 0;
}
/* print data */
void Array32::print()
{
for ( int i = 0; i < (int) size(); i++ )
printf( "%d, ", arr[i + 2] );
printf( "\n" );
}
/* initializer */
void Array32::init( int Size )
{
if( arr == NULL )
{
arr = (UINT32*) malloc( ( 2 + Size ) * sizeof(UINT32) );
arr[0] = 0;
arr[1] = Size;
}
}
......@@ -43,6 +43,8 @@
#ifndef __OPENCV_BITOPTS_HPP
#define __OPENCV_BITOPTS_HPP
#include "precomp.hpp"
#ifdef _WIN32
# include <intrin.h>
# define popcnt __popcnt
......@@ -63,6 +65,10 @@ const int lookup[] =
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5,
6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };
namespace cv
{
namespace line_descriptor
{
/*matching function */
inline int match( UINT8*P, UINT8*Q, int codelb )
{
......@@ -163,5 +169,7 @@ inline UINT64 choose( int n, int r )
return nchooser;
}
}
}
#endif
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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 "precomp.hpp"
/* constructor */
BucketGroup::BucketGroup()
{
empty = 0;
group = NULL;
}
/* destructor */
BucketGroup::~BucketGroup()
{
if( group != NULL )
delete group;
}
/* insert data into the bucket */
void BucketGroup::insert( int subindex, UINT32 data )
{
if( group == NULL )
{
group = new Array32();
group->push( 0 );
}
UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1;
int end = popcnt( empty & lowerbits );
if( ! ( empty & ( (UINT32) 1 << subindex ) ) )
{
group->insert( end, group->arr[end + 2] );
empty |= (UINT32) 1 << subindex;
}
int totones = popcnt( empty );
group->insert( totones + 1 + group->arr[2 + end + 1], data );
for ( int i = end + 1; i < totones + 1; i++ )
group->arr[2 + i]++;
}
/* perform a query to the bucket */
UINT32* BucketGroup::query( int subindex, int *size )
{
if( empty & ( (UINT32) 1 << subindex ) )
{
UINT32 lowerbits = ( (UINT32) 1 << subindex ) - 1;
int end = popcnt( empty & lowerbits );
int totones = popcnt( empty );
*size = group->arr[2 + end + 1] - group->arr[2 + end];
return group->arr + 2 + totones + 1 + group->arr[2 + end];
}
else
{
*size = 0;
return NULL;
}
}
This diff is collapsed.
/*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, Mohammad Norouzi, Ali Punjani, David J. Fleet,
// 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 "precomp.hpp"
/* execute a batch query */
void Mihasher::batchquery( UINT32 * results, UINT32 *numres, const cv::Mat & queries, UINT32 numq, int dim1queries )
{
/* create and initialize a bitarray */
counter = new bitarray;
counter->init( N );
UINT32 *res = new UINT32[K * ( D + 1 )];
UINT64 *chunks = new UINT64[m];
UINT32 * presults = results;
UINT32 *pnumres = numres;
/* make a copy of input queries */
cv::Mat queries_clone = queries.clone();
/* set a pointer to first query (row) */
UINT8 *pq = queries_clone.ptr();
/* loop over number of descriptors */
for ( size_t i = 0; i < numq; i++ )
{
/* for every descriptor, query database */
query( presults, pnumres, pq, chunks, res );
/* move pointer to write next K indeces */
presults += K;
pnumres += B + 1;
/* move forward pointer to current row in descriptors matrix */
pq += dim1queries;
}
delete[] res;
delete[] chunks;
delete counter;
}
/* execute a single query */
void Mihasher::query( UINT32* results, UINT32* numres, UINT8 * Query, UINT64 *chunks, UINT32 *res )
{
/* if K == 0 that means we want everything to be processed.
So maxres = N in that case. Otherwise K limits the results processed */
UINT32 maxres = K ? K : (UINT32) N;
/* number of results so far obtained (up to a distance of s per chunk) */
UINT32 n = 0;
/* number of candidates tested with full codes (not counting duplicates) */
UINT32 nc = 0;
/* counting everything retrieved (duplicates are counted multiple times)
number of lookups (and xors) */
UINT32 nl = 0;
UINT32 nd = 0;
UINT32 *arr;
int size = 0;
UINT32 index;
int hammd;
counter->erase();
memset( numres, 0, ( B + 1 ) * sizeof ( *numres ) );
split( chunks, Query, m, mplus, b );
/* the growing search radius per substring */
int s;
/* current b: for the first mplus substrings it is b, for the rest it is (b-1) */
int curb = b;
for ( s = 0; s <= d && n < maxres; s++ )
{
for ( int k = 0; k < m; k++ )
{
if( k < mplus )
curb = b;
else
curb = b - 1;
UINT64 chunksk = chunks[k];
/* number of bit-strings with s number of 1s */
nl += xornum[s + 1] - xornum[s];
/* the bit-string with s number of 1s */
UINT64 bitstr = 0;
for ( int i = 0; i < s; i++ )
/* power[i] stores the location of the i'th 1 */
power[i] = i;
/* used for stopping criterion (location of (s+1)th 1) */
power[s] = curb + 1;
/* bit determines the 1 that should be moving to the left */
int bit = s - 1;
/* start from the left-most 1, and move it to the left until
it touches another one */
/* the loop for changing bitstr */
bool infiniteWhile = true;
while ( infiniteWhile )
{
if( bit != -1 )
{
bitstr ^= ( power[bit] == bit ) ? (UINT64) 1 << power[bit] : (UINT64) 3 << ( power[bit] - 1 );
power[bit]++;
bit--;
}
else
{ /* bit == -1 */
/* the binary code bitstr is available for processing */
arr = H[k].query( chunksk ^ bitstr, &size ); // lookup
if( size )
{ /* the corresponding bucket is not empty */
nd += size;
for ( int c = 0; c < size; c++ )
{
index = arr[c];
if( !counter->get( index ) )
{ /* if it is not a duplicate */
counter->set( index );
hammd = match( codes.ptr() + (UINT64) index * ( B_over_8 ), Query, B_over_8 );
nc++;
if( hammd <= D && numres[hammd] < maxres )
res[hammd * K + numres[hammd]] = index + 1;
numres[hammd]++;
}
}
}
/* end of processing */
while ( ++bit < s && power[bit] == power[bit + 1] - 1 )
{
bitstr ^= (UINT64) 1 << ( power[bit] - 1 );
power[bit] = bit;
}
if( bit == s )
break;
}
}
n = n + numres[s * m + k];
if( n >= maxres )
break;
}
}
n = 0;
for ( s = 0; s <= D && (int) n < K; s++ )
{
for ( int c = 0; c < (int) numres[s] && (int) n < K; c++ )
results[n++] = res[s * K + c];
}
}
/* constructor 2 */
Mihasher::Mihasher( int _B, int _m )
{
B = _B;
B_over_8 = B / 8;
m = _m;
b = (int) ceil( (double) B / m );
/* assuming that B/2 is large enough radius to include
all of the k nearest neighbors */
D = (int) ceil( B / 2.0 );
d = (int) ceil( (double) D / m );
/* mplus is the number of chunks with b bits
(m-mplus) is the number of chunks with (b-1) bits */
mplus = B - m * ( b - 1 );
xornum = new UINT32[d + 2];
xornum[0] = 0;
for ( int i = 0; i <= d; i++ )
xornum[i + 1] = xornum[i] + (UINT32) choose( b, i );
H = new SparseHashtable[m];
/* H[i].init might fail */
for ( int i = 0; i < mplus; i++ )
H[i].init( b );
for ( int i = mplus; i < m; i++ )
H[i].init( b - 1 );
}
/* K setter */
void Mihasher::setK( int _K )
{
K = _K;
}
/* desctructor */
Mihasher::~Mihasher()
{
delete[] xornum;
delete[] H;
}
/* populate tables */
void Mihasher::populate( cv::Mat & _codes, UINT32 _N, int dim1codes )
{
N = _N;
codes = _codes;
UINT64 * chunks = new UINT64[m];
UINT8 * pcodes = codes.ptr();
for ( UINT64 i = 0; i < N; i++, pcodes += dim1codes )
{
split( chunks, pcodes, m, mplus, b );
for ( int k = 0; k < m; k++ )
H[k].insert( chunks[k], (UINT32) i );
if( i % (int) ceil( N / 1000.0 ) == 0 )
fflush( stdout );
}
delete[] chunks;
}
......@@ -68,6 +68,10 @@
#include <sstream>
#include <vector>
#include "bitarray.hpp"
#include "bitops.hpp"
#include "types.hpp"
#include "opencv2/line_descriptor.hpp"
#endif
/*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 "precomp.hpp"
const int SparseHashtable::MAX_B = 37;
/* constructor */
SparseHashtable::SparseHashtable()
{
table = NULL;
size = 0;
b = 0;
}
/* initializer */
int SparseHashtable::init( int _b )
{
b = _b;
if( b < 5 || b > MAX_B || b > (int) ( sizeof(UINT64) * 8 ) )
return 1;
size = UINT64_1 << ( b - 5 ); // size = 2 ^ b
table = (BucketGroup*) calloc( size, sizeof(BucketGroup) );
return 0;
}
/* destructor */
SparseHashtable::~SparseHashtable()
{
free( table );
}
/* insert data */
void SparseHashtable::insert( UINT64 index, UINT32 data )
{
table[index >> 5].insert( (int) ( index % 32 ), data );
}
/* query data */
UINT32* SparseHashtable::query( UINT64 index, int *Size )
{
return table[index >> 5].query( (int) ( index % 32 ), Size );
}
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