added openfabmap code, contributed by Arren Glover. fixed several warnings in the new versions of retina filters
@ -0,0 +1,228 @@ |
||||
openFABMAP |
||||
======================================== |
||||
|
||||
.. highlight:: cpp |
||||
|
||||
The openFABMAP package has been integrated into OpenCV from the openFABMAP <http://code.google.com/p/openfabmap/> project. OpenFABMAP is an open and modifiable code-source which implements the Fast Appearance-based Mapping algorithm (FAB-MAP) developed by Mark Cummins and Paul Newman. The algorithms used in openFABMAP were developed using only the relevant FAB-MAP publications. |
||||
|
||||
FAB-MAP is an approach to appearance-based place recognition. FAB-MAP compares images of locations that have been visited and determines the probability of re-visiting a location, as well as providing a measure of the probability of being at a new, previously unvisited location. Camera images form the sole input to the system, from which visual bag-of-words models are formed through the extraction of appearance-based (e.g. SURF) features. |
||||
|
||||
openFABMAP requires training data (e.g. a collection of images from a similar but not identical environment) to construct a visual vocabulary for the visual bag-of-words model, along with a Chow-Liu tree representation of feature likelihood and for use in the Sampled new place method (see below). |
||||
|
||||
FabMap |
||||
-------------------- |
||||
|
||||
.. ocv:class:: FabMap |
||||
|
||||
The main FabMap class performs the comparison between visual bags-of-words extracted from one or more images. The FabMap class is instantiated as one of the four inherited FabMap classes (FabMap1, FabMapLUT, FabMapFBO, FabMap2). Each inherited class performs the comparison differently based on algorithm iterations as published (see each class below for specifics). A Chow-Liu tree, detector model parameters and some option flags are common to all Fabmap variants and are supplied on class creation. Training data (visual bag-of-words) is supplied to the class if using the SAMPLED new place method. Test data (visual bag-of-words) is supplied as images to which query bag-of-words are compared against. The common flags are listed below: :: |
||||
|
||||
enum { |
||||
MEAN_FIELD, |
||||
SAMPLED, |
||||
NAIVE_BAYES, |
||||
CHOW_LIU, |
||||
MOTION_MODEL |
||||
}; |
||||
|
||||
#. MEAN_FIELD: Use the Mean Field approximation to determine the new place likelihood (cannot be used for FabMap2). |
||||
#. SAMPLED: Use the Sampled approximation to determine the new place likelihood. Requires training data (see below). |
||||
#. NAIVE_BAYES: Assume a naive Bayes approximation to feature distribution (i.e. all features are independent). Note that a Chow-Liu tree is still required but only the absolute word probabilities are used, feature co-occurrance information is discarded. |
||||
#. CHOW_LIU: Use the full Chow-Liu tree to approximate feature distribution. |
||||
#. MOTION_MODEL: Update the location distribution using the previous distribution as a (weak) prior. Used for matching in sequences (i.e. successive video frames). |
||||
|
||||
Training Data |
||||
++++++++++++++++++++ |
||||
|
||||
Training data is required to use the SAMPLED new place method. The SAMPLED method was shown to have improved performance over the alternative MEAN_FIELD method. Training data can be added singularly or as a batch. |
||||
|
||||
.. ocv:function:: virtual void addTraining(const Mat& queryImgDescriptor) |
||||
|
||||
:param queryImgDescriptor: bag-of-words image descriptors stored as rows in a Mat |
||||
|
||||
.. ocv:function:: virtual void addTraining(const vector<Mat>& queryImgDescriptors) |
||||
|
||||
:param queryImgDescriptors: a vector containing multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: const vector<Mat>& getTrainingImgDescriptors() const |
||||
|
||||
Returns a vector containing multiple bag-of-words image descriptors |
||||
|
||||
Test Data |
||||
++++++++++++++++++++ |
||||
|
||||
Test Data is the database of images represented using bag-of-words models. When a compare function is called, each query point is compared to the test data. |
||||
|
||||
.. ocv:function:: virtual void add(const Mat& queryImgDescriptor) |
||||
|
||||
:param queryImgDescriptor: bag-of-words image descriptors stored as rows in a Mat |
||||
|
||||
.. ocv:function:: virtual void add(const vector<Mat>& queryImgDescriptors) |
||||
|
||||
:param queryImgDescriptors: a vector containing multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: const vector<Mat>& getTestImgDescriptors() const |
||||
|
||||
Returns a vector containing multiple bag-of-words image descriptors |
||||
|
||||
Image Comparison |
||||
++++++++++++++++++++ |
||||
|
||||
Image matching is performed calling the compare function. Query bag-of-words image descriptors are provided and compared to test data added to the FabMap class. Alternatively test data can be provided with the call to compare to which the comparison is performed. Results are written to the 'matches' argument. |
||||
|
||||
.. ocv:function:: void compare(const Mat& queryImgDescriptor, vector<IMatch>& matches, bool addQuery = false, const Mat& mask = Mat()) |
||||
|
||||
:param queryImgDescriptor: bag-of-words image descriptors stored as rows in a Mat |
||||
|
||||
:param matches: a vector of image match probabilities |
||||
|
||||
:param addQuery: if true the queryImg Descriptor is added to the test data after the comparison is performed. |
||||
|
||||
:param mask: *not implemented* |
||||
|
||||
.. ocv:function:: void compare(const Mat& queryImgDescriptor, const Mat& testImgDescriptors, vector<IMatch>& matches, const Mat& mask = Mat()) |
||||
|
||||
:param testImgDescriptors: bag-of-words image descriptors stored as rows in a Mat |
||||
|
||||
.. ocv:function:: void compare(const Mat& queryImgDescriptor, const vector<Mat>& testImgDescriptors, vector<IMatch>& matches, const Mat& mask = Mat()) |
||||
|
||||
:param testImgDescriptors: a vector of multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: void compare(const vector<Mat>& queryImgDescriptors, vector<IMatch>& matches, bool addQuery = false, const Mat& mask = Mat()) |
||||
|
||||
:param queryImgDescriptors: a vector of multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: void compare(const vector<Mat>& queryImgDescriptors, const vector<Mat>& testImgDescriptors, vector<IMatch>& matches, const Mat& mask = Mat()) |
||||
|
||||
|
||||
|
||||
FabMap classes |
||||
++++++++++++++++++++ |
||||
|
||||
.. ocv:class:: FabMap1 : public FabMap |
||||
|
||||
The original FAB-MAP algorithm without any computational improvements as published in [IJRR2008]_ |
||||
|
||||
.. ocv:function:: FabMap1::FabMap1(const Mat& clTree, double PzGe, double PzGNe, int flags, int numSamples = 0) |
||||
|
||||
:param clTree: a Chow-Liu tree class |
||||
|
||||
:param PzGe: the dector model recall. The probability of the feature detector extracting a feature from an object given it is in the scene. This is used to account for detector noise. |
||||
|
||||
:param PzGNe: the dector model precision. The probability of the feature detector falsing extracting a feature representing an object that is not in the scene. |
||||
|
||||
:param numSamples: the number of samples to use for the SAMPLED new place calculation |
||||
|
||||
.. ocv:class:: FabMapLUT : public FabMap |
||||
|
||||
The original FAB-MAP algorithm implemented as a look-up table for speed enhancements [ICRA2011]_ |
||||
|
||||
.. ocv:function:: FabMapLUT::FabMapLUT(const Mat& clTree, double PzGe, double PzGNe, int flags, int numSamples = 0, int precision = 6) |
||||
|
||||
:param precision: the precision with which to store the pre-computed likelihoods |
||||
|
||||
.. ocv:class:: FabMapFBO : public FabMap |
||||
|
||||
The accelerated FAB-MAP using a 'fast bail-out' approach as in [TRO2010]_ |
||||
|
||||
.. ocv:function:: FabMapFBO::FabMapFBO(const Mat& clTree, double PzGe, double PzGNe, int flags, int numSamples = 0, double rejectionThreshold = 1e-8, double PsGd = 1e-8, int bisectionStart = 512, int bisectionIts = 9) |
||||
|
||||
:param rejectionThreshold: images are not considered a match when the likelihood falls below the Bennett bound by the amount given by the rejectionThreshold. The threshold provides a speed/accuracy trade-off. A lower bound will be more accurate |
||||
|
||||
:param PsGd: used to calculate the Bennett bound. Provides a speed/accuracy trade-off. A lower bound will be more accurate |
||||
|
||||
:param bisectionStart: Used to estimate the bound using the bisection method. Must be larger than the largest expected difference between maximum and minimum image likelihoods |
||||
|
||||
:param bisectionIts: The number of iterations for which to perform the bisection method |
||||
|
||||
|
||||
.. ocv:class:: FabMap2 : public FabMap |
||||
|
||||
The inverted index FAB-MAP as in [IJRR2010]_. This version of FAB-MAP is the fastest without any loss of accuracy. |
||||
|
||||
.. ocv:function:: FabMap2::FabMap2(const Mat& clTree, double PzGe, double PzGNe, int flags) |
||||
|
||||
.. [IJRR2008] M. Cummins and P. Newman, "FAB-MAP: Probabilistic Localization and Mapping in the Space of Appearance," The International Journal of Robotics Research, vol. 27(6), pp. 647-665, 2008 |
||||
|
||||
.. [TRO2010] M. Cummins and P. Newman, "Accelerating FAB-MAP with concentration inequalities," IEEE Transactions on Robotics, vol. 26(6), pp. 1042-1050, 2010 |
||||
|
||||
.. [IJRR2010] M. Cummins and P. Newman, "Appearance-only SLAM at large scale with FAB-MAP 2.0," The International Journal of Robotics Research, vol. 30(9), pp. 1100-1123, 2010 |
||||
|
||||
.. [ICRA2011] A. Glover, et al., "OpenFABMAP: An Open Source Toolbox for Appearance-based Loop Closure Detection," in IEEE International Conference on Robotics and Automation, St Paul, Minnesota, 2011 |
||||
|
||||
ImageMatch |
||||
-------------------- |
||||
|
||||
.. ocv:struct:: IMatch |
||||
|
||||
FAB-MAP comparison results are stored in a vector of IMatch structs. Each IMatch structure provides the index of the provided query bag-of-words, the index of the test bag-of-words, the raw log-likelihood of the match (independent of other comparisons), and the match probability (normalised over other comparison likelihoods). |
||||
|
||||
:: |
||||
|
||||
struct IMatch { |
||||
|
||||
IMatch() : |
||||
queryIdx(-1), imgIdx(-1), likelihood(-DBL_MAX), match(-DBL_MAX) { |
||||
} |
||||
IMatch(int _queryIdx, int _imgIdx, double _likelihood, double _match) : |
||||
queryIdx(_queryIdx), imgIdx(_imgIdx), likelihood(_likelihood), match( |
||||
_match) { |
||||
} |
||||
|
||||
int queryIdx; //query index |
||||
int imgIdx; //test index |
||||
|
||||
double likelihood; //raw loglikelihood |
||||
double match; //normalised probability |
||||
|
||||
bool operator<(const IMatch& m) const { |
||||
return match < m.match; |
||||
} |
||||
|
||||
}; |
||||
|
||||
Chow-Liu Tree |
||||
-------------------- |
||||
|
||||
.. ocv:class:: ChowLiuTree |
||||
|
||||
The Chow-Liu tree is a probabilistic model of the environment in terms of feature occurance and co-occurance. The Chow-Liu tree is a form of Bayesian network. FAB-MAP uses the model when calculating bag-of-words similarity by taking into account feature saliency. Training data is provided to the ChowLiuTree class in the form of bag-of-words image descriptors. The make function produces a cv::Mat that encodes the tree structure. |
||||
|
||||
.. ocv:function:: ChowLiuTree::ChowLiuTree() |
||||
|
||||
.. ocv:function:: void add(const Mat& imgDescriptor) |
||||
|
||||
:param imgDescriptor: bag-of-words image descriptors stored as rows in a Mat |
||||
|
||||
.. ocv:function:: void add(const vector<Mat>& imgDescriptors) |
||||
|
||||
:param imgDescriptors: a vector containing multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: const vector<Mat>& getImgDescriptors() const |
||||
|
||||
Returns a vector containing multiple bag-of-words image descriptors |
||||
|
||||
.. ocv:function:: Mat make(double infoThreshold = 0.0) |
||||
|
||||
:param infoThreshold: a threshold can be set to reduce the amount of memory used when making the Chow-Liu tree, which can occur with large vocabulary sizes. This function can fail if the threshold is set too high. If memory is an issue the value must be set by trial and error (~0.0005) |
||||
|
||||
|
||||
BOWMSCTrainer |
||||
-------------------- |
||||
|
||||
.. ocv:class:: BOWMSCTrainer : public BOWTrainer |
||||
|
||||
BOWMSCTrainer is a custom clustering algorithm used to produce the feature vocabulary required to create bag-of-words representations. The algorithm is an implementation of [AVC2007]_. Arguments against using K-means for the FAB-MAP algorithm are discussed in [IJRR2010]_. The BOWMSCTrainer inherits from the cv::BOWTrainer class, overwriting the cluster function. |
||||
|
||||
.. ocv:function:: BOWMSCTrainer::BOWMSCTrainer(double clusterSize = 0.4) |
||||
|
||||
:param clusterSize: the specificity of the vocabulary produced. A smaller cluster size will instigate a larger vocabulary. |
||||
|
||||
.. ocv:function:: virtual Mat cluster() const |
||||
|
||||
Cluster using features added to the class |
||||
|
||||
.. ocv:function:: virtual Mat cluster(const Mat& descriptors) const |
||||
|
||||
:param descriptors: feature descriptors provided as rows of the Mat. |
||||
|
||||
.. [AVC2007] Alexandra Teynor and Hans Burkhardt, "Fast Codebook Generation by Sequential Data Analysis for Object Classification", in Advances in Visual Computing, pp. 610-620, 2007 |
@ -0,0 +1,405 @@ |
||||
/*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.
|
||||
//
|
||||
// This file originates from the openFABMAP project:
|
||||
// [http://code.google.com/p/openfabmap/]
|
||||
//
|
||||
// For published work which uses all or part of OpenFABMAP, please cite:
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
|
||||
//
|
||||
// Original Algorithm by Mark Cummins and Paul Newman:
|
||||
// [http://ijr.sagepub.com/content/27/6/647.short]
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
|
||||
// [http://ijr.sagepub.com/content/30/9/1100.abstract]
|
||||
//
|
||||
// License Agreement
|
||||
//
|
||||
// Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
|
||||
// Will Maddern [w.maddern@qut.edu.au], all rights reserved.
|
||||
//
|
||||
//
|
||||
// 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_OPENFABMAP_H_ |
||||
#define __OPENCV_OPENFABMAP_H_ |
||||
|
||||
#include "opencv2/core/core.hpp" |
||||
#include "opencv2/features2d/features2d.hpp" |
||||
|
||||
#include <vector> |
||||
#include <list> |
||||
#include <map> |
||||
#include <set> |
||||
#include <valarray> |
||||
|
||||
namespace cv { |
||||
|
||||
namespace of2 { |
||||
|
||||
using std::list; |
||||
using std::map; |
||||
using std::multiset; |
||||
|
||||
/*
|
||||
Return data format of a FABMAP compare call |
||||
*/ |
||||
struct CV_EXPORTS IMatch { |
||||
|
||||
IMatch() : |
||||
queryIdx(-1), imgIdx(-1), likelihood(-DBL_MAX), match(-DBL_MAX) { |
||||
} |
||||
IMatch(int _queryIdx, int _imgIdx, double _likelihood, double _match) : |
||||
queryIdx(_queryIdx), imgIdx(_imgIdx), likelihood(_likelihood), match( |
||||
_match) { |
||||
} |
||||
|
||||
int queryIdx; //query index
|
||||
int imgIdx; //test index
|
||||
|
||||
double likelihood; //raw loglikelihood
|
||||
double match; //normalised probability
|
||||
|
||||
bool operator<(const IMatch& m) const { |
||||
return match < m.match; |
||||
} |
||||
|
||||
}; |
||||
|
||||
/*
|
||||
Base FabMap class. Each FabMap method inherits from this class. |
||||
*/ |
||||
class CV_EXPORTS FabMap { |
||||
public: |
||||
|
||||
//FabMap options
|
||||
enum { |
||||
MEAN_FIELD = 1, |
||||
SAMPLED = 2, |
||||
NAIVE_BAYES = 4, |
||||
CHOW_LIU = 8, |
||||
MOTION_MODEL = 16 |
||||
}; |
||||
|
||||
FabMap(const Mat& clTree, double PzGe, double PzGNe, int flags, |
||||
int numSamples = 0); |
||||
virtual ~FabMap(); |
||||
|
||||
//methods to add training data for sampling method
|
||||
virtual void addTraining(const Mat& queryImgDescriptor); |
||||
virtual void addTraining(const vector<Mat>& queryImgDescriptors); |
||||
|
||||
//methods to add to the test data
|
||||
virtual void add(const Mat& queryImgDescriptor); |
||||
virtual void add(const vector<Mat>& queryImgDescriptors); |
||||
|
||||
//accessors
|
||||
const vector<Mat>& getTrainingImgDescriptors() const; |
||||
const vector<Mat>& getTestImgDescriptors() const; |
||||
|
||||
//Main FabMap image comparison
|
||||
void compare(const Mat& queryImgDescriptor, |
||||
vector<IMatch>& matches, bool addQuery = false, |
||||
const Mat& mask = Mat()); |
||||
void compare(const Mat& queryImgDescriptor, |
||||
const Mat& testImgDescriptors, vector<IMatch>& matches, |
||||
const Mat& mask = Mat()); |
||||
void compare(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, |
||||
vector<IMatch>& matches, const Mat& mask = Mat()); |
||||
void compare(const vector<Mat>& queryImgDescriptors, vector< |
||||
IMatch>& matches, bool addQuery = false, const Mat& mask = |
||||
Mat()); |
||||
void compare(const vector<Mat>& queryImgDescriptors, |
||||
const vector<Mat>& testImgDescriptors, |
||||
vector<IMatch>& matches, const Mat& mask = Mat()); |
||||
|
||||
protected: |
||||
|
||||
void compareImgDescriptor(const Mat& queryImgDescriptor, |
||||
int queryIndex, const vector<Mat>& testImgDescriptors, |
||||
vector<IMatch>& matches); |
||||
|
||||
void addImgDescriptor(const Mat& queryImgDescriptor); |
||||
|
||||
//the getLikelihoods method is overwritten for each different FabMap
|
||||
//method.
|
||||
virtual void getLikelihoods(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, |
||||
vector<IMatch>& matches); |
||||
virtual double getNewPlaceLikelihood(const Mat& queryImgDescriptor); |
||||
|
||||
//turn likelihoods into probabilities (also add in motion model if used)
|
||||
void normaliseDistribution(vector<IMatch>& matches); |
||||
|
||||
//Chow-Liu Tree
|
||||
int pq(int q); |
||||
double Pzq(int q, bool zq); |
||||
double PzqGzpq(int q, bool zq, bool zpq); |
||||
|
||||
//FAB-MAP Core
|
||||
double PzqGeq(bool zq, bool eq); |
||||
double PeqGL(int q, bool Lzq, bool eq); |
||||
double PzqGL(int q, bool zq, bool zpq, bool Lzq); |
||||
double PzqGzpqL(int q, bool zq, bool zpq, bool Lzq); |
||||
double (FabMap::*PzGL)(int q, bool zq, bool zpq, bool Lzq); |
||||
|
||||
//data
|
||||
Mat clTree; |
||||
vector<Mat> trainingImgDescriptors; |
||||
vector<Mat> testImgDescriptors; |
||||
vector<IMatch> priorMatches; |
||||
|
||||
//parameters
|
||||
double PzGe; |
||||
double PzGNe; |
||||
double Pnew; |
||||
|
||||
double mBias; |
||||
double sFactor; |
||||
|
||||
int flags; |
||||
int numSamples; |
||||
|
||||
}; |
||||
|
||||
/*
|
||||
The original FAB-MAP algorithm, developed based on: |
||||
http://ijr.sagepub.com/content/27/6/647.short
|
||||
*/ |
||||
class CV_EXPORTS FabMap1: public FabMap { |
||||
public: |
||||
FabMap1(const Mat& clTree, double PzGe, double PzGNe, int flags, |
||||
int numSamples = 0); |
||||
virtual ~FabMap1(); |
||||
protected: |
||||
|
||||
//FabMap1 implementation of likelihood comparison
|
||||
void getLikelihoods(const Mat& queryImgDescriptor, const vector< |
||||
Mat>& testImgDescriptors, vector<IMatch>& matches); |
||||
}; |
||||
|
||||
/*
|
||||
A computationally faster version of the original FAB-MAP algorithm. A look- |
||||
up-table is used to precompute many of the reoccuring calculations |
||||
*/ |
||||
class CV_EXPORTS FabMapLUT: public FabMap { |
||||
public: |
||||
FabMapLUT(const Mat& clTree, double PzGe, double PzGNe, |
||||
int flags, int numSamples = 0, int precision = 6); |
||||
virtual ~FabMapLUT(); |
||||
protected: |
||||
|
||||
//FabMap look-up-table implementation of the likelihood comparison
|
||||
void getLikelihoods(const Mat& queryImgDescriptor, const vector< |
||||
Mat>& testImgDescriptors, vector<IMatch>& matches); |
||||
|
||||
//procomputed data
|
||||
int (*table)[8]; |
||||
|
||||
//data precision
|
||||
int precision; |
||||
}; |
||||
|
||||
/*
|
||||
The Accelerated FAB-MAP algorithm, developed based on: |
||||
http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942
|
||||
*/ |
||||
class CV_EXPORTS FabMapFBO: public FabMap { |
||||
public: |
||||
FabMapFBO(const Mat& clTree, double PzGe, double PzGNe, int flags, |
||||
int numSamples = 0, double rejectionThreshold = 1e-8, double PsGd = |
||||
1e-8, int bisectionStart = 512, int bisectionIts = 9); |
||||
virtual ~FabMapFBO(); |
||||
|
||||
protected: |
||||
|
||||
//FabMap Fast Bail-out implementation of the likelihood comparison
|
||||
void getLikelihoods(const Mat& queryImgDescriptor, const vector< |
||||
Mat>& testImgDescriptors, vector<IMatch>& matches); |
||||
|
||||
//stucture used to determine word comparison order
|
||||
struct WordStats { |
||||
WordStats() : |
||||
q(0), info(0), V(0), M(0) { |
||||
} |
||||
|
||||
WordStats(int _q, double _info) : |
||||
q(_q), info(_info), V(0), M(0) { |
||||
} |
||||
|
||||
int q; |
||||
double info; |
||||
mutable double V; |
||||
mutable double M; |
||||
|
||||
bool operator<(const WordStats& w) const { |
||||
return info < w.info; |
||||
} |
||||
|
||||
}; |
||||
|
||||
//private fast bail-out necessary functions
|
||||
void setWordStatistics(const Mat& queryImgDescriptor, multiset<WordStats>& wordData); |
||||
double limitbisection(double v, double m); |
||||
double bennettInequality(double v, double m, double delta); |
||||
static bool compInfo(const WordStats& first, const WordStats& second); |
||||
|
||||
//parameters
|
||||
double PsGd; |
||||
double rejectionThreshold; |
||||
int bisectionStart; |
||||
int bisectionIts; |
||||
}; |
||||
|
||||
/*
|
||||
The FAB-MAP2.0 algorithm, developed based on: |
||||
http://ijr.sagepub.com/content/30/9/1100.abstract
|
||||
*/ |
||||
class CV_EXPORTS FabMap2: public FabMap { |
||||
public: |
||||
|
||||
FabMap2(const Mat& clTree, double PzGe, double PzGNe, int flags); |
||||
virtual ~FabMap2(); |
||||
|
||||
//FabMap2 builds the inverted index and requires an additional training/test
|
||||
//add function
|
||||
void addTraining(const Mat& queryImgDescriptors) { |
||||
FabMap::addTraining(queryImgDescriptors); |
||||
} |
||||
void addTraining(const vector<Mat>& queryImgDescriptors); |
||||
|
||||
void add(const Mat& queryImgDescriptors) { |
||||
FabMap::add(queryImgDescriptors); |
||||
} |
||||
void add(const vector<Mat>& queryImgDescriptors); |
||||
|
||||
protected: |
||||
|
||||
//FabMap2 implementation of the likelihood comparison
|
||||
void getLikelihoods(const Mat& queryImgDescriptor, const vector< |
||||
Mat>& testImgDescriptors, vector<IMatch>& matches); |
||||
double getNewPlaceLikelihood(const Mat& queryImgDescriptor); |
||||
|
||||
//the likelihood function using the inverted index
|
||||
void getIndexLikelihoods(const Mat& queryImgDescriptor, vector< |
||||
double>& defaults, map<int, vector<int> >& invertedMap, |
||||
vector<IMatch>& matches); |
||||
void addToIndex(const Mat& queryImgDescriptor, |
||||
vector<double>& defaults, |
||||
map<int, vector<int> >& invertedMap); |
||||
|
||||
//data
|
||||
vector<double> d1, d2, d3, d4; |
||||
vector<vector<int> > children; |
||||
|
||||
// TODO: inverted map a vector?
|
||||
|
||||
vector<double> trainingDefaults; |
||||
map<int, vector<int> > trainingInvertedMap; |
||||
|
||||
vector<double> testDefaults; |
||||
map<int, vector<int> > testInvertedMap; |
||||
|
||||
}; |
||||
/*
|
||||
A Chow-Liu tree is required by FAB-MAP. The Chow-Liu tree provides an |
||||
estimate of the full distribution of visual words using a minimum spanning |
||||
tree. The tree is generated through training data. |
||||
*/ |
||||
class CV_EXPORTS ChowLiuTree { |
||||
public: |
||||
ChowLiuTree(); |
||||
virtual ~ChowLiuTree(); |
||||
|
||||
//add data to the chow-liu tree before calling make
|
||||
void add(const Mat& imgDescriptor); |
||||
void add(const vector<Mat>& imgDescriptors); |
||||
|
||||
const vector<Mat>& getImgDescriptors() const; |
||||
|
||||
Mat make(double infoThreshold = 0.0); |
||||
|
||||
private: |
||||
vector<Mat> imgDescriptors; |
||||
Mat mergedImgDescriptors; |
||||
|
||||
typedef struct info { |
||||
float score; |
||||
short word1; |
||||
short word2; |
||||
} info; |
||||
|
||||
//probabilities extracted from mergedImgDescriptors
|
||||
double P(int a, bool za); |
||||
double JP(int a, bool za, int b, bool zb); //a & b
|
||||
double CP(int a, bool za, int b, bool zb); // a | b
|
||||
|
||||
//calculating mutual information of all edges
|
||||
void createBaseEdges(list<info>& edges, double infoThreshold); |
||||
double calcMutInfo(int word1, int word2); |
||||
static bool sortInfoScores(const info& first, const info& second); |
||||
|
||||
//selecting minimum spanning egdges with maximum information
|
||||
bool reduceEdgesToMinSpan(list<info>& edges); |
||||
|
||||
//building the tree sctructure
|
||||
Mat buildTree(int root_word, list<info> &edges); |
||||
void recAddToTree(Mat &cltree, int q, int pq, |
||||
list<info> &remaining_edges); |
||||
vector<int> extractChildren(list<info> &remaining_edges, int q); |
||||
|
||||
}; |
||||
|
||||
/*
|
||||
A custom vocabulary training method based on: |
||||
http://www.springerlink.com/content/d1h6j8x552532003/
|
||||
*/ |
||||
class CV_EXPORTS BOWMSCTrainer: public BOWTrainer { |
||||
public: |
||||
BOWMSCTrainer(double clusterSize = 0.4); |
||||
virtual ~BOWMSCTrainer(); |
||||
|
||||
// Returns trained vocabulary (i.e. cluster centers).
|
||||
virtual Mat cluster() const; |
||||
virtual Mat cluster(const Mat& descriptors) const; |
||||
|
||||
protected: |
||||
|
||||
double clusterSize; |
||||
|
||||
}; |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
#endif /* OPENFABMAP_H_ */ |
@ -0,0 +1,139 @@ |
||||
/*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.
|
||||
//
|
||||
// This file originates from the openFABMAP project:
|
||||
// [http://code.google.com/p/openfabmap/]
|
||||
//
|
||||
// For published work which uses all or part of OpenFABMAP, please cite:
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
|
||||
//
|
||||
// Original Algorithm by Mark Cummins and Paul Newman:
|
||||
// [http://ijr.sagepub.com/content/27/6/647.short]
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
|
||||
// [http://ijr.sagepub.com/content/30/9/1100.abstract]
|
||||
//
|
||||
// License Agreement
|
||||
//
|
||||
// Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
|
||||
// Will Maddern [w.maddern@qut.edu.au], all rights reserved.
|
||||
//
|
||||
//
|
||||
// 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" |
||||
#include "opencv2/contrib/openfabmap.hpp" |
||||
|
||||
namespace cv { |
||||
|
||||
namespace of2 { |
||||
|
||||
BOWMSCTrainer::BOWMSCTrainer(double _clusterSize) : |
||||
clusterSize(_clusterSize) { |
||||
} |
||||
|
||||
BOWMSCTrainer::~BOWMSCTrainer() { |
||||
} |
||||
|
||||
Mat BOWMSCTrainer::cluster() const { |
||||
CV_Assert(!descriptors.empty()); |
||||
int descCount = 0; |
||||
for(size_t i = 0; i < descriptors.size(); i++) |
||||
descCount += descriptors[i].rows; |
||||
|
||||
Mat mergedDescriptors(descCount, descriptors[0].cols, |
||||
descriptors[0].type()); |
||||
for(size_t i = 0, start = 0; i < descriptors.size(); i++) |
||||
{ |
||||
Mat submut = mergedDescriptors.rowRange((int)start, |
||||
(int)(start + descriptors[i].rows)); |
||||
descriptors[i].copyTo(submut); |
||||
start += descriptors[i].rows; |
||||
} |
||||
return cluster(mergedDescriptors); |
||||
} |
||||
|
||||
Mat BOWMSCTrainer::cluster(const Mat& descriptors) const { |
||||
|
||||
CV_Assert(!descriptors.empty()); |
||||
|
||||
// TODO: sort the descriptors before clustering.
|
||||
|
||||
|
||||
Mat icovar = Mat::eye(descriptors.cols,descriptors.cols,descriptors.type()); |
||||
|
||||
vector<Mat> initialCentres; |
||||
initialCentres.push_back(descriptors.row(0)); |
||||
for (int i = 1; i < descriptors.rows; i++) { |
||||
double minDist = DBL_MAX; |
||||
for (size_t j = 0; j < initialCentres.size(); j++) { |
||||
minDist = std::min(minDist, |
||||
cv::Mahalanobis(descriptors.row(i),initialCentres[j], |
||||
icovar)); |
||||
} |
||||
if (minDist > clusterSize) |
||||
initialCentres.push_back(descriptors.row(i)); |
||||
} |
||||
|
||||
std::vector<std::list<cv::Mat> > clusters; |
||||
clusters.resize(initialCentres.size()); |
||||
for (int i = 0; i < descriptors.rows; i++) { |
||||
int index = 0; double dist = 0, minDist = DBL_MAX; |
||||
for (size_t j = 0; j < initialCentres.size(); j++) { |
||||
dist = cv::Mahalanobis(descriptors.row(i),initialCentres[j],icovar); |
||||
if (dist < minDist) { |
||||
minDist = dist; |
||||
index = (int)j; |
||||
} |
||||
} |
||||
clusters[index].push_back(descriptors.row(i)); |
||||
} |
||||
|
||||
// TODO: throw away small clusters.
|
||||
|
||||
Mat vocabulary; |
||||
Mat centre = Mat::zeros(1,descriptors.cols,descriptors.type()); |
||||
for (size_t i = 0; i < clusters.size(); i++) { |
||||
centre.setTo(0); |
||||
for (std::list<cv::Mat>::iterator Ci = clusters[i].begin(); Ci != clusters[i].end(); Ci++) { |
||||
centre += *Ci; |
||||
} |
||||
centre /= (double)clusters[i].size(); |
||||
vocabulary.push_back(centre); |
||||
} |
||||
|
||||
return vocabulary; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,290 @@ |
||||
/*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.
|
||||
//
|
||||
// This file originates from the openFABMAP project:
|
||||
// [http://code.google.com/p/openfabmap/]
|
||||
//
|
||||
// For published work which uses all or part of OpenFABMAP, please cite:
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
|
||||
//
|
||||
// Original Algorithm by Mark Cummins and Paul Newman:
|
||||
// [http://ijr.sagepub.com/content/27/6/647.short]
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
|
||||
// [http://ijr.sagepub.com/content/30/9/1100.abstract]
|
||||
//
|
||||
// License Agreement
|
||||
//
|
||||
// Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
|
||||
// Will Maddern [w.maddern@qut.edu.au], all rights reserved.
|
||||
//
|
||||
//
|
||||
// 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" |
||||
#include "opencv2/contrib/openfabmap.hpp" |
||||
|
||||
namespace cv { |
||||
|
||||
namespace of2 { |
||||
|
||||
ChowLiuTree::ChowLiuTree() { |
||||
} |
||||
|
||||
ChowLiuTree::~ChowLiuTree() { |
||||
} |
||||
|
||||
void ChowLiuTree::add(const Mat& imgDescriptor) { |
||||
CV_Assert(!imgDescriptor.empty()); |
||||
if (!imgDescriptors.empty()) { |
||||
CV_Assert(imgDescriptors[0].cols == imgDescriptor.cols); |
||||
CV_Assert(imgDescriptors[0].type() == imgDescriptor.type()); |
||||
} |
||||
|
||||
imgDescriptors.push_back(imgDescriptor); |
||||
|
||||
} |
||||
|
||||
void ChowLiuTree::add(const vector<Mat>& _imgDescriptors) { |
||||
for (size_t i = 0; i < _imgDescriptors.size(); i++) { |
||||
add(_imgDescriptors[i]); |
||||
} |
||||
} |
||||
|
||||
const std::vector<cv::Mat>& ChowLiuTree::getImgDescriptors() const { |
||||
return imgDescriptors; |
||||
} |
||||
|
||||
Mat ChowLiuTree::make(double infoThreshold) { |
||||
CV_Assert(!imgDescriptors.empty()); |
||||
|
||||
unsigned int descCount = 0; |
||||
for (size_t i = 0; i < imgDescriptors.size(); i++) |
||||
descCount += imgDescriptors[i].rows; |
||||
|
||||
mergedImgDescriptors = cv::Mat(descCount, imgDescriptors[0].cols, |
||||
imgDescriptors[0].type()); |
||||
for (size_t i = 0, start = 0; i < imgDescriptors.size(); i++) |
||||
{ |
||||
Mat submut = mergedImgDescriptors.rowRange((int)start, |
||||
(int)(start + imgDescriptors[i].rows)); |
||||
imgDescriptors[i].copyTo(submut); |
||||
start += imgDescriptors[i].rows; |
||||
} |
||||
|
||||
std::list<info> edges; |
||||
createBaseEdges(edges, infoThreshold); |
||||
|
||||
// TODO: if it cv_asserts here they really won't know why.
|
||||
|
||||
CV_Assert(reduceEdgesToMinSpan(edges)); |
||||
|
||||
return buildTree(edges.front().word1, edges); |
||||
} |
||||
|
||||
double ChowLiuTree::P(int a, bool za) { |
||||
|
||||
if(za) { |
||||
return (0.98 * cv::countNonZero(mergedImgDescriptors.col(a)) / |
||||
mergedImgDescriptors.rows) + 0.01; |
||||
} else { |
||||
return 1 - ((0.98 * cv::countNonZero(mergedImgDescriptors.col(a)) / |
||||
mergedImgDescriptors.rows) + 0.01); |
||||
} |
||||
|
||||
} |
||||
double ChowLiuTree::JP(int a, bool za, int b, bool zb) { |
||||
|
||||
double count = 0; |
||||
for(int i = 0; i < mergedImgDescriptors.rows; i++) { |
||||
if((mergedImgDescriptors.at<float>(i,a) > 0) == za && |
||||
(mergedImgDescriptors.at<float>(i,b) > 0) == zb) { |
||||
count++; |
||||
} |
||||
} |
||||
return count / mergedImgDescriptors.rows; |
||||
|
||||
} |
||||
double ChowLiuTree::CP(int a, bool za, int b, bool zb){ |
||||
|
||||
int count = 0, total = 0; |
||||
for(int i = 0; i < mergedImgDescriptors.rows; i++) { |
||||
if((mergedImgDescriptors.at<float>(i,b) > 0) == zb) { |
||||
total++; |
||||
if((mergedImgDescriptors.at<float>(i,a) > 0) == za) { |
||||
count++; |
||||
} |
||||
} |
||||
} |
||||
if(total) { |
||||
return (double)(0.98 * count)/total + 0.01; |
||||
} else { |
||||
return (za) ? 0.01 : 0.99; |
||||
} |
||||
} |
||||
|
||||
cv::Mat ChowLiuTree::buildTree(int root_word, std::list<info> &edges) { |
||||
|
||||
int q = root_word; |
||||
cv::Mat cltree(4, (int)edges.size()+1, CV_64F); |
||||
|
||||
cltree.at<double>(0, q) = q; |
||||
cltree.at<double>(1, q) = P(q, true); |
||||
cltree.at<double>(2, q) = P(q, true); |
||||
cltree.at<double>(3, q) = P(q, true); |
||||
//setting P(zq|zpq) to P(zq) gives the root node of the chow-liu
|
||||
//independence from a parent node.
|
||||
|
||||
//find all children and do the same
|
||||
vector<int> nextqs = extractChildren(edges, q); |
||||
|
||||
int pq = q; |
||||
vector<int>::iterator nextq; |
||||
for(nextq = nextqs.begin(); nextq != nextqs.end(); nextq++) { |
||||
recAddToTree(cltree, *nextq, pq, edges); |
||||
} |
||||
|
||||
return cltree; |
||||
|
||||
|
||||
} |
||||
|
||||
void ChowLiuTree::recAddToTree(cv::Mat &cltree, int q, int pq, |
||||
std::list<info>& remaining_edges) { |
||||
|
||||
cltree.at<double>(0, q) = pq; |
||||
cltree.at<double>(1, q) = P(q, true); |
||||
cltree.at<double>(2, q) = CP(q, true, pq, true); |
||||
cltree.at<double>(3, q) = CP(q, true, pq, false); |
||||
|
||||
//find all children and do the same
|
||||
vector<int> nextqs = extractChildren(remaining_edges, q); |
||||
|
||||
pq = q; |
||||
vector<int>::iterator nextq; |
||||
for(nextq = nextqs.begin(); nextq != nextqs.end(); nextq++) { |
||||
recAddToTree(cltree, *nextq, pq, remaining_edges); |
||||
} |
||||
} |
||||
|
||||
vector<int> ChowLiuTree::extractChildren(std::list<info> &remaining_edges, int q) { |
||||
|
||||
std::vector<int> children; |
||||
std::list<info>::iterator edge = remaining_edges.begin(); |
||||
|
||||
while(edge != remaining_edges.end()) { |
||||
if(edge->word1 == q) { |
||||
children.push_back(edge->word2); |
||||
edge = remaining_edges.erase(edge); |
||||
continue; |
||||
} |
||||
if(edge->word2 == q) { |
||||
children.push_back(edge->word1); |
||||
edge = remaining_edges.erase(edge); |
||||
continue; |
||||
} |
||||
edge++; |
||||
} |
||||
|
||||
return children; |
||||
} |
||||
|
||||
bool ChowLiuTree::sortInfoScores(const info& first, const info& second) { |
||||
return first.score > second.score; |
||||
} |
||||
|
||||
double ChowLiuTree::calcMutInfo(int word1, int word2) { |
||||
double accumulation = 0; |
||||
|
||||
double P00 = JP(word1, false, word2, false); |
||||
if(P00) accumulation += P00 * log(P00 / (P(word1, false)*P(word2, false))); |
||||
|
||||
double P01 = JP(word1, false, word2, true); |
||||
if(P01) accumulation += P01 * log(P01 / (P(word1, false)*P(word2, true))); |
||||
|
||||
double P10 = JP(word1, true, word2, false); |
||||
if(P10) accumulation += P10 * log(P10 / (P(word1, true)*P(word2, false))); |
||||
|
||||
double P11 = JP(word1, true, word2, true); |
||||
if(P11) accumulation += P11 * log(P11 / (P(word1, true)*P(word2, true))); |
||||
|
||||
return accumulation; |
||||
} |
||||
|
||||
void ChowLiuTree::createBaseEdges(std::list<info>& edges, double infoThreshold) { |
||||
|
||||
int nWords = imgDescriptors[0].cols; |
||||
info mutInfo; |
||||
|
||||
for(int word1 = 0; word1 < nWords; word1++) { |
||||
for(int word2 = word1 + 1; word2 < nWords; word2++) { |
||||
mutInfo.word1 = (short)word1; |
||||
mutInfo.word2 = (short)word2; |
||||
mutInfo.score = (float)calcMutInfo(word1, word2); |
||||
if(mutInfo.score >= infoThreshold) |
||||
edges.push_back(mutInfo); |
||||
} |
||||
} |
||||
edges.sort(sortInfoScores); |
||||
} |
||||
|
||||
bool ChowLiuTree::reduceEdgesToMinSpan(std::list<info>& edges) { |
||||
|
||||
std::map<int, int> groups; |
||||
std::map<int, int>::iterator groupIt; |
||||
for(int i = 0; i < imgDescriptors[0].cols; i++) groups[i] = i; |
||||
int group1, group2; |
||||
|
||||
std::list<info>::iterator edge = edges.begin(); |
||||
while(edge != edges.end()) { |
||||
if(groups[edge->word1] != groups[edge->word2]) { |
||||
group1 = groups[edge->word1]; |
||||
group2 = groups[edge->word2]; |
||||
for(groupIt = groups.begin(); groupIt != groups.end(); groupIt++) |
||||
if(groupIt->second == group2) groupIt->second = group1; |
||||
edge++; |
||||
} else { |
||||
edge = edges.erase(edge); |
||||
} |
||||
} |
||||
|
||||
if(edges.size() != (unsigned int)imgDescriptors[0].cols - 1) { |
||||
return false; |
||||
} else { |
||||
return true; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,779 @@ |
||||
/*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.
|
||||
//
|
||||
// This file originates from the openFABMAP project:
|
||||
// [http://code.google.com/p/openfabmap/]
|
||||
//
|
||||
// For published work which uses all or part of OpenFABMAP, please cite:
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
|
||||
//
|
||||
// Original Algorithm by Mark Cummins and Paul Newman:
|
||||
// [http://ijr.sagepub.com/content/27/6/647.short]
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
|
||||
// [http://ijr.sagepub.com/content/30/9/1100.abstract]
|
||||
//
|
||||
// License Agreement
|
||||
//
|
||||
// Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
|
||||
// Will Maddern [w.maddern@qut.edu.au], all rights reserved.
|
||||
//
|
||||
//
|
||||
// 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" |
||||
#include "opencv2/contrib/openfabmap.hpp" |
||||
|
||||
|
||||
/*
|
||||
Calculate the sum of two log likelihoods |
||||
*/ |
||||
namespace cv { |
||||
|
||||
namespace of2 { |
||||
|
||||
static double logsumexp(double a, double b) { |
||||
return a > b ? log(1 + exp(b - a)) + a : log(1 + exp(a - b)) + b; |
||||
} |
||||
|
||||
FabMap::FabMap(const Mat& _clTree, double _PzGe, |
||||
double _PzGNe, int _flags, int _numSamples) : |
||||
clTree(_clTree), PzGe(_PzGe), PzGNe(_PzGNe), flags( |
||||
_flags), numSamples(_numSamples) { |
||||
|
||||
CV_Assert(flags & MEAN_FIELD || flags & SAMPLED); |
||||
CV_Assert(flags & NAIVE_BAYES || flags & CHOW_LIU); |
||||
if (flags & NAIVE_BAYES) { |
||||
PzGL = &FabMap::PzqGL; |
||||
} else { |
||||
PzGL = &FabMap::PzqGzpqL; |
||||
} |
||||
|
||||
//check for a valid Chow-Liu tree
|
||||
CV_Assert(clTree.type() == CV_64FC1); |
||||
cv::checkRange(clTree.row(0), false, NULL, 0, clTree.cols); |
||||
cv::checkRange(clTree.row(1), false, NULL, DBL_MIN, 1); |
||||
cv::checkRange(clTree.row(2), false, NULL, DBL_MIN, 1); |
||||
cv::checkRange(clTree.row(3), false, NULL, DBL_MIN, 1); |
||||
|
||||
// TODO: Add default values for member variables
|
||||
Pnew = 0.9; |
||||
sFactor = 0.99; |
||||
mBias = 0.5; |
||||
} |
||||
|
||||
FabMap::~FabMap() { |
||||
} |
||||
|
||||
const std::vector<cv::Mat>& FabMap::getTrainingImgDescriptors() const { |
||||
return trainingImgDescriptors; |
||||
} |
||||
|
||||
const std::vector<cv::Mat>& FabMap::getTestImgDescriptors() const { |
||||
return testImgDescriptors; |
||||
} |
||||
|
||||
void FabMap::addTraining(const Mat& queryImgDescriptor) { |
||||
CV_Assert(!queryImgDescriptor.empty()); |
||||
vector<Mat> queryImgDescriptors; |
||||
for (int i = 0; i < queryImgDescriptor.rows; i++) { |
||||
queryImgDescriptors.push_back(queryImgDescriptor.row(i)); |
||||
} |
||||
addTraining(queryImgDescriptors); |
||||
} |
||||
|
||||
void FabMap::addTraining(const vector<Mat>& queryImgDescriptors) { |
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
trainingImgDescriptors.push_back(queryImgDescriptors[i]); |
||||
} |
||||
} |
||||
|
||||
void FabMap::add(const cv::Mat& queryImgDescriptor) { |
||||
CV_Assert(!queryImgDescriptor.empty()); |
||||
vector<Mat> queryImgDescriptors; |
||||
for (int i = 0; i < queryImgDescriptor.rows; i++) { |
||||
queryImgDescriptors.push_back(queryImgDescriptor.row(i)); |
||||
} |
||||
add(queryImgDescriptors); |
||||
} |
||||
|
||||
void FabMap::add(const std::vector<cv::Mat>& queryImgDescriptors) { |
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
testImgDescriptors.push_back(queryImgDescriptors[i]); |
||||
} |
||||
} |
||||
|
||||
void FabMap::compare(const Mat& queryImgDescriptor, |
||||
vector<IMatch>& matches, bool addQuery, |
||||
const Mat& mask) { |
||||
CV_Assert(!queryImgDescriptor.empty()); |
||||
vector<Mat> queryImgDescriptors; |
||||
for (int i = 0; i < queryImgDescriptor.rows; i++) { |
||||
queryImgDescriptors.push_back(queryImgDescriptor.row(i)); |
||||
} |
||||
compare(queryImgDescriptors,matches,addQuery,mask); |
||||
} |
||||
|
||||
void FabMap::compare(const Mat& queryImgDescriptor, |
||||
const Mat& testImgDescriptor, vector<IMatch>& matches, |
||||
const Mat& mask) { |
||||
CV_Assert(!queryImgDescriptor.empty()); |
||||
vector<Mat> queryImgDescriptors; |
||||
for (int i = 0; i < queryImgDescriptor.rows; i++) { |
||||
queryImgDescriptors.push_back(queryImgDescriptor.row(i)); |
||||
} |
||||
|
||||
CV_Assert(!testImgDescriptor.empty()); |
||||
vector<Mat> _testImgDescriptors; |
||||
for (int i = 0; i < testImgDescriptor.rows; i++) { |
||||
_testImgDescriptors.push_back(testImgDescriptor.row(i)); |
||||
} |
||||
compare(queryImgDescriptors,_testImgDescriptors,matches,mask); |
||||
|
||||
} |
||||
|
||||
void FabMap::compare(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& _testImgDescriptors, |
||||
vector<IMatch>& matches, const Mat& mask) { |
||||
CV_Assert(!queryImgDescriptor.empty()); |
||||
vector<Mat> queryImgDescriptors; |
||||
for (int i = 0; i < queryImgDescriptor.rows; i++) { |
||||
queryImgDescriptors.push_back(queryImgDescriptor.row(i)); |
||||
} |
||||
compare(queryImgDescriptors,_testImgDescriptors,matches,mask); |
||||
} |
||||
|
||||
void FabMap::compare(const vector<Mat>& queryImgDescriptors, |
||||
vector<IMatch>& matches, bool addQuery, const Mat& /*mask*/) { |
||||
|
||||
// TODO: add first query if empty (is this necessary)
|
||||
|
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
|
||||
// TODO: add mask
|
||||
|
||||
compareImgDescriptor(queryImgDescriptors[i], |
||||
(int)i, testImgDescriptors, matches); |
||||
if (addQuery) |
||||
add(queryImgDescriptors[i]); |
||||
} |
||||
} |
||||
|
||||
void FabMap::compare(const vector<Mat>& queryImgDescriptors, |
||||
const vector<Mat>& _testImgDescriptors, |
||||
vector<IMatch>& matches, const Mat& /*mask*/) { |
||||
if (_testImgDescriptors[0].data != this->testImgDescriptors[0].data) { |
||||
CV_Assert(!(flags & MOTION_MODEL)); |
||||
for (size_t i = 0; i < _testImgDescriptors.size(); i++) { |
||||
CV_Assert(!_testImgDescriptors[i].empty()); |
||||
CV_Assert(_testImgDescriptors[i].rows == 1); |
||||
CV_Assert(_testImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(_testImgDescriptors[i].type() == CV_32F); |
||||
} |
||||
} |
||||
|
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
|
||||
// TODO: add mask
|
||||
|
||||
compareImgDescriptor(queryImgDescriptors[i], |
||||
(int)i, _testImgDescriptors, matches); |
||||
} |
||||
} |
||||
|
||||
void FabMap::compareImgDescriptor(const Mat& queryImgDescriptor, |
||||
int queryIndex, const vector<Mat>& _testImgDescriptors, |
||||
vector<IMatch>& matches) { |
||||
|
||||
vector<IMatch> queryMatches; |
||||
queryMatches.push_back(IMatch(queryIndex,-1, |
||||
getNewPlaceLikelihood(queryImgDescriptor),0)); |
||||
getLikelihoods(queryImgDescriptor,_testImgDescriptors,queryMatches); |
||||
normaliseDistribution(queryMatches); |
||||
for (size_t j = 1; j < queryMatches.size(); j++) { |
||||
queryMatches[j].queryIdx = queryIndex; |
||||
} |
||||
matches.insert(matches.end(), queryMatches.begin(), queryMatches.end()); |
||||
} |
||||
|
||||
void FabMap::getLikelihoods(const Mat& /*queryImgDescriptor*/, |
||||
const vector<Mat>& /*testImgDescriptors*/, vector<IMatch>& /*matches*/) { |
||||
|
||||
} |
||||
|
||||
double FabMap::getNewPlaceLikelihood(const Mat& queryImgDescriptor) { |
||||
if (flags & MEAN_FIELD) { |
||||
double logP = 0; |
||||
bool zq, zpq; |
||||
if(flags & NAIVE_BAYES) { |
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
zq = queryImgDescriptor.at<float>(0,q) > 0; |
||||
|
||||
logP += log(Pzq(q, false) * PzqGeq(zq, false) + |
||||
Pzq(q, true) * PzqGeq(zq, true)); |
||||
} |
||||
} else { |
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
zq = queryImgDescriptor.at<float>(0,q) > 0; |
||||
zpq = queryImgDescriptor.at<float>(0,pq(q)) > 0; |
||||
|
||||
double alpha, beta, p; |
||||
alpha = Pzq(q, zq) * PzqGeq(!zq, false) * PzqGzpq(q, !zq, zpq); |
||||
beta = Pzq(q, !zq) * PzqGeq(zq, false) * PzqGzpq(q, zq, zpq); |
||||
p = Pzq(q, false) * beta / (alpha + beta); |
||||
|
||||
alpha = Pzq(q, zq) * PzqGeq(!zq, true) * PzqGzpq(q, !zq, zpq); |
||||
beta = Pzq(q, !zq) * PzqGeq(zq, true) * PzqGzpq(q, zq, zpq); |
||||
p += Pzq(q, true) * beta / (alpha + beta); |
||||
|
||||
logP += log(p); |
||||
} |
||||
} |
||||
return logP; |
||||
} |
||||
|
||||
if (flags & SAMPLED) { |
||||
CV_Assert(!trainingImgDescriptors.empty()); |
||||
CV_Assert(numSamples > 0); |
||||
|
||||
vector<Mat> sampledImgDescriptors; |
||||
|
||||
// TODO: this method can result in the same sample being added
|
||||
// multiple times. Is this desired?
|
||||
|
||||
for (int i = 0; i < numSamples; i++) { |
||||
int index = rand() % trainingImgDescriptors.size(); |
||||
sampledImgDescriptors.push_back(trainingImgDescriptors[index]); |
||||
} |
||||
|
||||
vector<IMatch> matches; |
||||
getLikelihoods(queryImgDescriptor,sampledImgDescriptors,matches); |
||||
|
||||
double averageLogLikelihood = -DBL_MAX + matches.front().likelihood + 1; |
||||
for (int i = 0; i < numSamples; i++) { |
||||
averageLogLikelihood = |
||||
logsumexp(matches[i].likelihood, averageLogLikelihood); |
||||
} |
||||
|
||||
return averageLogLikelihood - log((double)numSamples); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
void FabMap::normaliseDistribution(vector<IMatch>& matches) { |
||||
CV_Assert(!matches.empty()); |
||||
|
||||
if (flags & MOTION_MODEL) { |
||||
|
||||
matches[0].match = matches[0].likelihood + log(Pnew); |
||||
|
||||
if (priorMatches.size() > 2) { |
||||
matches[1].match = matches[1].likelihood; |
||||
matches[1].match += log( |
||||
(2 * (1-mBias) * priorMatches[1].match + |
||||
priorMatches[1].match + |
||||
2 * mBias * priorMatches[2].match) / 3); |
||||
for (size_t i = 2; i < priorMatches.size()-1; i++) { |
||||
matches[i].match = matches[i].likelihood; |
||||
matches[i].match += log( |
||||
(2 * (1-mBias) * priorMatches[i-1].match + |
||||
priorMatches[i].match + |
||||
2 * mBias * priorMatches[i+1].match)/3); |
||||
} |
||||
matches[priorMatches.size()-1].match = |
||||
matches[priorMatches.size()-1].likelihood; |
||||
matches[priorMatches.size()-1].match += log( |
||||
(2 * (1-mBias) * priorMatches[priorMatches.size()-2].match + |
||||
priorMatches[priorMatches.size()-1].match + |
||||
2 * mBias * priorMatches[priorMatches.size()-1].match)/3); |
||||
|
||||
for(size_t i = priorMatches.size(); i < matches.size(); i++) { |
||||
matches[i].match = matches[i].likelihood; |
||||
} |
||||
} else { |
||||
for(size_t i = 1; i < matches.size(); i++) { |
||||
matches[i].match = matches[i].likelihood; |
||||
} |
||||
} |
||||
|
||||
double logsum = -DBL_MAX + matches.front().match + 1; |
||||
|
||||
//calculate the normalising constant
|
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
logsum = logsumexp(logsum, matches[i].match); |
||||
} |
||||
|
||||
//normalise
|
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
matches[i].match = exp(matches[i].match - logsum); |
||||
} |
||||
|
||||
//smooth final probabilities
|
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
matches[i].match = sFactor*matches[i].match + |
||||
(1 - sFactor)/matches.size(); |
||||
} |
||||
|
||||
//update our location priors
|
||||
priorMatches = matches; |
||||
|
||||
} else { |
||||
|
||||
double logsum = -DBL_MAX + matches.front().likelihood + 1; |
||||
|
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
logsum = logsumexp(logsum, matches[i].likelihood); |
||||
} |
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
matches[i].match = exp(matches[i].likelihood - logsum); |
||||
} |
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
matches[i].match = sFactor*matches[i].match + |
||||
(1 - sFactor)/matches.size(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
int FabMap::pq(int q) { |
||||
return (int)clTree.at<double>(0,q); |
||||
} |
||||
|
||||
double FabMap::Pzq(int q, bool zq) { |
||||
return (zq) ? clTree.at<double>(1,q) : 1 - clTree.at<double>(1,q); |
||||
} |
||||
|
||||
double FabMap::PzqGzpq(int q, bool zq, bool zpq) { |
||||
if (zpq) { |
||||
return (zq) ? clTree.at<double>(2,q) : 1 - clTree.at<double>(2,q); |
||||
} else { |
||||
return (zq) ? clTree.at<double>(3,q) : 1 - clTree.at<double>(3,q); |
||||
} |
||||
} |
||||
|
||||
double FabMap::PzqGeq(bool zq, bool eq) { |
||||
if (eq) { |
||||
return (zq) ? PzGe : 1 - PzGe; |
||||
} else { |
||||
return (zq) ? PzGNe : 1 - PzGNe; |
||||
} |
||||
} |
||||
|
||||
double FabMap::PeqGL(int q, bool Lzq, bool eq) { |
||||
double alpha, beta; |
||||
alpha = PzqGeq(Lzq, true) * Pzq(q, true); |
||||
beta = PzqGeq(Lzq, false) * Pzq(q, false); |
||||
|
||||
if (eq) { |
||||
return alpha / (alpha + beta); |
||||
} else { |
||||
return 1 - (alpha / (alpha + beta)); |
||||
} |
||||
} |
||||
|
||||
double FabMap::PzqGL(int q, bool zq, bool /*zpq*/, bool Lzq) { |
||||
return PeqGL(q, Lzq, false) * PzqGeq(zq, false) + |
||||
PeqGL(q, Lzq, true) * PzqGeq(zq, true); |
||||
} |
||||
|
||||
|
||||
double FabMap::PzqGzpqL(int q, bool zq, bool zpq, bool Lzq) { |
||||
double p; |
||||
double alpha, beta; |
||||
|
||||
alpha = Pzq(q, zq) * PzqGeq(!zq, false) * PzqGzpq(q, !zq, zpq); |
||||
beta = Pzq(q, !zq) * PzqGeq( zq, false) * PzqGzpq(q, zq, zpq); |
||||
p = PeqGL(q, Lzq, false) * beta / (alpha + beta); |
||||
|
||||
alpha = Pzq(q, zq) * PzqGeq(!zq, true) * PzqGzpq(q, !zq, zpq); |
||||
beta = Pzq(q, !zq) * PzqGeq( zq, true) * PzqGzpq(q, zq, zpq); |
||||
p += PeqGL(q, Lzq, true) * beta / (alpha + beta); |
||||
|
||||
return p; |
||||
} |
||||
|
||||
|
||||
FabMap1::FabMap1(const Mat& _clTree, double _PzGe, double _PzGNe, int _flags, |
||||
int _numSamples) : FabMap(_clTree, _PzGe, _PzGNe, _flags, |
||||
_numSamples) { |
||||
} |
||||
|
||||
FabMap1::~FabMap1() { |
||||
} |
||||
|
||||
void FabMap1::getLikelihoods(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, vector<IMatch>& matches) { |
||||
|
||||
for (size_t i = 0; i < testImgDescriptors.size(); i++) { |
||||
bool zq, zpq, Lzq; |
||||
double logP = 0; |
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
|
||||
zq = queryImgDescriptor.at<float>(0,q) > 0; |
||||
zpq = queryImgDescriptor.at<float>(0,pq(q)) > 0; |
||||
Lzq = testImgDescriptors[i].at<float>(0,q) > 0; |
||||
|
||||
logP += log((this->*PzGL)(q, zq, zpq, Lzq)); |
||||
|
||||
} |
||||
matches.push_back(IMatch(0,(int)i,logP,0)); |
||||
} |
||||
} |
||||
|
||||
FabMapLUT::FabMapLUT(const Mat& _clTree, double _PzGe, double _PzGNe, |
||||
int _flags, int _numSamples, int _precision) : |
||||
FabMap(_clTree, _PzGe, _PzGNe, _flags, _numSamples), precision(_precision) { |
||||
|
||||
int nWords = clTree.cols; |
||||
double precFactor = (double)pow(10.0, precision); |
||||
|
||||
table = new int[nWords][8]; |
||||
|
||||
for (int q = 0; q < nWords; q++) { |
||||
for (unsigned char i = 0; i < 8; i++) { |
||||
|
||||
bool Lzq = (bool) ((i >> 2) & 0x01); |
||||
bool zq = (bool) ((i >> 1) & 0x01); |
||||
bool zpq = (bool) (i & 1); |
||||
|
||||
table[q][i] = -(int)(log((this->*PzGL)(q, zq, zpq, Lzq)) |
||||
* precFactor); |
||||
} |
||||
} |
||||
} |
||||
|
||||
FabMapLUT::~FabMapLUT() { |
||||
delete[] table; |
||||
} |
||||
|
||||
void FabMapLUT::getLikelihoods(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, vector<IMatch>& matches) { |
||||
|
||||
double precFactor = (double)pow(10.0, -precision); |
||||
|
||||
for (size_t i = 0; i < testImgDescriptors.size(); i++) { |
||||
unsigned long long int logP = 0; |
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
logP += table[q][(queryImgDescriptor.at<float>(0,pq(q)) > 0) + |
||||
((queryImgDescriptor.at<float>(0, q) > 0) << 1) + |
||||
((testImgDescriptors[i].at<float>(0,q) > 0) << 2)]; |
||||
} |
||||
matches.push_back(IMatch(0,(int)i,-precFactor*(double)logP,0)); |
||||
} |
||||
} |
||||
|
||||
FabMapFBO::FabMapFBO(const Mat& _clTree, double _PzGe, double _PzGNe, |
||||
int _flags, int _numSamples, double _rejectionThreshold, |
||||
double _PsGd, int _bisectionStart, int _bisectionIts) : |
||||
FabMap(_clTree, _PzGe, _PzGNe, _flags, _numSamples), PsGd(_PsGd), |
||||
rejectionThreshold(_rejectionThreshold), bisectionStart(_bisectionStart), |
||||
bisectionIts(_bisectionIts) { |
||||
} |
||||
|
||||
|
||||
FabMapFBO::~FabMapFBO() { |
||||
} |
||||
|
||||
void FabMapFBO::getLikelihoods(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, vector<IMatch>& matches) { |
||||
|
||||
std::multiset<WordStats> wordData; |
||||
setWordStatistics(queryImgDescriptor, wordData); |
||||
|
||||
vector<int> matchIndices; |
||||
vector<IMatch> queryMatches; |
||||
|
||||
for (size_t i = 0; i < testImgDescriptors.size(); i++) { |
||||
queryMatches.push_back(IMatch(0,(int)i,0,0)); |
||||
matchIndices.push_back((int)i); |
||||
} |
||||
|
||||
double currBest = -DBL_MAX; |
||||
double bailedOut = DBL_MAX; |
||||
|
||||
for (std::multiset<WordStats>::iterator wordIter = wordData.begin(); |
||||
wordIter != wordData.end(); wordIter++) { |
||||
bool zq = queryImgDescriptor.at<float>(0,wordIter->q) > 0; |
||||
bool zpq = queryImgDescriptor.at<float>(0,pq(wordIter->q)) > 0; |
||||
|
||||
currBest = -DBL_MAX; |
||||
|
||||
for (size_t i = 0; i < matchIndices.size(); i++) { |
||||
bool Lzq = |
||||
testImgDescriptors[matchIndices[i]].at<float>(0,wordIter->q) > 0; |
||||
queryMatches[matchIndices[i]].likelihood += |
||||
log((this->*PzGL)(wordIter->q,zq,zpq,Lzq)); |
||||
currBest = |
||||
std::max(queryMatches[matchIndices[i]].likelihood, currBest); |
||||
} |
||||
|
||||
if (matchIndices.size() == 1) |
||||
continue; |
||||
|
||||
double delta = std::max(limitbisection(wordIter->V, wordIter->M), |
||||
-log(rejectionThreshold)); |
||||
|
||||
vector<int>::iterator matchIter = matchIndices.begin(); |
||||
while (matchIter != matchIndices.end()) { |
||||
if (currBest - queryMatches[*matchIter].likelihood > delta) { |
||||
queryMatches[*matchIter].likelihood = bailedOut; |
||||
matchIter = matchIndices.erase(matchIter); |
||||
} else { |
||||
matchIter++; |
||||
} |
||||
} |
||||
} |
||||
|
||||
for (size_t i = 0; i < queryMatches.size(); i++) { |
||||
if (queryMatches[i].likelihood == bailedOut) { |
||||
queryMatches[i].likelihood = currBest + log(rejectionThreshold); |
||||
} |
||||
} |
||||
matches.insert(matches.end(), queryMatches.begin(), queryMatches.end()); |
||||
|
||||
} |
||||
|
||||
void FabMapFBO::setWordStatistics(const Mat& queryImgDescriptor, |
||||
std::multiset<WordStats>& wordData) { |
||||
//words are sorted according to information = -ln(P(zq|zpq))
|
||||
//in non-log format this is lowest probability first
|
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
wordData.insert(WordStats(q,PzqGzpq(q, |
||||
queryImgDescriptor.at<float>(0,q) > 0, |
||||
queryImgDescriptor.at<float>(0,pq(q)) > 0))); |
||||
} |
||||
|
||||
double d = 0, V = 0, M = 0; |
||||
bool zq, zpq; |
||||
|
||||
for (std::multiset<WordStats>::reverse_iterator wordIter = |
||||
wordData.rbegin(); |
||||
wordIter != wordData.rend(); wordIter++) { |
||||
|
||||
zq = queryImgDescriptor.at<float>(0,wordIter->q) > 0; |
||||
zpq = queryImgDescriptor.at<float>(0,pq(wordIter->q)) > 0; |
||||
|
||||
d = log((this->*PzGL)(wordIter->q, zq, zpq, true)) - |
||||
log((this->*PzGL)(wordIter->q, zq, zpq, false)); |
||||
|
||||
V += pow(d, 2.0) * 2 * |
||||
(Pzq(wordIter->q, true) - pow(Pzq(wordIter->q, true), 2.0)); |
||||
M = std::max(M, fabs(d)); |
||||
|
||||
wordIter->V = V; |
||||
wordIter->M = M; |
||||
} |
||||
} |
||||
|
||||
double FabMapFBO::limitbisection(double v, double m) { |
||||
double midpoint, left_val, mid_val; |
||||
double left = 0, right = bisectionStart; |
||||
|
||||
left_val = bennettInequality(v, m, left) - PsGd; |
||||
|
||||
for(int i = 0; i < bisectionIts; i++) { |
||||
|
||||
midpoint = (left + right)*0.5; |
||||
mid_val = bennettInequality(v, m, midpoint)- PsGd; |
||||
|
||||
if(left_val * mid_val > 0) { |
||||
left = midpoint; |
||||
left_val = mid_val; |
||||
} else { |
||||
right = midpoint; |
||||
} |
||||
} |
||||
|
||||
return (right + left) * 0.5; |
||||
} |
||||
|
||||
double FabMapFBO::bennettInequality(double v, double m, double delta) { |
||||
double DMonV = delta * m / v; |
||||
double f_delta = log(DMonV + sqrt(pow(DMonV, 2.0) + 1)); |
||||
return exp((v / pow(m, 2.0))*(cosh(f_delta) - 1 - DMonV * f_delta)); |
||||
} |
||||
|
||||
bool FabMapFBO::compInfo(const WordStats& first, const WordStats& second) { |
||||
return first.info < second.info; |
||||
} |
||||
|
||||
FabMap2::FabMap2(const Mat& _clTree, double _PzGe, double _PzGNe, |
||||
int _flags) : |
||||
FabMap(_clTree, _PzGe, _PzGNe, _flags) { |
||||
CV_Assert(flags & SAMPLED); |
||||
|
||||
children.resize(clTree.cols); |
||||
|
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
d1.push_back(log((this->*PzGL)(q, false, false, true) / |
||||
(this->*PzGL)(q, false, false, false))); |
||||
d2.push_back(log((this->*PzGL)(q, false, true, true) / |
||||
(this->*PzGL)(q, false, true, false)) - d1[q]); |
||||
d3.push_back(log((this->*PzGL)(q, true, false, true) / |
||||
(this->*PzGL)(q, true, false, false))- d1[q]); |
||||
d4.push_back(log((this->*PzGL)(q, true, true, true) / |
||||
(this->*PzGL)(q, true, true, false))- d1[q]); |
||||
children[pq(q)].push_back(q); |
||||
} |
||||
|
||||
} |
||||
|
||||
FabMap2::~FabMap2() { |
||||
} |
||||
|
||||
|
||||
void FabMap2::addTraining(const vector<Mat>& queryImgDescriptors) { |
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
trainingImgDescriptors.push_back(queryImgDescriptors[i]); |
||||
addToIndex(queryImgDescriptors[i], trainingDefaults, trainingInvertedMap); |
||||
} |
||||
} |
||||
|
||||
|
||||
void FabMap2::add(const vector<Mat>& queryImgDescriptors) { |
||||
for (size_t i = 0; i < queryImgDescriptors.size(); i++) { |
||||
CV_Assert(!queryImgDescriptors[i].empty()); |
||||
CV_Assert(queryImgDescriptors[i].rows == 1); |
||||
CV_Assert(queryImgDescriptors[i].cols == clTree.cols); |
||||
CV_Assert(queryImgDescriptors[i].type() == CV_32F); |
||||
testImgDescriptors.push_back(queryImgDescriptors[i]); |
||||
addToIndex(queryImgDescriptors[i], testDefaults, testInvertedMap); |
||||
} |
||||
} |
||||
|
||||
void FabMap2::getLikelihoods(const Mat& queryImgDescriptor, |
||||
const vector<Mat>& testImgDescriptors, vector<IMatch>& matches) { |
||||
|
||||
if (&testImgDescriptors== &(this->testImgDescriptors)) { |
||||
getIndexLikelihoods(queryImgDescriptor, testDefaults, testInvertedMap, |
||||
matches); |
||||
} else { |
||||
CV_Assert(!(flags & MOTION_MODEL)); |
||||
vector<double> defaults; |
||||
std::map<int, vector<int> > invertedMap; |
||||
for (size_t i = 0; i < testImgDescriptors.size(); i++) { |
||||
addToIndex(testImgDescriptors[i],defaults,invertedMap); |
||||
} |
||||
getIndexLikelihoods(queryImgDescriptor, defaults, invertedMap, matches); |
||||
} |
||||
} |
||||
|
||||
double FabMap2::getNewPlaceLikelihood(const Mat& queryImgDescriptor) { |
||||
|
||||
CV_Assert(!trainingImgDescriptors.empty()); |
||||
|
||||
vector<IMatch> matches; |
||||
getIndexLikelihoods(queryImgDescriptor, trainingDefaults, |
||||
trainingInvertedMap, matches); |
||||
|
||||
double averageLogLikelihood = -DBL_MAX + matches.front().likelihood + 1; |
||||
for (size_t i = 0; i < matches.size(); i++) { |
||||
averageLogLikelihood = |
||||
logsumexp(matches[i].likelihood, averageLogLikelihood); |
||||
} |
||||
|
||||
return averageLogLikelihood - log((double)trainingDefaults.size()); |
||||
|
||||
} |
||||
|
||||
void FabMap2::addToIndex(const Mat& queryImgDescriptor, |
||||
vector<double>& defaults, |
||||
std::map<int, vector<int> >& invertedMap) { |
||||
defaults.push_back(0); |
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
if (queryImgDescriptor.at<float>(0,q) > 0) { |
||||
defaults.back() += d1[q]; |
||||
invertedMap[q].push_back((int)defaults.size()-1); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void FabMap2::getIndexLikelihoods(const Mat& queryImgDescriptor, |
||||
std::vector<double>& defaults, |
||||
std::map<int, vector<int> >& invertedMap, |
||||
std::vector<IMatch>& matches) { |
||||
|
||||
vector<int>::iterator LwithI, child; |
||||
|
||||
std::vector<double> likelihoods = defaults; |
||||
|
||||
for (int q = 0; q < clTree.cols; q++) { |
||||
if (queryImgDescriptor.at<float>(0,q) > 0) { |
||||
for (LwithI = invertedMap[q].begin(); |
||||
LwithI != invertedMap[q].end(); LwithI++) { |
||||
|
||||
if (queryImgDescriptor.at<float>(0,pq(q)) > 0) { |
||||
likelihoods[*LwithI] += d4[q]; |
||||
} else { |
||||
likelihoods[*LwithI] += d3[q]; |
||||
} |
||||
} |
||||
for (child = children[q].begin(); child != children[q].end(); |
||||
child++) { |
||||
|
||||
if (queryImgDescriptor.at<float>(0,*child) == 0) { |
||||
for (LwithI = invertedMap[*child].begin(); |
||||
LwithI != invertedMap[*child].end(); LwithI++) { |
||||
|
||||
likelihoods[*LwithI] += d2[*child]; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
for (size_t i = 0; i < likelihoods.size(); i++) { |
||||
matches.push_back(IMatch(0,(int)i,likelihoods[i],0)); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 30 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 30 KiB |
@ -0,0 +1,197 @@ |
||||
/*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.
|
||||
//
|
||||
// This file originates from the openFABMAP project:
|
||||
// [http://code.google.com/p/openfabmap/]
|
||||
//
|
||||
// For published work which uses all or part of OpenFABMAP, please cite:
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
|
||||
//
|
||||
// Original Algorithm by Mark Cummins and Paul Newman:
|
||||
// [http://ijr.sagepub.com/content/27/6/647.short]
|
||||
// [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
|
||||
// [http://ijr.sagepub.com/content/30/9/1100.abstract]
|
||||
//
|
||||
// License Agreement
|
||||
//
|
||||
// Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
|
||||
// Will Maddern [w.maddern@qut.edu.au], all rights reserved.
|
||||
//
|
||||
//
|
||||
// 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 "opencv2/opencv.hpp" |
||||
#include "opencv2/nonfree/nonfree.hpp" |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
int main(int argc, char * argv[]) { |
||||
|
||||
cout << "This sample program demonstrates the FAB-MAP image matching " |
||||
"algorithm" << endl << endl; |
||||
|
||||
string dataDir; |
||||
if (argc == 1) { |
||||
dataDir = "fabmap/"; |
||||
} else if (argc == 2) { |
||||
dataDir = string(argv[1]); |
||||
dataDir += "/"; |
||||
} else { |
||||
//incorrect arguments
|
||||
cout << "Usage: fabmap_sample <sample data directory>" << |
||||
endl; |
||||
return -1; |
||||
} |
||||
|
||||
FileStorage fs; |
||||
|
||||
//load/generate vocab
|
||||
cout << "Loading Vocabulary: " << |
||||
dataDir + string("vocab_small.yml") << endl << endl; |
||||
fs.open(dataDir + string("vocab_small.yml"), FileStorage::READ); |
||||
Mat vocab; |
||||
fs["Vocabulary"] >> vocab; |
||||
if (vocab.empty()) { |
||||
cerr << "Vocabulary not found" << endl; |
||||
return -1; |
||||
} |
||||
fs.release(); |
||||
|
||||
//load/generate training data
|
||||
|
||||
cout << "Loading Training Data: " << |
||||
dataDir + string("train_data_small.yml") << endl << endl; |
||||
fs.open(dataDir + string("train_data_small.yml"), FileStorage::READ); |
||||
Mat trainData; |
||||
fs["BOWImageDescs"] >> trainData; |
||||
if (trainData.empty()) { |
||||
cerr << "Training Data not found" << endl; |
||||
return -1; |
||||
} |
||||
fs.release(); |
||||
|
||||
//create Chow-liu tree
|
||||
cout << "Making Chow-Liu Tree from training data" << endl << |
||||
endl; |
||||
of2::ChowLiuTree treeBuilder; |
||||
treeBuilder.add(trainData); |
||||
Mat tree = treeBuilder.make(); |
||||
|
||||
//generate test data
|
||||
cout << "Extracting Test Data from images" << endl << |
||||
endl; |
||||
Ptr<FeatureDetector> detector = |
||||
new DynamicAdaptedFeatureDetector( |
||||
AdjusterAdapter::create("STAR"), 130, 150, 5); |
||||
Ptr<DescriptorExtractor> extractor = |
||||
new SurfDescriptorExtractor(1000, 4, 2, false, true); |
||||
Ptr<DescriptorMatcher> matcher = |
||||
DescriptorMatcher::create("FlannBased"); |
||||
|
||||
BOWImgDescriptorExtractor bide(extractor, matcher); |
||||
bide.setVocabulary(vocab); |
||||
|
||||
vector<string> imageNames; |
||||
imageNames.push_back(string("stlucia_test_small0000.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0001.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0002.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0003.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0004.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0005.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0006.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0007.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0008.jpeg")); |
||||
imageNames.push_back(string("stlucia_test_small0009.jpeg")); |
||||
|
||||
Mat testData; |
||||
Mat frame; |
||||
Mat bow; |
||||
vector<KeyPoint> kpts; |
||||
|
||||
for(size_t i = 0; i < imageNames.size(); i++) { |
||||
cout << dataDir + imageNames[i] << endl; |
||||
frame = imread(dataDir + imageNames[i]); |
||||
if(frame.empty()) { |
||||
cerr << "Test images not found" << endl; |
||||
return -1; |
||||
} |
||||
|
||||
detector->detect(frame, kpts); |
||||
|
||||
bide.compute(frame, kpts, bow); |
||||
|
||||
testData.push_back(bow); |
||||
|
||||
drawKeypoints(frame, kpts, frame); |
||||
imshow(imageNames[i], frame); |
||||
waitKey(10); |
||||
} |
||||
|
||||
//run fabmap
|
||||
cout << "Running FAB-MAP algorithm" << endl << |
||||
endl; |
||||
Ptr<of2::FabMap> fabmap; |
||||
|
||||
fabmap = new of2::FabMap2(tree, 0.39, 0, of2::FabMap::SAMPLED | |
||||
of2::FabMap::CHOW_LIU); |
||||
fabmap->addTraining(trainData); |
||||
|
||||
vector<of2::IMatch> matches; |
||||
fabmap->compare(testData, matches, true); |
||||
|
||||
//display output
|
||||
Mat result_small = Mat::zeros(10, 10, CV_8UC1); |
||||
vector<of2::IMatch>::iterator l; |
||||
|
||||
for(l = matches.begin(); l != matches.end(); l++) { |
||||
if(l->imgIdx < 0) { |
||||
result_small.at<char>(l->queryIdx, l->queryIdx) = |
||||
(char)(l->match*255); |
||||
|
||||
} else { |
||||
result_small.at<char>(l->queryIdx, l->imgIdx) = |
||||
(char)(l->match*255); |
||||
} |
||||
} |
||||
|
||||
Mat result_large(100, 100, CV_8UC1); |
||||
resize(result_small, result_large, Size(500, 500), 0, 0, CV_INTER_NN); |
||||
|
||||
imshow("Confusion Matrix", result_large); |
||||
waitKey(); |
||||
|
||||
cout << endl << "Press any key to exit" << endl; |
||||
|
||||
return 0; |
||||
} |