Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / softcascade / src / integral_channel_builder.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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2013, 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 {
46
47 using namespace cv::softcascade;
48
49 class HOG6MagLuv : public ChannelFeatureBuilder
50 {
51     enum {N_CHANNELS = 10};
52 public:
53     virtual ~HOG6MagLuv() {}
54     virtual cv::AlgorithmInfo* info() const;
55
56     virtual int totalChannels() const {return N_CHANNELS; }
57
58     virtual void operator()(cv::InputArray _frame, cv::OutputArray _integrals, cv::Size channelsSize) const
59     {
60         CV_Assert(_frame.type() == CV_8UC3);
61
62         cv::Mat frame      = _frame.getMat();
63         int h = frame.rows;
64         int w = frame.cols;
65
66         if (channelsSize != cv::Size())
67             _integrals.create(channelsSize.height * N_CHANNELS + 1, channelsSize.width + 1, CV_32SC1);
68
69         if(_integrals.empty())
70             _integrals.create(frame.rows * N_CHANNELS + 1, frame.cols + 1, CV_32SC1);
71
72         cv::Mat& integrals = _integrals.getMatRef();
73
74         cv::Mat channels, gray;
75
76         channels.create(h * N_CHANNELS, w, CV_8UC1);
77         channels.setTo(0);
78
79         cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
80
81         cv::Mat df_dx, df_dy, mag, angle;
82         cv::Sobel(gray, df_dx, CV_32F, 1, 0);
83         cv::Sobel(gray, df_dy, CV_32F, 0, 1);
84
85         cv::cartToPolar(df_dx, df_dy, mag, angle, true);
86         mag *= (1.f / (8 * sqrt(2.f)));
87
88         cv::Mat nmag = channels(cv::Rect(0, h * (N_CHANNELS - 4), w, h));
89         mag.convertTo(nmag, CV_8UC1);
90
91         angle *=  6 / 360.f;
92
93         for (int y = 0; y < h; ++y)
94         {
95             uchar* magnitude = nmag.ptr<uchar>(y);
96             float* ang = angle.ptr<float>(y);
97
98             for (int x = 0; x < w; ++x)
99             {
100                 channels.ptr<uchar>(y + (h * (int)ang[x]))[x] = magnitude[x];
101             }
102         }
103
104         cv::Mat luv, shrunk;
105         cv::cvtColor(frame, luv, cv::COLOR_BGR2Luv);
106
107         std::vector<cv::Mat> splited;
108         for (int i = 0; i < 3; ++i)
109             splited.push_back(channels(cv::Rect(0, h * (7 + i), w, h)));
110         split(luv, splited);
111         cv::resize(channels, shrunk, cv::Size(integrals.cols - 1, integrals.rows - 1), -1 , -1, cv::INTER_AREA);
112         cv::integral(shrunk, integrals, cv::noArray(), CV_32S);
113     }
114 };
115
116 }
117
118 using cv::softcascade::ChannelFeatureBuilder;
119 using cv::softcascade::ChannelFeature;
120
121 CV_INIT_ALGORITHM(HOG6MagLuv,  "ChannelFeatureBuilder.HOG6MagLuv", )
122
123 ChannelFeatureBuilder::~ChannelFeatureBuilder() {}
124
125 cv::Ptr<ChannelFeatureBuilder> ChannelFeatureBuilder::create(const cv::String& featureType)
126 {
127     return Algorithm::create<ChannelFeatureBuilder>("ChannelFeatureBuilder." + featureType);
128 }
129
130 ChannelFeature::ChannelFeature(int x, int y, int w, int h, int ch)
131 : bb(cv::Rect(x, y, w, h)), channel(ch) {}
132
133 bool ChannelFeature::operator ==(ChannelFeature b)
134 {
135     return bb == b.bb && channel == b.channel;
136 }
137
138 bool ChannelFeature::operator !=(ChannelFeature b)
139 {
140     return bb != b.bb || channel != b.channel;
141 }
142
143
144 float ChannelFeature::operator() (const cv::Mat& integrals, const cv::Size& model) const
145 {
146     int step = model.width + 1;
147
148     const int* ptr = integrals.ptr<int>(0) + (model.height * channel + bb.y) * step + bb.x;
149
150     int a = ptr[0];
151     int b = ptr[bb.width];
152
153     ptr += bb.height * step;
154
155     int c = ptr[bb.width];
156     int d = ptr[0];
157
158     return (float)(a - b + c - d);
159 }
160
161 void cv::softcascade::write(cv::FileStorage& fs, const cv::String&, const ChannelFeature& f)
162 {
163     fs << "{" << "channel" << f.channel << "rect" << f.bb << "}";
164 }
165
166 std::ostream& cv::softcascade::operator<<(std::ostream& out, const ChannelFeature& m)
167 {
168     return out << m.channel << " " << "[" << m.bb.width << " x " << m.bb.height << " from (" << m.bb.x << ", " << m.bb.y << ")]";
169 }
170
171 ChannelFeature::~ChannelFeature(){}
172
173 namespace {
174
175 using namespace cv::softcascade;
176
177 class ChannelFeaturePool : public FeaturePool
178 {
179 public:
180     ChannelFeaturePool(cv::Size m, int n, int ch) : FeaturePool(), model(m), N_CHANNELS(ch)
181     {
182         CV_Assert(m != cv::Size() && n > 0 && (ch == 10 || ch == 8));
183         fill(n);
184     }
185
186     virtual int size() const { return (int)pool.size(); }
187     virtual float apply(int fi, int si, const cv::Mat& integrals) const;
188     virtual void write( cv::FileStorage& fs, int index) const;
189
190     virtual ~ChannelFeaturePool() {}
191
192 private:
193
194     void fill(int desired);
195
196     cv::Size model;
197     std::vector<ChannelFeature> pool;
198     int N_CHANNELS;
199 };
200
201 float ChannelFeaturePool::apply(int fi, int si, const cv::Mat& integrals) const
202 {
203     return pool[fi](integrals.row(si), model);
204 }
205
206 void ChannelFeaturePool::write( cv::FileStorage& fs, int index) const
207 {
208
209     CV_Assert((index >= 0) && (index < (int)pool.size()));
210     fs << pool[index];
211 }
212
213 void ChannelFeaturePool::fill(int desired)
214 {
215     using namespace cv::softcascade::internal;
216     int mw = model.width;
217     int mh = model.height;
218
219     int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS;
220
221     int nfeatures = std::min(desired, maxPoolSize);
222     pool.reserve(nfeatures);
223
224     Random::engine eng((Random::seed_type)FEATURE_RECT_SEED);
225     Random::engine eng_ch(DCHANNELS_SEED);
226
227     Random::uniform chRand(0, N_CHANNELS - 1);
228
229     Random::uniform xRand(0, model.width  - 2);
230     Random::uniform yRand(0, model.height - 2);
231
232     Random::uniform wRand(1, model.width  - 1);
233     Random::uniform hRand(1, model.height - 1);
234
235     while (pool.size() < size_t(nfeatures))
236     {
237         int x = xRand(eng);
238         int y = yRand(eng);
239
240         int w = 1 + wRand(eng, model.width  - x - 1);
241         int h = 1 + hRand(eng, model.height - y - 1);
242
243         CV_Assert(w > 0);
244         CV_Assert(h > 0);
245
246         CV_Assert(w + x < model.width);
247         CV_Assert(h + y < model.height);
248
249         int ch = chRand(eng_ch);
250
251         ChannelFeature f(x, y, w, h, ch);
252
253         if (std::find(pool.begin(), pool.end(),f) == pool.end())
254         {
255             pool.push_back(f);
256         }
257     }
258 }
259
260 }
261
262 cv::Ptr<FeaturePool> FeaturePool::create(const cv::Size& model, int nfeatures, int nchannels )
263 {
264     cv::Ptr<FeaturePool> pool(new ChannelFeaturePool(model, nfeatures, nchannels));
265     return pool;
266 }