2011-05-04 04:41:11 +08:00
Feature Detection and Description
2011-02-23 04:43:26 +08:00
=================================
.. highlight :: cpp
.. index :: FAST
2011-03-01 05:26:43 +08:00
FAST
2011-02-23 04:43:26 +08:00
--------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void FAST( const Mat& image, vector<KeyPoint>& keypoints, int threshold, bool nonmaxSupression=true )
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Detects corners using the FAST algorithm by E. Rosten (*Machine learning for high-speed corner detection* , 2006).
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param image: Image where keypoints (corners) are detected.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
:param keypoints: Keypoints detected on the image.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param threshold: Threshold on difference between intensity of the central pixel and pixels on a circle around this pixel. See the algorithm description below.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param nonmaxSupression: If it is true, non-maximum supression is applied to detected corners (keypoints).
2011-02-23 04:43:26 +08:00
.. index :: MSER
.. _MSER:
MSER
----
2011-06-16 20:48:23 +08:00
.. ocv:class :: MSER
2011-02-23 04:43:26 +08:00
2011-05-05 21:31:54 +08:00
Maximally stable extremal region extractor ::
2011-02-23 04:43:26 +08:00
class MSER : public CvMSERParams
{
public:
// default constructor
MSER();
// constructor that initializes all the algorithm parameters
MSER( int _delta, int _min_area, int _max_area,
float _max_variation, float _min_diversity,
int _max_evolution, double _area_threshold,
double _min_margin, int _edge_blur_size );
// runs the extractor on the specified image; returns the MSERs,
// each encoded as a contour (vector<Point>, see findContours)
// the optional mask marks the area where MSERs are searched for
void operator()( const Mat& image, vector<vector<Point> >& msers, const Mat& mask ) const;
};
2011-05-04 04:41:11 +08:00
The class encapsulates all the parameters of the MSER extraction algorithm (see
http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions).
2011-02-23 04:43:26 +08:00
.. index :: StarDetector
.. _StarDetector:
StarDetector
------------
2011-06-16 20:48:23 +08:00
.. ocv:class :: StarDetector
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Class implementing the Star keypoint detector ::
2011-02-23 04:43:26 +08:00
class StarDetector : CvStarDetectorParams
{
public:
// default constructor
StarDetector();
2011-05-04 04:41:11 +08:00
// the full constructor that initializes all the algorithm parameters:
2011-02-26 19:05:10 +08:00
// maxSize - maximum size of the features. The following
2011-02-23 04:43:26 +08:00
// values of the parameter are supported:
// 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128
// responseThreshold - threshold for the approximated laplacian,
// used to eliminate weak features. The larger it is,
// the less features will be retrieved
2011-02-26 19:05:10 +08:00
// lineThresholdProjected - another threshold for the laplacian to
2011-02-23 04:43:26 +08:00
// eliminate edges
2011-02-26 19:05:10 +08:00
// lineThresholdBinarized - another threshold for the feature
2011-02-23 04:43:26 +08:00
// size to eliminate edges.
2011-05-04 04:41:11 +08:00
// The larger the 2nd threshold, the more points you get.
2011-02-23 04:43:26 +08:00
StarDetector(int maxSize, int responseThreshold,
int lineThresholdProjected,
int lineThresholdBinarized,
int suppressNonmaxSize);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
// finds keypoints in an image
void operator()(const Mat& image, vector<KeyPoint>& keypoints) const;
};
2011-05-08 23:30:00 +08:00
The class implements a modified version of the `` CenSurE `` keypoint detector described in
2011-05-05 21:31:54 +08:00
[Agrawal08].
2011-02-23 04:43:26 +08:00
.. index :: SIFT
.. _SIFT:
SIFT
----
2011-06-16 20:48:23 +08:00
.. ocv:class :: SIFT
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Class for extracting keypoints and computing descriptors using the Scale Invariant Feature Transform (SIFT) approach ::
2011-02-23 04:43:26 +08:00
class CV_EXPORTS SIFT
{
public:
struct CommonParams
{
static const int DEFAULT_NOCTAVES = 4;
static const int DEFAULT_NOCTAVE_LAYERS = 3;
static const int DEFAULT_FIRST_OCTAVE = -1;
enum{ FIRST_ANGLE = 0, AVERAGE_ANGLE = 1 };
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
CommonParams();
2011-02-26 19:05:10 +08:00
CommonParams( int _nOctaves, int _nOctaveLayers, int _firstOctave,
2011-02-23 04:43:26 +08:00
int _angleMode );
int nOctaves, nOctaveLayers, firstOctave;
int angleMode;
};
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
struct DetectorParams
{
2011-02-26 19:05:10 +08:00
static double GET_DEFAULT_THRESHOLD()
2011-02-23 04:43:26 +08:00
{ return 0.04 / SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS / 2.0; }
static double GET_DEFAULT_EDGE_THRESHOLD() { return 10.0; }
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
DetectorParams();
DetectorParams( double _threshold, double _edgeThreshold );
double threshold, edgeThreshold;
};
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
struct DescriptorParams
{
static double GET_DEFAULT_MAGNIFICATION() { return 3.0; }
static const bool DEFAULT_IS_NORMALIZE = true;
static const int DESCRIPTOR_SIZE = 128;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
DescriptorParams();
2011-02-26 19:05:10 +08:00
DescriptorParams( double _magnification, bool _isNormalize,
2011-02-23 04:43:26 +08:00
bool _recalculateAngles );
double magnification;
bool isNormalize;
bool recalculateAngles;
};
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
SIFT();
//! sift-detector constructor
SIFT( double _threshold, double _edgeThreshold,
int _nOctaves=CommonParams::DEFAULT_NOCTAVES,
int _nOctaveLayers=CommonParams::DEFAULT_NOCTAVE_LAYERS,
int _firstOctave=CommonParams::DEFAULT_FIRST_OCTAVE,
int _angleMode=CommonParams::FIRST_ANGLE );
//! sift-descriptor constructor
SIFT( double _magnification, bool _isNormalize=true,
bool _recalculateAngles = true,
int _nOctaves=CommonParams::DEFAULT_NOCTAVES,
int _nOctaveLayers=CommonParams::DEFAULT_NOCTAVE_LAYERS,
int _firstOctave=CommonParams::DEFAULT_FIRST_OCTAVE,
int _angleMode=CommonParams::FIRST_ANGLE );
SIFT( const CommonParams& _commParams,
const DetectorParams& _detectorParams = DetectorParams(),
const DescriptorParams& _descriptorParams = DescriptorParams() );
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
//! returns the descriptor size in floats (128)
int descriptorSize() const { return DescriptorParams::DESCRIPTOR_SIZE; }
2011-05-04 04:41:11 +08:00
//! finds the keypoints using the SIFT algorithm
2011-02-23 04:43:26 +08:00
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints) const;
2011-02-26 19:05:10 +08:00
//! finds the keypoints and computes descriptors for them using SIFT algorithm.
2011-02-23 04:43:26 +08:00
//! Optionally it can compute descriptors for the user-provided keypoints
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints,
Mat& descriptors,
bool useProvidedKeypoints=false) const;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
CommonParams getCommonParams () const { return commParams; }
DetectorParams getDetectorParams () const { return detectorParams; }
DescriptorParams getDescriptorParams () const { return descriptorParams; }
protected:
...
};
2011-03-01 05:26:43 +08:00
2011-02-23 04:43:26 +08:00
.. index :: SURF
.. _SURF:
SURF
----
2011-06-16 20:48:23 +08:00
.. ocv:class :: SURF
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Class for extracting Speeded Up Robust Features from an image ::
2011-02-23 04:43:26 +08:00
class SURF : public CvSURFParams
{
public:
2011-03-01 05:26:43 +08:00
// c:function::default constructor
2011-02-23 04:43:26 +08:00
SURF();
// constructor that initializes all the algorithm parameters
SURF(double _hessianThreshold, int _nOctaves=4,
int _nOctaveLayers=2, bool _extended=false);
// returns the number of elements in each descriptor (64 or 128)
int descriptorSize() const;
// detects keypoints using fast multi-scale Hessian detector
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints) const;
// detects keypoints and computes the SURF descriptors for them;
2011-02-26 19:05:10 +08:00
// output vector "descriptors" stores elements of descriptors and has size
// equal descriptorSize()*keypoints.size() as each descriptor is
2011-02-23 04:43:26 +08:00
// descriptorSize() elements of this vector.
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints,
vector<float>& descriptors,
bool useProvidedKeypoints=false) const;
};
2011-05-16 04:56:53 +08:00
The class implements the Speeded Up Robust Features descriptor
[Bay06].
2011-05-04 04:41:11 +08:00
There is a fast multi-scale Hessian keypoint detector that can be used to find keypoints
2011-05-16 04:56:53 +08:00
(default option). But the descriptors can be also computed for the user-specified keypoints.
2011-05-05 21:31:54 +08:00
The algorithm can be used for object tracking and localization, image stitching, and so on. See the `` find_obj.cpp `` demo in OpenCV samples directory.
2011-02-23 04:43:26 +08:00
2011-05-21 06:25:53 +08:00
.. index :: ORB
.. _ORB:
ORB
----
2011-06-16 20:48:23 +08:00
.. ocv:class :: ORB
2011-05-21 06:25:53 +08:00
Class for extracting ORB features and descriptors from an image ::
class ORB
{
public:
/** The patch sizes that can be used (only one right now) * /
struct CommonParams
{
2011-06-01 10:09:31 +08:00
enum { DEFAULT_N_LEVELS = 3, DEFAULT_FIRST_LEVEL = 0};
2011-05-21 06:25:53 +08:00
/** default constructor * /
2011-06-01 10:09:31 +08:00
CommonParams(float scale_factor = 1.2f, unsigned int n_levels = DEFAULT_N_LEVELS,
int edge_threshold = 31, unsigned int first_level = DEFAULT_FIRST_LEVEL);
2011-05-21 06:25:53 +08:00
void read(const FileNode& fn);
void write(FileStorage& fs) const;
/** Coefficient by which we divide the dimensions from one scale pyramid level to the next * /
float scale_factor_;
/** The number of levels in the scale pyramid * /
unsigned int n_levels_;
/** The level at which the image is given
* if 1, that means we will also look at the image scale_factor_ times bigger
*/
unsigned int first_level_;
2011-06-01 10:09:31 +08:00
/** How far from the boundary the points should be * /
int edge_threshold_;
2011-05-21 06:25:53 +08:00
};
// c:function::default constructor
ORB();
// constructor that initializes all the algorithm parameters
ORB( const CommonParams detector_params );
// returns the number of elements in each descriptor (32 bytes)
int descriptorSize() const;
// detects keypoints using ORB
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints) const;
// detects ORB keypoints and computes the ORB descriptors for them;
// output vector "descriptors" stores elements of descriptors and has size
// equal descriptorSize()*keypoints.size() as each descriptor is
// descriptorSize() elements of this vector.
void operator()(const Mat& img, const Mat& mask,
vector<KeyPoint>& keypoints,
cv::Mat& descriptors,
bool useProvidedKeypoints=false) const;
};
The class implements ORB
2011-02-23 04:43:26 +08:00
.. index :: RandomizedTree
.. _RandomizedTree:
RandomizedTree
--------------
2011-06-16 20:48:23 +08:00
.. ocv:class :: RandomizedTree
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Class containing a base structure for `` RTreeClassifier `` ::
2011-02-23 04:43:26 +08:00
class CV_EXPORTS RandomizedTree
2011-02-26 19:05:10 +08:00
{
2011-02-23 04:43:26 +08:00
public:
2011-02-26 19:05:10 +08:00
friend class RTreeClassifier;
2011-02-23 04:43:26 +08:00
RandomizedTree();
~RandomizedTree();
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
void train(std::vector<BaseKeypoint> const& base_set,
2011-03-01 05:26:43 +08:00
RNG &rng, int depth, int views,
2011-02-23 04:43:26 +08:00
size_t reduced_num_dim, int num_quant_bits);
void train(std::vector<BaseKeypoint> const& base_set,
2011-03-01 05:26:43 +08:00
RNG &rng, PatchGenerator &make_patch, int depth,
2011-02-23 04:43:26 +08:00
int views, size_t reduced_num_dim, int num_quant_bits);
2011-02-26 19:05:10 +08:00
2011-05-04 04:41:11 +08:00
// next two functions are EXPERIMENTAL
2011-02-23 04:43:26 +08:00
//(do not use unless you know exactly what you do)
static void quantizeVector(float *vec, int dim, int N, float bnds[2],
int clamp_mode=0);
static void quantizeVector(float *src, int dim, int N, float bnds[2],
2011-02-26 19:05:10 +08:00
uchar *dst);
2011-02-23 04:43:26 +08:00
// patch_data must be a 32x32 array (no row padding)
float* getPosterior(uchar* patch_data);
const float* getPosterior(uchar* patch_data) const;
uchar* getPosterior2(uchar* patch_data);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
void read(const char* file_name, int num_quant_bits);
void read(std::istream &is, int num_quant_bits);
void write(const char* file_name) const;
void write(std::ostream &os) const;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
int classes() { return classes_; }
int depth() { return depth_; }
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
void discardFloatPosteriors() { freePosteriors(1); }
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
inline void applyQuantization(int num_quant_bits)
{ makePosteriors2(num_quant_bits); }
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
private:
int classes_;
int depth_;
2011-02-26 19:05:10 +08:00
int num_leaves_;
std::vector<RTreeNode> nodes_;
2011-05-04 04:41:11 +08:00
float **posteriors_; // 16-byte aligned posteriors
uchar **posteriors2_; // 16-byte aligned posteriors
2011-02-23 04:43:26 +08:00
std::vector<int> leaf_counts_;
2011-02-26 19:05:10 +08:00
2011-03-01 05:26:43 +08:00
void createNodes(int num_nodes, RNG &rng);
2011-02-23 04:43:26 +08:00
void allocPosteriorsAligned(int num_leaves, int num_classes);
2011-02-26 19:05:10 +08:00
void freePosteriors(int which);
2011-02-23 04:43:26 +08:00
// which: 1=posteriors_, 2=posteriors2_, 3=both
2011-03-01 05:26:43 +08:00
void init(int classes, int depth, RNG &rng);
2011-02-23 04:43:26 +08:00
void addExample(int class_id, uchar* patch_data);
2011-02-26 19:05:10 +08:00
void finalize(size_t reduced_num_dim, int num_quant_bits);
2011-02-23 04:43:26 +08:00
int getIndex(uchar* patch_data) const;
inline float* getPosteriorByIndex(int index);
inline uchar* getPosteriorByIndex2(int index);
inline const float* getPosteriorByIndex(int index) const;
void convertPosteriorsToChar();
void makePosteriors2(int num_quant_bits);
2011-02-26 19:05:10 +08:00
void compressLeaves(size_t reduced_num_dim);
2011-02-23 04:43:26 +08:00
void estimateQuantPercForPosteriors(float perc[2]);
};
.. index :: RandomizedTree::train
2011-03-01 05:26:43 +08:00
RandomizedTree::train
2011-02-23 04:43:26 +08:00
-------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void train(std::vector<BaseKeypoint> const& base_set, RNG& rng, PatchGenerator& make_patch, int depth, int views, size_t reduced_num_dim, int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Trains a randomized tree using an input set of keypoints.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void train(std::vector<BaseKeypoint> const& base_set, RNG& rng, PatchGenerator& make_patch, int depth, int views, size_t reduced_num_dim, int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-05-16 04:56:53 +08:00
:param base_set: Vector of the ``BaseKeypoint`` type. It contains image keypoints used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param rng: Random-number generator used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param make_patch: Patch generator used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param depth: Maximum tree depth.
2011-02-26 19:05:10 +08:00
2011-05-08 23:30:00 +08:00
:param views: Number of random views of each keypoint neighborhood to generate.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param reduced_num_dim: Number of dimensions used in the compressed signature.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param num_quant_bits: Number of bits used for quantization.
2011-02-23 04:43:26 +08:00
.. index :: RandomizedTree::read
2011-03-01 05:26:43 +08:00
RandomizedTree::read
2011-02-23 04:43:26 +08:00
------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: read(const char* file_name, int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: read(std::istream &is, int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
Reads a pre-saved randomized tree from a file or stream.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param file_name: Name of the file that contains randomized tree data.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param is: Input stream associated with the file that contains randomized tree data.
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
:param num_quant_bits: Number of bits used for quantization.
2011-02-23 04:43:26 +08:00
.. index :: RandomizedTree::write
2011-03-01 05:26:43 +08:00
RandomizedTree::write
2011-02-23 04:43:26 +08:00
-------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void write(const char* file_name) const
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Writes the current randomized tree to a file or stream.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void write(std::ostream \&os) const
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param file_name: Name of the file where randomized tree data is stored.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param is: Output stream associated with the file where randomized tree data is stored.
2011-02-23 04:43:26 +08:00
.. index :: RandomizedTree::applyQuantization
2011-03-01 05:26:43 +08:00
RandomizedTree::applyQuantization
2011-02-23 04:43:26 +08:00
-------------------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void applyQuantization(int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Applies quantization to the current randomized tree.
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
:param num_quant_bits: Number of bits used for quantization.
2011-02-23 04:43:26 +08:00
.. index :: RTreeNode
.. _RTreeNode:
RTreeNode
---------
2011-06-16 20:48:23 +08:00
.. ocv:class :: RTreeNode
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Class containing a base structure for `` RandomizedTree `` ::
2011-02-23 04:43:26 +08:00
struct RTreeNode
{
short offset1, offset2;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
RTreeNode() {}
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
RTreeNode(uchar x1, uchar y1, uchar x2, uchar y2)
: offset1(y1*PATCH_SIZE + x1),
offset2(y2*PATCH_SIZE + x2)
{}
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
//! Left child on 0, right child on 1
inline bool operator() (uchar* patch_data) const
{
return patch_data[offset1] > patch_data[offset2];
}
};
.. index :: RTreeClassifier
.. _RTreeClassifier:
RTreeClassifier
---------------
2011-06-16 20:48:23 +08:00
.. ocv:class :: RTreeClassifier
2011-02-23 04:43:26 +08:00
2011-05-16 04:56:53 +08:00
Class containing `` RTreeClassifier `` . It represents the Calonder descriptor that was originally introduced by Michael Calonder. ::
2011-02-23 04:43:26 +08:00
class CV_EXPORTS RTreeClassifier
2011-02-26 19:05:10 +08:00
{
2011-02-23 04:43:26 +08:00
public:
static const int DEFAULT_TREES = 48;
2011-02-26 19:05:10 +08:00
static const size_t DEFAULT_NUM_QUANT_BITS = 4;
2011-02-23 04:43:26 +08:00
RTreeClassifier();
2011-02-26 19:05:10 +08:00
void train(std::vector<BaseKeypoint> const& base_set,
2011-03-01 05:26:43 +08:00
RNG &rng,
2011-02-23 04:43:26 +08:00
int num_trees = RTreeClassifier::DEFAULT_TREES,
int depth = DEFAULT_DEPTH,
int views = DEFAULT_VIEWS,
size_t reduced_num_dim = DEFAULT_REDUCED_NUM_DIM,
int num_quant_bits = DEFAULT_NUM_QUANT_BITS,
bool print_status = true);
void train(std::vector<BaseKeypoint> const& base_set,
2011-03-01 05:26:43 +08:00
RNG &rng,
2011-02-23 04:43:26 +08:00
PatchGenerator &make_patch,
int num_trees = RTreeClassifier::DEFAULT_TREES,
int depth = DEFAULT_DEPTH,
int views = DEFAULT_VIEWS,
size_t reduced_num_dim = DEFAULT_REDUCED_NUM_DIM,
int num_quant_bits = DEFAULT_NUM_QUANT_BITS,
bool print_status = true);
2011-02-26 19:05:10 +08:00
// sig must point to a memory block of at least
2011-02-23 04:43:26 +08:00
//classes()*sizeof(float|uchar) bytes
void getSignature(IplImage *patch, uchar * sig);
void getSignature(IplImage *patch, float * sig);
void getSparseSignature(IplImage *patch, float * sig,
float thresh);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
static int countNonZeroElements(float *vec, int n, double tol=1e-10);
static inline void safeSignatureAlloc(uchar **sig, int num_sig=1,
int sig_len=176);
static inline uchar* safeSignatureAlloc(int num_sig=1,
2011-02-26 19:05:10 +08:00
int sig_len=176);
2011-02-23 04:43:26 +08:00
inline int classes() { return classes_; }
inline int original_num_classes()
{ return original_num_classes_; }
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
void setQuantization(int num_quant_bits);
void discardFloatPosteriors();
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
void read(const char* file_name);
void read(std::istream &is);
void write(const char* file_name) const;
void write(std::ostream &os) const;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
std::vector<RandomizedTree> trees_;
2011-02-26 19:05:10 +08:00
private:
2011-02-23 04:43:26 +08:00
int classes_;
int num_quant_bits_;
uchar **posteriors_;
ushort *ptemp_;
2011-02-26 19:05:10 +08:00
int original_num_classes_;
2011-02-23 04:43:26 +08:00
bool keep_floats_;
};
.. index :: RTreeClassifier::train
2011-03-01 05:26:43 +08:00
RTreeClassifier::train
2011-02-23 04:43:26 +08:00
--------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void train(vector<BaseKeypoint> const& base_set, RNG& rng, int num_trees = RTreeClassifier::DEFAULT_TREES, int depth = DEFAULT_DEPTH, int views = DEFAULT_VIEWS, size_t reduced_num_dim = DEFAULT_REDUCED_NUM_DIM, int num_quant_bits = DEFAULT_NUM_QUANT_BITS, bool print_status = true)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Trains a randomized tree classifier using an input set of keypoints.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void train(vector<BaseKeypoint> const& base_set, RNG& rng, PatchGenerator& make_patch, int num_trees = RTreeClassifier::DEFAULT_TREES, int depth = DEFAULT_DEPTH, int views = DEFAULT_VIEWS, size_t reduced_num_dim = DEFAULT_REDUCED_NUM_DIM, int num_quant_bits = DEFAULT_NUM_QUANT_BITS, bool print_status = true)
2011-02-23 04:43:26 +08:00
2011-05-16 04:56:53 +08:00
:param base_set: Vector of the ``BaseKeypoint`` type. It contains image keypoints used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param rng: Random-number generator used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param make_patch: Patch generator used for training.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param num_trees: Number of randomized trees used in ``RTreeClassificator`` .
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param depth: Maximum tree depth.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param views: Number of random views of each keypoint neighborhood to generate.
2011-02-26 19:05:10 +08:00
2011-05-08 23:30:00 +08:00
:param reduced_num_dim: Number of dimensions used in the compressed signature.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param num_quant_bits: Number of bits used for quantization.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param print_status: Current status of training printed on the console.
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::getSignature
2011-03-01 05:26:43 +08:00
RTreeClassifier::getSignature
2011-02-23 04:43:26 +08:00
---------------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void getSignature(IplImage *patch, uchar * sig)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Returns a signature for an image patch.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void getSignature(IplImage *patch, float * sig)
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
:param patch: Image patch to calculate the signature for.
:param sig: Output signature (array dimension is ``reduced_num_dim)`` .
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::getSparseSignature
2011-03-01 05:26:43 +08:00
RTreeClassifier::getSparseSignature
---------------------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void getSparseSignature(IplImage *patch, float * sig, float thresh)
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
Returns a signature for an image patch similarly to `` getSignature `` but uses a threshold for removing all signature elements below the threshold so that the signature is compressed.
2011-02-23 04:43:26 +08:00
2011-05-16 04:56:53 +08:00
:param patch: Image patch to calculate the signature for.
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param sig: Output signature (array dimension is ``reduced_num_dim)`` .
2011-05-05 21:31:54 +08:00
2011-05-08 23:30:00 +08:00
:param thresh: Threshold that is used for compressing the signature.
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::countNonZeroElements
2011-03-01 05:26:43 +08:00
RTreeClassifier::countNonZeroElements
2011-02-23 04:43:26 +08:00
-----------------------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: static int countNonZeroElements(float *vec, int n, double tol=1e-10)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Returns the number of non-zero elements in an input array.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param vec: Input vector containing float elements.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param n: Input vector size.
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
:param tol: Threshold used for counting elements. All elements less than ``tol`` are considered as zero elements.
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::read
2011-03-01 05:26:43 +08:00
RTreeClassifier::read
2011-02-23 04:43:26 +08:00
-------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: read(const char* file_name)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Reads a pre-saved `` RTreeClassifier `` from a file or stream.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: read(std::istream& is)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param file_name: Name of the file that contains randomized tree data.
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param is: Input stream associated with the file that contains randomized tree data.
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::write
2011-03-01 05:26:43 +08:00
RTreeClassifier::write
2011-02-23 04:43:26 +08:00
--------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void write(const char* file_name) const
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
Writes the current `` RTreeClassifier `` to a file or stream.
2011-02-23 04:43:26 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void write(std::ostream &os) const
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
:param file_name: Name of the file where randomized tree data is stored.
2011-02-23 04:43:26 +08:00
2011-05-05 21:31:54 +08:00
:param os: Output stream associated with the file where randomized tree data is stored.
2011-02-23 04:43:26 +08:00
.. index :: RTreeClassifier::setQuantization
2011-03-01 05:26:43 +08:00
RTreeClassifier::setQuantization
2011-02-23 04:43:26 +08:00
------------------------------------
2011-06-16 20:48:23 +08:00
.. ocv:function :: void setQuantization(int num_quant_bits)
2011-02-23 04:43:26 +08:00
2011-05-04 04:41:11 +08:00
Applies quantization to the current randomized tree.
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
:param num_quant_bits: Number of bits used for quantization.
2011-02-23 04:43:26 +08:00
2011-05-08 23:30:00 +08:00
The example below demonstrates the usage of `` RTreeClassifier `` for matching the features. The features are extracted from the test and train images with SURF. Output is
2011-02-26 19:05:10 +08:00
:math: `best\_corr` and
2011-05-04 04:41:11 +08:00
:math: `best\_corr\_idx` arrays that keep the best probabilities and corresponding features indices for every train feature. ::
2011-02-23 04:43:26 +08:00
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq *objectKeypoints = 0, * objectDescriptors = 0;
CvSeq *imageKeypoints = 0, * imageDescriptors = 0;
CvSURFParams params = cvSURFParams(500, 1);
cvExtractSURF( test_image, 0, &imageKeypoints, &imageDescriptors,
storage, params );
cvExtractSURF( train_image, 0, &objectKeypoints, &objectDescriptors,
storage, params );
2011-02-26 19:05:10 +08:00
2011-03-01 05:26:43 +08:00
RTreeClassifier detector;
int patch_width = PATCH_SIZE;
iint patch_height = PATCH_SIZE;
vector<BaseKeypoint> base_set;
2011-02-23 04:43:26 +08:00
int i=0;
CvSURFPoint* point;
for (i=0;i<(n_points > 0 ? n_points : objectKeypoints->total);i++)
{
point=(CvSURFPoint*)cvGetSeqElem(objectKeypoints,i);
base_set.push_back(
2011-03-01 05:26:43 +08:00
BaseKeypoint(point->pt.x,point->pt.y,train_image));
2011-02-23 04:43:26 +08:00
}
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
//Detector training
2011-03-01 05:26:43 +08:00
RNG rng( cvGetTickCount() );
PatchGenerator gen(0,255,2,false,0.7,1.3,-CV_PI/3,CV_PI/3,
2011-02-23 04:43:26 +08:00
-CV_PI/3,CV_PI/3);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
printf("RTree Classifier training...n");
2011-03-01 05:26:43 +08:00
detector.train(base_set,rng,gen,24,DEFAULT_DEPTH,2000,
2011-02-23 04:43:26 +08:00
(int)base_set.size(), detector.DEFAULT_NUM_QUANT_BITS);
printf("Donen");
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
float* signature = new float[detector.original_num_classes()];
float* best_corr;
int* best_corr_idx;
if (imageKeypoints->total > 0)
{
best_corr = new float[imageKeypoints->total];
best_corr_idx = new int[imageKeypoints->total];
}
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
for(i=0; i < imageKeypoints->total; i++)
{
point=(CvSURFPoint*)cvGetSeqElem(imageKeypoints,i);
int part_idx = -1;
float prob = 0.0f;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
CvRect roi = cvRect((int)(point->pt.x) - patch_width/2,
(int)(point->pt.y) - patch_height/2,
patch_width, patch_height);
cvSetImageROI(test_image, roi);
roi = cvGetImageROI(test_image);
if(roi.width != patch_width || roi.height != patch_height)
{
best_corr_idx[i] = part_idx;
best_corr[i] = prob;
}
else
{
cvSetImageROI(test_image, roi);
IplImage* roi_image =
cvCreateImage(cvSize(roi.width, roi.height),
test_image->depth, test_image->nChannels);
cvCopy(test_image,roi_image);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
detector.getSignature(roi_image, signature);
for (int j = 0; j< detector.original_num_classes();j++)
{
if (prob < signature[j])
{
part_idx = j;
prob = signature[j];
}
}
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
best_corr_idx[i] = part_idx;
best_corr[i] = prob;
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
if (roi_image)
cvReleaseImage(&roi_image);
}
cvResetImageROI(test_image);
}
2011-03-01 05:26:43 +08:00
..