feature_adjuster.h

Alternative classes for DynamicAdaptedFeatureDetector and AdjusterAdapter - Felix Endres, 2014-08-19 03:25 pm

Download (3.7 kB)

 
1
#ifndef FEATUREADJUSTER_H
2
#define FEATUREADJUSTER_H
3
#include <opencv2/features2d/features2d.hpp>
4
5
/** \brief an detector adjuster optimized for image sequences (video).
6
 * Use this Adjuster with the DynamicAdaptedFeatureDetector. 
7
 * It lets you set the increase/decrease factor for faster adaptation.
8
 * It works for SURF, SIFT, FAST and the adjustable ORB variant "AORB" 
9
 * which exposes its FAST threshold.
10
 */
11
class DetectorAdjuster: public cv::AdjusterAdapter
12
{
13
public:
14
    ///Initial values are for SURF detector
15
    DetectorAdjuster(const char* detector_name, double initial_thresh=200.f, double min_thresh=2, double max_thresh=10000, double increase_factor=1.3, double decrease_factor=0.7 );
16
    
17
    virtual void tooFew(int minv, int n_detected);
18
    virtual void tooMany(int maxv, int n_detected);
19
    virtual bool good() const;
20
21
    virtual cv::Ptr<cv::AdjusterAdapter> clone() const;
22
23
    void setIncreaseFactor(double new_factor);
24
    void setDecreaseFactor(double new_factor);
25
protected:
26
    virtual void detectImpl( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, const cv::Mat& mask=cv::Mat() ) const;
27
28
    double thresh_, init_thresh_, min_thresh_, max_thresh_;
29
    double increase_factor_, decrease_factor_;
30
    const char* detector_name_;
31
};
32
33
/** \brief an adaptively adjusting detector that iteratively detects until the desired number
34
 * of features are detected.
35
 *  Beware that this is not thread safe - as the adjustment of parameters breaks the const
36
 *  of the detection routine...
37
 *  /TODO Make this const correct and thread safe
38
 *
39
 *  sample usage:
40
 //will create a detector that attempts to find 100 - 110 FAST Keypoints, and will at most run
41
 //FAST feature detection 10 times until that number of keypoints are found
42
 Ptr<FeatureDetector> detector(new DynamicAdaptedFeatureDetector(new FastAdjuster(20,true),100, 110, 10));
43
44
 In contrast to the original DynamicAdaptedFeatureDetector, this variant is enhanced for
45
 processing of video sequences. It is meant to work with the DetectorAdjuster.
46
 It keeps the DetectorAdjuster alive, so that the final threshold will be retained
47
 throughout detection calls. For video sequences the "good" threshold will in
48
 general be similar for successive frames, therefore the last "good" threshold
49
 is a good starting point for the next frame.
50
51
 In case of too many features, this variant will just decrease the threshold of the
52
 DetectorAdjuster without triggering redetection, so the user needs to get rid of the
53
 superfluous keypoints. Mostly the keypoints are scored anyway, so this avoids
54
 costly and unnecessary redetections. 
55
 */
56
class DynamicAdaptedFeatureDetectorWithStorage: public cv::FeatureDetector
57
{
58
public:
59
60
    /** \param adjuster an AdjusterAdapter that will do the detection and parameter adjustment
61
     *  \param max_features the maximum desired number of features
62
     *  \param max_iters the maximum number of times to try to adjust the feature detector params
63
     *          for the FastAdjuster this can be high, but with Star or Surf this can get time consuming
64
     *  \param min_features the minimum desired features
65
     */
66
    DynamicAdaptedFeatureDetectorWithStorage( cv::Ptr<cv::AdjusterAdapter> adjuster, int min_features=400, int max_features=500, int max_iters=5 );
67
68
    virtual bool empty() const;
69
70
protected:
71
    virtual void detectImpl( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, const cv::Mat& mask=cv::Mat() ) const;
72
73
private:
74
    DynamicAdaptedFeatureDetectorWithStorage& operator=(const DynamicAdaptedFeatureDetectorWithStorage&);
75
    DynamicAdaptedFeatureDetectorWithStorage(const DynamicAdaptedFeatureDetectorWithStorage&);
76
77
    int escape_iters_;
78
    int min_features_, max_features_;
79
    mutable cv::Ptr<cv::AdjusterAdapter> adjuster_;
80
};
81
#endif