OpenCV 4.5.3(日本語機械翻訳)
saliencySpecializedClasses.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) 2014, 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_SALIENCY_SPECIALIZED_CLASSES_HPP__
43 #define __OPENCV_SALIENCY_SPECIALIZED_CLASSES_HPP__
44
45 #include <cstdio>
46 #include <string>
47 #include <iostream>
48 #include <stdint.h>
49 #include "saliencyBaseClasses.hpp"
50 #include "opencv2/core.hpp"
51
52 namespace cv
53{
54 namespace saliency
55{
56
59
60 /************************************ Specific Static Saliency Specialized Classes ************************************/
61
70{
71 public:
72
75
76 CV_WRAP static Ptr<StaticSaliencySpectralResidual> create()
77 {
78 return makePtr<StaticSaliencySpectralResidual>();
79 }
80
81 CV_WRAP bool computeSaliency( InputArray image, OutputArray saliencyMap )
82 {
83 if( image.empty() )
84 return false;
85
86 return computeSaliencyImpl( image, saliencyMap );
87 }
88
89 CV_WRAP void read( const FileNode& fn ) CV_OVERRIDE;
90 void write( FileStorage& fs ) const CV_OVERRIDE;
91
92 CV_WRAP int getImageWidth() const
93 {
94 return resImWidth;
95 }
96 CV_WRAP inline void setImageWidth(int val)
97 {
98 resImWidth = val;
99 }
100 CV_WRAP int getImageHeight() const
101 {
102 return resImHeight;
103 }
104 CV_WRAP void setImageHeight(int val)
105 {
106 resImHeight = val;
107 }
108
109 protected:
110 bool computeSaliencyImpl( InputArray image, OutputArray saliencyMap ) CV_OVERRIDE;
111 CV_PROP_RW int resImWidth;
112 CV_PROP_RW int resImHeight;
113
114};
115
116
122 class CV_EXPORTS_W StaticSaliencyFineGrained : public StaticSaliency
123{
124 public:
125
127
128 CV_WRAP static Ptr<StaticSaliencyFineGrained> create()
129 {
130 return makePtr<StaticSaliencyFineGrained>();
131 }
132
133 CV_WRAP bool computeSaliency( InputArray image, OutputArray saliencyMap )
134 {
135 if( image.empty() )
136 return false;
137
138 return computeSaliencyImpl( image, saliencyMap );
139 }
141
142 protected:
143 bool computeSaliencyImpl( InputArray image, OutputArray saliencyMap ) CV_OVERRIDE;
144
145 private:
146 void calcIntensityChannel(Mat src, Mat dst);
147 void copyImage(Mat src, Mat dst);
148 void getIntensityScaled(Mat integralImage, Mat gray, Mat saliencyOn, Mat saliencyOff, int neighborhood);
149 float getMean(Mat srcArg, Point2i PixArg, int neighbourhood, int centerVal);
150 void mixScales(Mat *saliencyOn, Mat intensityOn, Mat *saliencyOff, Mat intensityOff, const int numScales);
151 void mixOnOff(Mat intensityOn, Mat intensityOff, Mat intensity);
152 void getIntensity(Mat srcArg, Mat dstArg, Mat dstOnArg, Mat dstOffArg, bool generateOnOff);
153};
154
155
156
157
158 /************************************ Specific Motion Saliency Specialized Classes ************************************/
159
169 class CV_EXPORTS_W MotionSaliencyBinWangApr2014 : public MotionSaliency
170{
171 public:
174
175 CV_WRAP static Ptr<MotionSaliencyBinWangApr2014> create()
176 {
177 return makePtr<MotionSaliencyBinWangApr2014>();
178 }
179
180 CV_WRAP bool computeSaliency( InputArray image, OutputArray saliencyMap )
181 {
182 if( image.empty() )
183 return false;
184
185 return computeSaliencyImpl( image, saliencyMap );
186 }
187
193 CV_WRAP void setImagesize( int W, int H );
197 CV_WRAP bool init();
198
199 CV_WRAP int getImageWidth() const
200 {
201 return imageWidth;
202 }
203 CV_WRAP inline void setImageWidth(int val)
204 {
205 imageWidth = val;
206 }
207 CV_WRAP int getImageHeight() const
208 {
209 return imageHeight;
210 }
211 CV_WRAP void setImageHeight(int val)
212 {
213 imageHeight = val;
214 }
215
216 protected:
225 bool computeSaliencyImpl( InputArray image, OutputArray saliencyMap ) CV_OVERRIDE;
226
227 private:
228
229 // classification (and adaptation) functions
230 bool fullResolutionDetection( const Mat& image, Mat& highResBFMask );
231 bool lowResolutionDetection( const Mat& image, Mat& lowResBFMask );
232
233 // Background model maintenance functions
234 bool templateOrdering();
235 bool templateReplacement( const Mat& finalBFMask, const Mat& image );
236
237 // Decision threshold adaptation and Activity control function
238 bool activityControl(const Mat& current_noisePixelsMask);
239 bool decisionThresholdAdaptation();
240
241 // changing structure
242 std::vector<Ptr<Mat> > backgroundModel;// The vector represents the background template T0---TK of reference paper.
243 // Matrices are two-channel matrix. In the first layer there are the B (background value)
244 // for each pixel. In the second layer, there are the C (efficacy) value for each pixel
245 Mat potentialBackground;// Two channel Matrix. For each pixel, in the first level there are the Ba value (potential background value)
246 // and in the secon level there are the Ca value, the counter for each potential value.
247 Mat epslonPixelsValue;// epslon threshold
248
249 Mat activityPixelsValue;// Activity level of each pixel
250
251 //vector<Mat> noisePixelMask; // We define a ‘noise-pixel’ as a pixel that has been classified as a foreground pixel during the full resolution
252 Mat noisePixelMask;// We define a ‘noise-pixel’ as a pixel that has been classified as a foreground pixel during the full resolution
253 //detection process,however, after the low resolution detection, it has become a
254 // background pixel. The matrix is two-channel matrix. In the first layer there is the mask ( the identified noise-pixels are set to 1 while other pixels are 0)
255 // for each pixel. In the second layer, there is the value of activity level A for each pixel.
256
257 //fixed parameter
258 bool activityControlFlag;
259 bool neighborhoodCheck;
260 int N_DS;// Number of template to be downsampled and used in lowResolutionDetection function
261 CV_PROP_RW int imageWidth;// Width of input image
262 CV_PROP_RW int imageHeight;//Height of input image
263 int K;// Number of background model template
264 int N;// NxN is the size of the block for downsampling in the lowlowResolutionDetection
265 float alpha;// Learning rate
266 int L0, L1;// Upper-bound values for C0 and C1 (efficacy of the first two template (matrices) of backgroundModel
267 int thetaL;// T0, T1 swap threshold
268 int thetaA;// Potential background value threshold
269 int gamma;// Parameter that controls the time that the newly updated long-term background value will remain in the
270 // long-term template, regardless of any subsequent background changes. A relatively large (eg gamma=3) will
271 //restrain the generation of ghosts.
272
273 uchar Ainc;// Activity Incrementation;
274 int Bmax;// Upper-bound value for pixel activity
275 int Bth;// Max activity threshold
276 int Binc, Bdec;// Threshold for pixel-level decision threshold (epslon) adaptation
277 float deltaINC, deltaDEC;// Increment-decrement value for epslon adaptation
278 int epslonMIN, epslonMAX;// Range values for epslon threshold
279
280};
281
282 /************************************ Specific Objectness Specialized Classes ************************************/
283
291 class CV_EXPORTS_W ObjectnessBING : public Objectness
292{
293 public:
294
296 virtual ~ObjectnessBING();
297
298 CV_WRAP static Ptr<ObjectnessBING> create()
299 {
300 return makePtr<ObjectnessBING>();
301 }
302
303 CV_WRAP bool computeSaliency( InputArray image, OutputArray saliencyMap )
304 {
305 if( image.empty() )
306 return false;
307
308 return computeSaliencyImpl( image, saliencyMap );
309 }
310
311 CV_WRAP void read();
312 CV_WRAP void write() const;
313
320 CV_WRAP std::vector<float> getobjectnessValues();
321
326 CV_WRAP void setTrainingPath( const String& trainingPath );
327
335 CV_WRAP void setBBResDir( const String& resultsDir );
336
337 CV_WRAP double getBase() const
338 {
339 return _base;
340 }
341 CV_WRAP inline void setBase(double val)
342 {
343 _base = val;
344 }
345 CV_WRAP int getNSS() const
346 {
347 return _NSS;
348 }
349 CV_WRAP void setNSS(int val)
350 {
351 _NSS = val;
352 }
353 CV_WRAP int getW() const
354 {
355 return _W;
356 }
357 CV_WRAP void setW(int val)
358 {
359 _W = val;
360 }
361
362 protected:
372 bool computeSaliencyImpl( InputArray image, OutputArray objectnessBoundingBox ) CV_OVERRIDE;
373
374 private:
375
376 class FilterTIG
377 {
378 public:
379 void update( Mat &w );
380
381 // For a W by H gradient magnitude map, find a W-7 by H-7 CV_32F matching score map
382 Mat matchTemplate( const Mat &mag1u );
383
384 float dot( int64_t tig1, int64_t tig2, int64_t tig4, int64_t tig8 );
385 void reconstruct( Mat &w );// For illustration purpose
386
387 private:
388 static const int NUM_COMP = 2;// Number of components
389 static const int D = 64;// Dimension of TIG
390 int64_t _bTIGs[NUM_COMP];// Binary TIG features
391 float _coeffs1[NUM_COMP];// Coefficients of binary TIG features
392
393 // For efficiently deals with different bits in CV_8U gradient map
394 float _coeffs2[NUM_COMP], _coeffs4[NUM_COMP], _coeffs8[NUM_COMP];
395 };
396
397 template<typename VT, typename ST>
398 struct ValStructVec
399 {
400 ValStructVec();
401 int size() const;
402 void clear();
403 void reserve( int resSz );
404 void pushBack( const VT& val, const ST& structVal );
405 const VT& operator ()( int i ) const;
406 const ST& operator []( int i ) const;
407 VT& operator ()( int i );
408 ST& operator []( int i );
409
410 void sort( bool descendOrder = true );
411 const std::vector<ST> &getSortedStructVal();
412 std::vector<std::pair<VT, int> > getvalIdxes();
413 void append( const ValStructVec<VT, ST> &newVals, int startV = 0 );
414
415 std::vector<ST> structVals; // struct values
416 int sz;// size of the value struct vector
417 std::vector<std::pair<VT, int> > valIdxes;// Indexes after sort
418 bool smaller()
419 {
420 return true;
421 }
422 std::vector<ST> sortedStructVals;
423 };
424
425 enum
426 {
427 MAXBGR,
428 HSV,
429 G
430 };
431
432 double _base, _logBase; // base for window size quantization
433 int _W;// As described in the paper: #Size, Size(_W, _H) of feature window.
434 int _NSS;// Size for non-maximal suppress
435 int _maxT, _minT, _numT;// The minimal and maximal dimensions of the template
436
437 int _Clr;//
438 static const char* _clrName[3];
439
440 // Names and paths to read model and to store results
441 std::string _modelName, _bbResDir, _trainingPath, _resultsDir;
442
443 std::vector<int> _svmSzIdxs;// Indexes of active size. It's equal to _svmFilters.size() and _svmReW1f.rows
444 Mat _svmFilter;// Filters learned at stage I, each is a _H by _W CV_32F matrix
445 FilterTIG _tigF;// TIG filter
446 Mat _svmReW1f;// Re-weight parameters learned at stage II.
447
448 // List of the rectangles' objectness value, in the same order as
449 // the vector<Vec4i> objectnessBoundingBox returned by the algorithm (in computeSaliencyImpl function)
450 std::vector<float> objectnessValues;
451
452 private:
453 // functions
454
455 inline static float LoG( float x, float y, float delta )
456 {
457 float d = - ( x * x + y * y ) / ( 2 * delta * delta );
458 return -1.0f / ( (float) ( CV_PI ) * pow( delta, 4 ) ) * ( 1 + d ) * exp( d );
459 } // Laplacian of Gaussian
460
461 // Read matrix from binary file
462 static bool matRead( const std::string& filename, Mat& M );
463
464 void setColorSpace( int clr = MAXBGR );
465
466 // Load trained model.
467 int loadTrainedModel();// Return -1, 0, or 1 if partial, none, or all loaded
468
469 // Get potential bounding boxes, each of which is represented by a Vec4i for (minX, minY, maxX, maxY).
470 // The trained model should be prepared before calling this function: loadTrainedModel() or trainStageI() + trainStageII().
471 // Use numDet to control the final number of proposed bounding boxes, and number of per size (scale and aspect ratio)
472 void getObjBndBoxes( Mat &img3u, ValStructVec<float, Vec4i> &valBoxes, int numDetPerSize = 120 );
473 void getObjBndBoxesForSingleImage( Mat img, ValStructVec<float, Vec4i> &boxes, int numDetPerSize );
474
475 bool filtersLoaded()
476 {
477 int n = (int) _svmSzIdxs.size();
478 return n > 0 && _svmReW1f.size() == Size( 2, n ) && _svmFilter.size() == Size( _W, _W );
479 }
480 void predictBBoxSI( Mat &mag3u, ValStructVec<float, Vec4i> &valBoxes, std::vector<int> &sz, int NUM_WIN_PSZ = 100, bool fast = true );
481 void predictBBoxSII( ValStructVec<float, Vec4i> &valBoxes, const std::vector<int> &sz );
482
483 // Calculate the image gradient: center option as in VLFeat
484 void gradientMag( Mat &imgBGR3u, Mat &mag1u );
485
486 static void gradientRGB( Mat &bgr3u, Mat &mag1u );
487 static void gradientGray( Mat &bgr3u, Mat &mag1u );
488 static void gradientHSV( Mat &bgr3u, Mat &mag1u );
489 static void gradientXY( Mat &x1i, Mat &y1i, Mat &mag1u );
490
491 static inline int bgrMaxDist( const Vec3b &u, const Vec3b &v )
492 {
493 int b = abs( u[0] - v[0] ), g = abs( u[1] - v[1] ), r = abs( u[2] - v[2] );
494 b = max( b, g );
495 return max( b, r );
496 }
497 static inline int vecDist3b( const Vec3b &u, const Vec3b &v )
498 {
499 return abs( u[0] - v[0] ) + abs( u[1] - v[1] ) + abs( u[2] - v[2] );
500 }
501
502 //Non-maximal suppress
503 static void nonMaxSup( Mat &matchCost1f, ValStructVec<float, Point> &matchCost, int NSS = 1, int maxPoint = 50, bool fast = true );
504
505};
506
508
509}
510 /* namespace saliency */
511} /* namespace cv */
512
513 #endif
This type is very similar to InputArray except that it is used for input/output and output function p...
Definition: mat.hpp:295
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
n-dimensional dense array class
Definition: mat.hpp:802
the Fast Self-tuning Background Subtraction Algorithm from
Definition: saliencySpecializedClasses.hpp:170
bool computeSaliencyImpl(InputArray image, OutputArray saliencyMap) CV_OVERRIDE
Performs all the operations and calls all internal functions necessary for the accomplishment of the ...
Definition: saliencyBaseClasses.hpp:112
Objectness algorithms based on [3] [3] Cheng, Ming-Ming, et al. "BING: Binarized normed gradients for...
Definition: saliencySpecializedClasses.hpp:292
bool computeSaliencyImpl(InputArray image, OutputArray objectnessBoundingBox) CV_OVERRIDE
Performs all the operations and calls all internal functions necessary for the accomplishment of the ...
Definition: saliencyBaseClasses.hpp:121
the Fine Grained Saliency approach from
Definition: saliencySpecializedClasses.hpp:123
Definition: saliencyBaseClasses.hpp:86
the Spectral Residual approach from
Definition: saliencySpecializedClasses.hpp:70
void write(FileStorage &fs) const CV_OVERRIDE
Stores algorithm parameters in a file storage
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
CV_EXPORTS_W void exp(InputArray src, OutputArray dst)
Calculates the exponent of every array element.
CV_EXPORTS_W void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
CV_EXPORTS_W void sort(InputArray src, OutputArray dst, int flags)
Sorts each row or each column of a matrix.
softfloat abs(softfloat a)
Absolute value
Definition: softfloat.hpp:444
CV_EXPORTS_W void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())
Compares a template against overlapped image regions.
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: features2d.hpp:910
Definition: cvstd_wrapper.hpp:74