Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / objdetect / src / lsvmtbbversion.cpp
1 #include "precomp.hpp"
2
3 #ifdef HAVE_TBB
4 #include "_lsvm_tbbversion.h"
5
6 /*
7 // Task class
8 */
9 class ScoreComputation : public tbb::task
10 {
11 private:
12     const CvLSVMFilterObject **filters;
13     const int n;
14     const CvLSVMFeaturePyramid *H;
15     const float b;
16     const int maxXBorder;
17     const int maxYBorder;
18     const float scoreThreshold;
19     const int kLevels;
20     const int *procLevels;
21 public:
22     float **score;
23     CvPoint ***points;
24     CvPoint ****partsDisplacement;
25     int *kPoints;
26 public:
27     ScoreComputation(const CvLSVMFilterObject **_filters, int _n,
28                      const CvLSVMFeaturePyramid *_H,
29                      float _b, int _maxXBorder, int _maxYBorder,
30                      float _scoreThreshold, int _kLevels, const int *_procLevels,
31                      float **_score, CvPoint ***_points, int *_kPoints,
32                      CvPoint ****_partsDisplacement) :
33     n(_n), b(_b), maxXBorder(_maxXBorder),
34         maxYBorder(_maxYBorder), scoreThreshold(_scoreThreshold),
35         kLevels(_kLevels), score(_score), points(_points),
36         partsDisplacement(_partsDisplacement), kPoints(_kPoints)
37     {
38         filters = _filters;
39         H = _H;
40         procLevels = _procLevels;
41     };
42
43     task* execute()
44     {
45         int i, level, partsLevel, res;
46         for (i = 0; i < kLevels; i++)
47         {
48             level = procLevels[i];
49             partsLevel = level - LAMBDA;//H->lambda;
50             res = thresholdFunctionalScoreFixedLevel(
51                 filters, n, H, level, b,
52                 maxXBorder, maxYBorder, scoreThreshold, &(score[partsLevel]),
53                 points[partsLevel], &(kPoints[partsLevel]),
54                 partsDisplacement[partsLevel]);
55             if (res != LATENT_SVM_OK)
56             {
57                 continue;
58             }
59         }
60         return NULL;
61     }
62 };
63
64 /*
65 // Computation score function using TBB tasks
66 //
67 // API
68 // int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
69                                         const CvLSVMFeatureMap *H, const float b,
70                                         const int maxXBorder, const int maxYBorder,
71                                         const float scoreThreshold,
72                                         int *kLevels, int **procLevels,
73                                         const int threadsNum,
74                                         float **score, CvPoint ***points,
75                                         int *kPoints,
76                                         CvPoint ****partsDisplacement);
77 // INPUT
78 // filters           - the set of filters (the first element is root filter,
79                        the other - part filters)
80 // n                 - the number of part filters
81 // H                 - feature pyramid
82 // b                 - linear term of the score function
83 // maxXBorder        - the largest root filter size (X-direction)
84 // maxYBorder        - the largest root filter size (Y-direction)
85 // scoreThreshold    - score threshold
86 // kLevels           - array that contains number of levels processed
87                        by each thread
88 // procLevels        - array that contains lists of levels processed
89                        by each thread
90 // threadsNum        - the number of created threads
91 // OUTPUT
92 // score             - score function values that exceed threshold
93 // points            - the set of root filter positions (in the block space)
94 // kPoints           - number of root filter positions
95 // partsDisplacement - displacement of part filters (in the block space)
96 // RESULT
97 //
98 */
99 int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
100                                      const CvLSVMFeaturePyramid *H, const float b,
101                                      const int maxXBorder, const int maxYBorder,
102                                      const float scoreThreshold,
103                                      int *kLevels, int **procLevels,
104                                      const int threadsNum,
105                                      float **score, CvPoint ***points,
106                                      int *kPoints,
107                                      CvPoint ****partsDisplacement)
108 {
109     tbb::task_list tasks;
110     int i;
111     for (i = 0; i < threadsNum; i++)
112     {
113         ScoreComputation& sc =
114             *new(tbb::task::allocate_root()) ScoreComputation(filters, n, H, b,
115             maxXBorder, maxYBorder, scoreThreshold, kLevels[i], procLevels[i],
116             score, points, kPoints, partsDisplacement);
117         tasks.push_back(sc);
118     }
119     tbb::task::spawn_root_and_wait(tasks);
120     return LATENT_SVM_OK;
121 };
122 #endif