OpenCV 4.5.3(日本語機械翻訳)
feature.hpp
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) 2013, OpenCV Foundation, 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 the copyright holders 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 #ifndef __OPENCV_FEATURE_HPP__
43 #define __OPENCV_FEATURE_HPP__
44
45 #include "opencv2/core.hpp"
46 #include "opencv2/imgproc.hpp"
47 #include <iostream>
48 #include <string>
49 #include <time.h>
50
51 /*
52 * TODO This implementation is based on apps/traincascade/
53 * TODO Changed CvHaarEvaluator based on ADABOOSTING implementation (Grabner et al.)
54 */
55
56 namespace cv {
57 namespace detail {
58 inline namespace tracking {
59
62
63 inline namespace contrib_feature {
64
65 #define FEATURES "features"
66
67 #define CC_FEATURES FEATURES
68 #define CC_FEATURE_PARAMS "featureParams"
69 #define CC_MAX_CAT_COUNT "maxCatCount"
70 #define CC_FEATURE_SIZE "featSize"
71 #define CC_NUM_FEATURES "numFeat"
72 #define CC_ISINTEGRAL "isIntegral"
73 #define CC_RECTS "rects"
74 #define CC_TILTED "tilted"
75 #define CC_RECT "rect"
76
77 #define LBPF_NAME "lbpFeatureParams"
78 #define HOGF_NAME "HOGFeatureParams"
79 #define HFP_NAME "haarFeatureParams"
80
81 #define CV_HAAR_FEATURE_MAX 3
82 #define N_BINS 9
83 #define N_CELLS 4
84
85 #define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \
86 /* (x, y) */ \
87 (p0) = (rect).x + (step) * (rect).y; \
88 /* (x + w, y) */ \
89 (p1) = (rect).x + (rect).width + (step) * (rect).y; \
90 /* (x + w, y) */ \
91 (p2) = (rect).x + (step) * ((rect).y + (rect).height); \
92 /* (x + w, y + h) */ \
93 (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);
94
95 #define CV_TILTED_OFFSETS( p0, p1, p2, p3, rect, step ) \
96 /* (x, y) */ \
97 (p0) = (rect).x + (step) * (rect).y; \
98 /* (x - h, y + h) */ \
99 (p1) = (rect).x - (rect).height + (step) * ((rect).y + (rect).height);\
100 /* (x + w, y + w) */ \
101 (p2) = (rect).x + (rect).width + (step) * ((rect).y + (rect).width); \
102 /* (x + w - h, y + w + h) */ \
103 (p3) = (rect).x + (rect).width - (rect).height \
104 + (step) * ((rect).y + (rect).width + (rect).height);
105
106 float calcNormFactor( const Mat& sum, const Mat& sqSum );
107
108 template<class Feature>
109 void _writeFeatures( const std::vector<Feature> features, FileStorage &fs, const Mat& featureMap )
110{
111 fs << FEATURES << "[";
112 const Mat_<int>& featureMap_ = (const Mat_<int>&) featureMap;
113 for ( int fi = 0; fi < featureMap.cols; fi++ )
114 if( featureMap_( 0, fi ) >= 0 )
115 {
116 fs << "{";
117 features[fi].write( fs );
118 fs << "}";
119 }
120 fs << "]";
121}
122
123 class CvParams
124{
125 public:
126 CvParams();
127 virtual ~CvParams()
128 {
129 }
130 // from|to file
131 virtual void write( FileStorage &fs ) const = 0;
132 virtual bool read( const FileNode &node ) = 0;
133 // from|to screen
134 virtual void printDefaults() const;
135 virtual void printAttrs() const;
136 virtual bool scanAttr( const std::string prmName, const std::string val );
137 std::string name;
138};
139
140 class CvFeatureParams : public CvParams
141{
142 public:
143 enum FeatureType
144 {
145 HAAR = 0,
146 LBP = 1,
147 HOG = 2
148 };
149
151 virtual void init( const CvFeatureParams& fp );
152 virtual void write( FileStorage &fs ) const CV_OVERRIDE;
153 virtual bool read( const FileNode &node ) CV_OVERRIDE;
154 static Ptr<CvFeatureParams> create(CvFeatureParams::FeatureType featureType);
155 int maxCatCount; // 0 in case of numerical features
156 int featSize; // 1 in case of simple features (HAAR, LBP) and N_BINS(9)*N_CELLS(4) in case of Dalal's HOG features
157 int numFeatures;
158};
159
161{
162 public:
163 virtual ~CvFeatureEvaluator()
164 {
165 }
166 virtual void init( const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize );
167 virtual void setImage( const Mat& img, uchar clsLabel, int idx );
168 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const = 0;
169 virtual float operator()( int featureIdx, int sampleIdx ) = 0;
170 static Ptr<CvFeatureEvaluator> create(CvFeatureParams::FeatureType type);
171
172 int getNumFeatures() const
173 {
174 return numFeatures;
175 }
176 int getMaxCatCount() const
177 {
178 return featureParams->maxCatCount;
179 }
180 int getFeatureSize() const
181 {
182 return featureParams->featSize;
183 }
184 const Mat& getCls() const
185 {
186 return cls;
187 }
188 float getCls( int si ) const
189 {
190 return cls.at<float>( si, 0 );
191 }
192 protected:
193 virtual void generateFeatures() = 0;
194
195 int npos, nneg;
196 int numFeatures;
197 Size winSize;
198 CvFeatureParams *featureParams;
199 Mat cls;
200};
201
203{
204 public:
205
207
208 virtual void init( const CvFeatureParams& fp ) CV_OVERRIDE;
209 virtual void write( FileStorage &fs ) const CV_OVERRIDE;
210 virtual bool read( const FileNode &node ) CV_OVERRIDE;
211
212 virtual void printDefaults() const CV_OVERRIDE;
213 virtual void printAttrs() const CV_OVERRIDE;
214 virtual bool scanAttr( const std::string prm, const std::string val ) CV_OVERRIDE;
215
216 bool isIntegral;
217};
218
220{
221 public:
222
224 {
225
226 public:
227
228 FeatureHaar( Size patchSize );
229 bool eval( const Mat& image, Rect ROI, float* result ) const;
230 int getNumAreas();
231 const std::vector<float>& getWeights() const;
232 const std::vector<Rect>& getAreas() const;
233 void write( FileStorage ) const
234 {
235 }
236 ;
237 float getInitMean() const;
238 float getInitSigma() const;
239
240 private:
241 int m_type;
242 int m_numAreas;
243 std::vector<float> m_weights;
244 float m_initMean;
245 float m_initSigma;
246 void generateRandomFeature( Size imageSize );
247 float getSum( const Mat& image, Rect imgROI ) const;
248 std::vector<Rect> m_areas; // areas within the patch over which to compute the feature
249 cv::Size m_initSize; // size of the patch used during training
250 cv::Size m_curSize; // size of the patches currently under investigation
251 float m_scaleFactorHeight; // scaling factor in vertical direction
252 float m_scaleFactorWidth; // scaling factor in horizontal direction
253 std::vector<Rect> m_scaleAreas; // areas after scaling
254 std::vector<float> m_scaleWeights; // weights after scaling
255
256 };
257
258 virtual void init( const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize ) CV_OVERRIDE;
259 virtual void setImage( const Mat& img, uchar clsLabel = 0, int idx = 1 ) CV_OVERRIDE;
260 virtual float operator()( int featureIdx, int sampleIdx ) CV_OVERRIDE;
261 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const CV_OVERRIDE;
262 void writeFeature( FileStorage &fs ) const; // for old file format
263 const std::vector<CvHaarEvaluator::FeatureHaar>& getFeatures() const;
264 inline CvHaarEvaluator::FeatureHaar& getFeatures( int idx )
265 {
266 return features[idx];
267 }
268 void setWinSize( Size patchSize );
269 Size setWinSize() const;
270 virtual void generateFeatures() CV_OVERRIDE;
271
278 virtual void generateFeatures( int numFeatures );
279
280 protected:
281 bool isIntegral;
282
283 /* TODO Added from MIL implementation */
284 Mat _ii_img;
285 void compute_integral( const cv::Mat & img, std::vector<cv::Mat_<float> > & ii_imgs )
286 {
287 Mat ii_img;
288 integral( img, ii_img, CV_32F );
289 split( ii_img, ii_imgs );
290 }
291
292 std::vector<FeatureHaar> features;
293 Mat sum; /* sum images (each row represents image) */
294};
295
297{
299};
300
302{
303 public:
304 virtual ~CvHOGEvaluator()
305 {
306 }
307 virtual void init( const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize ) CV_OVERRIDE;
308 virtual void setImage( const Mat& img, uchar clsLabel, int idx ) CV_OVERRIDE;
309 virtual float operator()( int varIdx, int sampleIdx ) CV_OVERRIDE;
310 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const CV_OVERRIDE;
311 protected:
312 virtual void generateFeatures() CV_OVERRIDE;
313 virtual void integralHistogram( const Mat &img, std::vector<Mat> &histogram, Mat &norm, int nbins ) const;
314 class Feature
315 {
316 public:
317 Feature();
318 Feature( int offset, int x, int y, int cellW, int cellH );
319 float calc( const std::vector<Mat> &_hists, const Mat &_normSum, size_t y, int featComponent ) const;
320 void write( FileStorage &fs ) const;
321 void write( FileStorage &fs, int varIdx ) const;
322
323 Rect rect[N_CELLS]; //cells
324
325 struct
326 {
327 int p0, p1, p2, p3;
328 } fastRect[N_CELLS];
329 };
330 std::vector<Feature> features;
331
332 Mat normSum; //for nomalization calculation (L1 or L2)
333 std::vector<Mat> hist;
334};
335
336 inline float CvHOGEvaluator::operator()( int varIdx, int sampleIdx )
337{
338 int featureIdx = varIdx / ( N_BINS * N_CELLS );
339 int componentIdx = varIdx % ( N_BINS * N_CELLS );
340 //return features[featureIdx].calc( hist, sampleIdx, componentIdx);
341 return features[featureIdx].calc( hist, normSum, sampleIdx, componentIdx );
342}
343
344 inline float CvHOGEvaluator::Feature::calc( const std::vector<Mat>& _hists, const Mat& _normSum, size_t y, int featComponent ) const
345 {
346 float normFactor;
347 float res;
348
349 int binIdx = featComponent % N_BINS;
350 int cellIdx = featComponent / N_BINS;
351
352 const float *phist = _hists[binIdx].ptr<float>( (int) y );
353 res = phist[fastRect[cellIdx].p0] - phist[fastRect[cellIdx].p1] - phist[fastRect[cellIdx].p2] + phist[fastRect[cellIdx].p3];
354
355 const float *pnormSum = _normSum.ptr<float>( (int) y );
356 normFactor = (float) ( pnormSum[fastRect[0].p0] - pnormSum[fastRect[1].p1] - pnormSum[fastRect[2].p2] + pnormSum[fastRect[3].p3] );
357 res = ( res > 0.001f ) ? ( res / ( normFactor + 0.001f ) ) : 0.f; //for cutting negative values, which apper due to floating precision
358
359 return res;
360}
361
363{
365
366};
367
369{
370 public:
371 virtual ~CvLBPEvaluator() CV_OVERRIDE
372 {
373 }
374 virtual void init( const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize ) CV_OVERRIDE;
375 virtual void setImage( const Mat& img, uchar clsLabel, int idx ) CV_OVERRIDE;
376 virtual float operator()( int featureIdx, int sampleIdx ) CV_OVERRIDE
377 {
378 return (float) features[featureIdx].calc( sum, sampleIdx );
379 }
380 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const CV_OVERRIDE;
381 protected:
382 virtual void generateFeatures() CV_OVERRIDE;
383
384 class Feature
385 {
386 public:
387 Feature();
388 Feature( int offset, int x, int y, int _block_w, int _block_h );
389 uchar calc( const Mat& _sum, size_t y ) const;
390 void write( FileStorage &fs ) const;
391
392 Rect rect;
393 int p[16];
394 };
395 std::vector<Feature> features;
396
397 Mat sum;
398};
399
400 inline uchar CvLBPEvaluator::Feature::calc( const Mat &_sum, size_t y ) const
401 {
402 const int* psum = _sum.ptr<int>( (int) y );
403 int cval = psum[p[5]] - psum[p[6]] - psum[p[9]] + psum[p[10]];
404
405 return (uchar) ( ( psum[p[0]] - psum[p[1]] - psum[p[4]] + psum[p[5]] >= cval ? 128 : 0 ) | // 0
406 ( psum[p[1]] - psum[p[2]] - psum[p[5]] + psum[p[6]] >= cval ? 64 : 0 ) | // 1
407 ( psum[p[2]] - psum[p[3]] - psum[p[6]] + psum[p[7]] >= cval ? 32 : 0 ) | // 2
408 ( psum[p[6]] - psum[p[7]] - psum[p[10]] + psum[p[11]] >= cval ? 16 : 0 ) | // 5
409 ( psum[p[10]] - psum[p[11]] - psum[p[14]] + psum[p[15]] >= cval ? 8 : 0 ) | // 8
410 ( psum[p[9]] - psum[p[10]] - psum[p[13]] + psum[p[14]] >= cval ? 4 : 0 ) | // 7
411 ( psum[p[8]] - psum[p[9]] - psum[p[12]] + psum[p[13]] >= cval ? 2 : 0 ) | // 6
412 ( psum[p[4]] - psum[p[5]] - psum[p[8]] + psum[p[9]] >= cval ? 1 : 0 ) ); // 3
413}
414
415} // namespace
416
418
419}}} // namespace cv
420
421 #endif
File Storage Node class.
Definition: persistence.hpp:482
XML/YAML/JSON file storage class that encapsulates all the information necessary for writing or readi...
Definition: persistence.hpp:304
Template matrix class derived from Mat
Definition: mat.hpp:2199
n-dimensional dense array class
Definition: mat.hpp:802
uchar * ptr(int i0=0)
Returns a pointer to the specified matrix row.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
Template class for 2D rectangles
Definition: core/types.hpp:421
Template class for specifying the size of an image or rectangle.
Definition: core/types.hpp:316
Definition: feature.hpp:161
Definition: feature.hpp:141
Definition: feature.hpp:302
Definition: feature.hpp:220
Definition: feature.hpp:203
Definition: feature.hpp:369
Definition: feature.hpp:124
CV_EXPORTS void split(const Mat &src, Mat *mvbegin)
Divides a multi-channel array into several single-channel arrays.
CV_EXPORTS_W double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray())
Calculates the absolute norm of an array.
CV_EXPORTS_W void integral(InputArray src, OutputArray sum, int sdepth=-1)
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: cvstd_wrapper.hpp:74
Definition: feature.hpp:297
Definition: feature.hpp:363