1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
47 DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector(const Ptr<AdjusterAdapter>& a,
48 int min_features, int max_features, int max_iters ) :
49 escape_iters_(max_iters), min_features_(min_features), max_features_(max_features), adjuster_(a)
52 bool DynamicAdaptedFeatureDetector::empty() const
54 return !adjuster_ || adjuster_->empty();
57 void DynamicAdaptedFeatureDetector::detectImpl(InputArray _image, std::vector<KeyPoint>& keypoints, InputArray _mask) const
59 Mat image = _image.getMat(), mask = _mask.getMat();
61 //for oscillation testing
65 //flag for whether the correct threshhold has been reached
66 bool thresh_good = false;
68 Ptr<AdjusterAdapter> adjuster = adjuster_->clone();
70 //break if the desired number hasn't been reached.
71 int iter_count = escape_iters_;
73 while( iter_count > 0 && !(down && up) && !thresh_good && adjuster->good() )
77 //the adjuster takes care of calling the detector with updated parameters
78 adjuster->detect(image, keypoints,mask);
80 if( int(keypoints.size()) < min_features_ )
83 adjuster->tooFew(min_features_, (int)keypoints.size());
85 else if( int(keypoints.size()) > max_features_ )
88 adjuster->tooMany(max_features_, (int)keypoints.size());
98 FastAdjuster::FastAdjuster( int init_thresh, bool nonmax, int min_thresh, int max_thresh ) :
99 thresh_(init_thresh), nonmax_(nonmax), init_thresh_(init_thresh),
100 min_thresh_(min_thresh), max_thresh_(max_thresh)
103 void FastAdjuster::detectImpl(InputArray image, std::vector<KeyPoint>& keypoints, InputArray mask) const
105 FastFeatureDetector(thresh_, nonmax_).detect(image, keypoints, mask);
108 void FastAdjuster::tooFew(int, int)
110 //fast is easy to adjust
114 void FastAdjuster::tooMany(int, int)
116 //fast is easy to adjust
120 //return whether or not the threshhold is beyond
122 bool FastAdjuster::good() const
124 return (thresh_ > min_thresh_) && (thresh_ < max_thresh_);
127 Ptr<AdjusterAdapter> FastAdjuster::clone() const
129 Ptr<AdjusterAdapter> cloned_obj(new FastAdjuster( init_thresh_, nonmax_, min_thresh_, max_thresh_ ));
133 StarAdjuster::StarAdjuster(double initial_thresh, double min_thresh, double max_thresh) :
134 thresh_(initial_thresh), init_thresh_(initial_thresh),
135 min_thresh_(min_thresh), max_thresh_(max_thresh)
138 void StarAdjuster::detectImpl(InputArray image, std::vector<KeyPoint>& keypoints, InputArray mask) const
140 StarFeatureDetector detector_tmp(16, cvRound(thresh_), 10, 8, 3);
141 detector_tmp.detect(image, keypoints, mask);
144 void StarAdjuster::tooFew(int, int)
151 void StarAdjuster::tooMany(int, int)
156 bool StarAdjuster::good() const
158 return (thresh_ > min_thresh_) && (thresh_ < max_thresh_);
161 Ptr<AdjusterAdapter> StarAdjuster::clone() const
163 Ptr<AdjusterAdapter> cloned_obj(new StarAdjuster( init_thresh_, min_thresh_, max_thresh_ ));
167 SurfAdjuster::SurfAdjuster( double initial_thresh, double min_thresh, double max_thresh ) :
168 thresh_(initial_thresh), init_thresh_(initial_thresh),
169 min_thresh_(min_thresh), max_thresh_(max_thresh)
172 void SurfAdjuster::detectImpl(InputArray image, std::vector<KeyPoint>& keypoints, InputArray mask) const
174 Ptr<FeatureDetector> surf = FeatureDetector::create("SURF");
175 surf->set("hessianThreshold", thresh_);
176 surf->detect(image, keypoints, mask);
179 void SurfAdjuster::tooFew(int, int)
186 void SurfAdjuster::tooMany(int, int)
191 //return whether or not the threshhold is beyond
193 bool SurfAdjuster::good() const
195 return (thresh_ > min_thresh_) && (thresh_ < max_thresh_);
198 Ptr<AdjusterAdapter> SurfAdjuster::clone() const
200 Ptr<AdjusterAdapter> cloned_obj(new SurfAdjuster( init_thresh_, min_thresh_, max_thresh_ ));
204 Ptr<AdjusterAdapter> AdjusterAdapter::create( const String& detectorType )
206 Ptr<AdjusterAdapter> adapter;
208 if( !detectorType.compare( "FAST" ) )
210 adapter = makePtr<FastAdjuster>();
212 else if( !detectorType.compare( "STAR" ) )
214 adapter = makePtr<StarAdjuster>();
216 else if( !detectorType.compare( "SURF" ) )
218 adapter = makePtr<SurfAdjuster>();