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 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42 
43 #include "precomp.hpp"
44 
45 namespace cv
46 {
47 
48 using std::vector;
49 
~Feature2D()50 Feature2D::~Feature2D() {}
51 
52 /*
53  * Detect keypoints in an image.
54  * image        The image.
55  * keypoints    The detected keypoints.
56  * mask         Mask specifying where to look for keypoints (optional). Must be a char
57  *              matrix with non-zero values in the region of interest.
58  */
detect(InputArray image,std::vector<KeyPoint> & keypoints,InputArray mask)59 void Feature2D::detect( InputArray image,
60                         std::vector<KeyPoint>& keypoints,
61                         InputArray mask )
62 {
63     CV_INSTRUMENT_REGION();
64 
65     if( image.empty() )
66     {
67         keypoints.clear();
68         return;
69     }
70     detectAndCompute(image, mask, keypoints, noArray(), false);
71 }
72 
73 
detect(InputArrayOfArrays images,std::vector<std::vector<KeyPoint>> & keypoints,InputArrayOfArrays masks)74 void Feature2D::detect( InputArrayOfArrays images,
75                         std::vector<std::vector<KeyPoint> >& keypoints,
76                         InputArrayOfArrays masks )
77 {
78     CV_INSTRUMENT_REGION();
79 
80     int nimages = (int)images.total();
81 
82     if (!masks.empty())
83     {
84         CV_Assert(masks.total() == (size_t)nimages);
85     }
86 
87     keypoints.resize(nimages);
88 
89     if (images.isMatVector())
90     {
91        for (int i = 0; i < nimages; i++)
92        {
93            detect(images.getMat(i), keypoints[i], masks.empty() ? noArray() : masks.getMat(i));
94        }
95     }
96     else
97     {
98         // assume UMats
99         for (int i = 0; i < nimages; i++)
100         {
101             detect(images.getUMat(i), keypoints[i], masks.empty() ? noArray() : masks.getUMat(i));
102         }
103     }
104 
105 
106 }
107 
108 /*
109  * Compute the descriptors for a set of keypoints in an image.
110  * image        The image.
111  * keypoints    The input keypoints. Keypoints for which a descriptor cannot be computed are removed.
112  * descriptors  Copmputed descriptors. Row i is the descriptor for keypoint i.
113  */
compute(InputArray image,std::vector<KeyPoint> & keypoints,OutputArray descriptors)114 void Feature2D::compute( InputArray image,
115                          std::vector<KeyPoint>& keypoints,
116                          OutputArray descriptors )
117 {
118     CV_INSTRUMENT_REGION();
119 
120     if( image.empty() )
121     {
122         descriptors.release();
123         return;
124     }
125     detectAndCompute(image, noArray(), keypoints, descriptors, true);
126 }
127 
compute(InputArrayOfArrays images,std::vector<std::vector<KeyPoint>> & keypoints,OutputArrayOfArrays descriptors)128 void Feature2D::compute( InputArrayOfArrays images,
129                          std::vector<std::vector<KeyPoint> >& keypoints,
130                          OutputArrayOfArrays descriptors )
131 {
132     CV_INSTRUMENT_REGION();
133 
134     if( !descriptors.needed() )
135         return;
136 
137     int nimages = (int)images.total();
138 
139     CV_Assert( keypoints.size() == (size_t)nimages );
140     // resize descriptors to appropriate size and compute
141     if (descriptors.isMatVector())
142     {
143         vector<Mat>& vec = *(vector<Mat>*)descriptors.getObj();
144         vec.resize(nimages);
145         for (int i = 0; i < nimages; i++)
146         {
147             compute(images.getMat(i), keypoints[i], vec[i]);
148         }
149     }
150     else if (descriptors.isUMatVector())
151     {
152         vector<UMat>& vec = *(vector<UMat>*)descriptors.getObj();
153         vec.resize(nimages);
154         for (int i = 0; i < nimages; i++)
155         {
156             compute(images.getUMat(i), keypoints[i], vec[i]);
157         }
158     }
159     else
160     {
161         CV_Error(Error::StsBadArg, "descriptors must be vector<Mat> or vector<UMat>");
162     }
163 }
164 
165 
166 /* Detects keypoints and computes the descriptors */
detectAndCompute(InputArray,InputArray,std::vector<KeyPoint> &,OutputArray,bool)167 void Feature2D::detectAndCompute( InputArray, InputArray,
168                                   std::vector<KeyPoint>&,
169                                   OutputArray,
170                                   bool )
171 {
172     CV_INSTRUMENT_REGION();
173 
174     CV_Error(Error::StsNotImplemented, "");
175 }
176 
write(const String & fileName) const177 void Feature2D::write( const String& fileName ) const
178 {
179     FileStorage fs(fileName, FileStorage::WRITE);
180     write(fs);
181 }
182 
read(const String & fileName)183 void Feature2D::read( const String& fileName )
184 {
185     FileStorage fs(fileName, FileStorage::READ);
186     read(fs.root());
187 }
188 
write(FileStorage &) const189 void Feature2D::write( FileStorage&) const
190 {
191 }
192 
read(const FileNode &)193 void Feature2D::read( const FileNode&)
194 {
195 }
196 
descriptorSize() const197 int Feature2D::descriptorSize() const
198 {
199     return 0;
200 }
201 
descriptorType() const202 int Feature2D::descriptorType() const
203 {
204     return CV_32F;
205 }
206 
defaultNorm() const207 int Feature2D::defaultNorm() const
208 {
209     int tp = descriptorType();
210     return tp == CV_8U ? NORM_HAMMING : NORM_L2;
211 }
212 
213 // Return true if detector object is empty
empty() const214 bool Feature2D::empty() const
215 {
216     return true;
217 }
218 
getDefaultName() const219 String Feature2D::getDefaultName() const
220 {
221     return "Feature2D";
222 }
223 
224 }
225