bc97fc167740a5fbcb7c5f3ea30383276b38f294
[platform/upstream/opencv.git] / modules / features2d / src / gftt.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 namespace cv
45 {
46
47 class GFTTDetector_Impl CV_FINAL : public GFTTDetector
48 {
49 public:
50     GFTTDetector_Impl( int _nfeatures, double _qualityLevel,
51                       double _minDistance, int _blockSize, int _gradientSize,
52                       bool _useHarrisDetector, double _k )
53         : nfeatures(_nfeatures), qualityLevel(_qualityLevel), minDistance(_minDistance),
54         blockSize(_blockSize), gradSize(_gradientSize), useHarrisDetector(_useHarrisDetector), k(_k)
55     {
56     }
57
58     void setMaxFeatures(int maxFeatures) CV_OVERRIDE { nfeatures = maxFeatures; }
59     int getMaxFeatures() const CV_OVERRIDE { return nfeatures; }
60
61     void setQualityLevel(double qlevel) CV_OVERRIDE { qualityLevel = qlevel; }
62     double getQualityLevel() const CV_OVERRIDE { return qualityLevel; }
63
64     void setMinDistance(double minDistance_) CV_OVERRIDE { minDistance = minDistance_; }
65     double getMinDistance() const CV_OVERRIDE { return minDistance; }
66
67     void setBlockSize(int blockSize_) CV_OVERRIDE { blockSize = blockSize_; }
68     int getBlockSize() const CV_OVERRIDE { return blockSize; }
69
70     //void setGradientSize(int gradientSize_) { gradSize = gradientSize_; }
71     //int getGradientSize() { return gradSize; }
72
73     void setHarrisDetector(bool val) CV_OVERRIDE { useHarrisDetector = val; }
74     bool getHarrisDetector() const CV_OVERRIDE { return useHarrisDetector; }
75
76     void setK(double k_) CV_OVERRIDE { k = k_; }
77     double getK() const CV_OVERRIDE { return k; }
78
79     void detect( InputArray _image, std::vector<KeyPoint>& keypoints, InputArray _mask ) CV_OVERRIDE
80     {
81         CV_INSTRUMENT_REGION();
82
83         if(_image.empty())
84         {
85             keypoints.clear();
86             return;
87         }
88
89         std::vector<Point2f> corners;
90         std::vector<float> cornersQuality;
91
92         if (_image.isUMat())
93         {
94             UMat ugrayImage;
95             if( _image.type() != CV_8U )
96                 cvtColor( _image, ugrayImage, COLOR_BGR2GRAY );
97             else
98                 ugrayImage = _image.getUMat();
99
100             goodFeaturesToTrack( ugrayImage, corners, nfeatures, qualityLevel, minDistance, _mask,
101                                  cornersQuality, blockSize, gradSize, useHarrisDetector, k );
102         }
103         else
104         {
105             Mat image = _image.getMat(), grayImage = image;
106             if( image.type() != CV_8U )
107                 cvtColor( image, grayImage, COLOR_BGR2GRAY );
108
109             goodFeaturesToTrack( grayImage, corners, nfeatures, qualityLevel, minDistance, _mask,
110                                  cornersQuality, blockSize, gradSize, useHarrisDetector, k );
111         }
112
113         CV_Assert(corners.size() == cornersQuality.size());
114
115         keypoints.resize(corners.size());
116         for (size_t i = 0; i < corners.size(); i++)
117             keypoints[i] = KeyPoint(corners[i], (float)blockSize, -1, cornersQuality[i]);
118
119     }
120
121     int nfeatures;
122     double qualityLevel;
123     double minDistance;
124     int blockSize;
125     int gradSize;
126     bool useHarrisDetector;
127     double k;
128 };
129
130
131 Ptr<GFTTDetector> GFTTDetector::create( int _nfeatures, double _qualityLevel,
132                          double _minDistance, int _blockSize, int _gradientSize,
133                          bool _useHarrisDetector, double _k )
134 {
135     return makePtr<GFTTDetector_Impl>(_nfeatures, _qualityLevel,
136                                       _minDistance, _blockSize, _gradientSize, _useHarrisDetector, _k);
137 }
138
139 Ptr<GFTTDetector> GFTTDetector::create( int _nfeatures, double _qualityLevel,
140                          double _minDistance, int _blockSize,
141                          bool _useHarrisDetector, double _k )
142 {
143     return makePtr<GFTTDetector_Impl>(_nfeatures, _qualityLevel,
144                                       _minDistance, _blockSize, 3, _useHarrisDetector, _k);
145 }
146
147 String GFTTDetector::getDefaultName() const
148 {
149     return (Feature2D::getDefaultName() + ".GFTTDetector");
150 }
151
152 }