ab5d5d8fd40f0e7049420a8ea057006ba071dea9
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / cpu / gcpuimgproc.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Copyright (C) 2018 Intel Corporation
6
7
8 #include "precomp.hpp"
9
10 #include "opencv2/gapi/imgproc.hpp"
11 #include "opencv2/gapi/cpu/imgproc.hpp"
12 #include "backends/cpu/gcpuimgproc.hpp"
13
14 namespace {
15     cv::Mat add_border(const cv::Mat& in, const int ksize, const int borderType, const cv::Scalar& bordVal){
16         if( borderType == cv::BORDER_CONSTANT )
17         {
18             cv::Mat temp_in;
19             int add = (ksize - 1) / 2;
20             cv::copyMakeBorder(in, temp_in, add, add, add, add, borderType, bordVal);
21             return temp_in(cv::Rect(add, add, in.cols, in.rows));
22         }
23         return in;
24     }
25 }
26
27 GAPI_OCV_KERNEL(GCPUSepFilter, cv::gapi::imgproc::GSepFilter)
28 {
29     static void run(const cv::Mat& in, int ddepth, const cv::Mat& kernX, const cv::Mat& kernY, const cv::Point& anchor, const cv::Scalar& delta,
30                     int border, const cv::Scalar& bordVal, cv::Mat &out)
31     {
32         if( border == cv::BORDER_CONSTANT )
33         {
34             cv::Mat temp_in;
35             int width_add = (kernY.cols - 1) / 2;
36             int height_add =  (kernX.rows - 1) / 2;
37             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal);
38             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
39             cv::sepFilter2D(temp_in(rect), out, ddepth, kernX, kernY, anchor, delta.val[0], border);
40         }
41         else
42             cv::sepFilter2D(in, out, ddepth, kernX, kernY, anchor, delta.val[0], border);
43     }
44 };
45
46 GAPI_OCV_KERNEL(GCPUBoxFilter, cv::gapi::imgproc::GBoxFilter)
47 {
48     static void run(const cv::Mat& in, int ddepth, const cv::Size& ksize, const cv::Point& anchor, bool normalize, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
49     {
50         if( borderType == cv::BORDER_CONSTANT )
51         {
52             cv::Mat temp_in;
53             int width_add = (ksize.width - 1) / 2;
54             int height_add =  (ksize.height - 1) / 2;
55             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
56             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
57             cv::boxFilter(temp_in(rect), out, ddepth, ksize, anchor, normalize, borderType);
58         }
59         else
60             cv::boxFilter(in, out, ddepth, ksize, anchor, normalize, borderType);
61     }
62 };
63
64 GAPI_OCV_KERNEL(GCPUBlur, cv::gapi::imgproc::GBlur)
65 {
66     static void run(const cv::Mat& in, const cv::Size& ksize, const cv::Point& anchor, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
67     {
68         if( borderType == cv::BORDER_CONSTANT )
69         {
70             cv::Mat temp_in;
71             int width_add = (ksize.width - 1) / 2;
72             int height_add =  (ksize.height - 1) / 2;
73             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
74             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
75             cv::blur(temp_in(rect), out, ksize, anchor, borderType);
76         }
77         else
78             cv::blur(in, out, ksize, anchor, borderType);
79     }
80 };
81
82
83 GAPI_OCV_KERNEL(GCPUFilter2D, cv::gapi::imgproc::GFilter2D)
84 {
85     static void run(const cv::Mat& in, int ddepth, const cv::Mat& k, const cv::Point& anchor, const cv::Scalar& delta, int border,
86                     const cv::Scalar& bordVal, cv::Mat &out)
87     {
88         if( border == cv::BORDER_CONSTANT )
89         {
90             cv::Mat temp_in;
91             int width_add = (k.cols - 1) / 2;
92             int height_add =  (k.rows - 1) / 2;
93             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal );
94             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
95             cv::filter2D(temp_in(rect), out, ddepth, k, anchor, delta.val[0], border);
96         }
97         else
98             cv::filter2D(in, out, ddepth, k, anchor, delta.val[0], border);
99     }
100 };
101
102 GAPI_OCV_KERNEL(GCPUGaussBlur, cv::gapi::imgproc::GGaussBlur)
103 {
104     static void run(const cv::Mat& in, const cv::Size& ksize, double sigmaX, double sigmaY, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
105     {
106         if( borderType == cv::BORDER_CONSTANT )
107         {
108             cv::Mat temp_in;
109             int width_add = (ksize.width - 1) / 2;
110             int height_add =  (ksize.height - 1) / 2;
111             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal );
112             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
113             cv::GaussianBlur(temp_in(rect), out, ksize, sigmaX, sigmaY, borderType);
114         }
115         else
116             cv::GaussianBlur(in, out, ksize, sigmaX, sigmaY, borderType);
117     }
118 };
119
120 GAPI_OCV_KERNEL(GCPUMedianBlur, cv::gapi::imgproc::GMedianBlur)
121 {
122     static void run(const cv::Mat& in, int ksize, cv::Mat &out)
123     {
124         cv::medianBlur(in, out, ksize);
125     }
126 };
127
128 GAPI_OCV_KERNEL(GCPUErode, cv::gapi::imgproc::GErode)
129 {
130     static void run(const cv::Mat& in, const cv::Mat& kernel, const cv::Point& anchor, int iterations, int borderType, const cv::Scalar& borderValue, cv::Mat &out)
131     {
132         cv::erode(in, out, kernel, anchor, iterations, borderType, borderValue);
133     }
134 };
135
136 GAPI_OCV_KERNEL(GCPUDilate, cv::gapi::imgproc::GDilate)
137 {
138     static void run(const cv::Mat& in, const cv::Mat& kernel, const cv::Point& anchor, int iterations, int borderType, const cv::Scalar& borderValue, cv::Mat &out)
139     {
140         cv::dilate(in, out, kernel, anchor, iterations, borderType, borderValue);
141     }
142 };
143
144 GAPI_OCV_KERNEL(GCPUSobel, cv::gapi::imgproc::GSobel)
145 {
146     static void run(const cv::Mat& in, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType,
147                     const cv::Scalar& bordVal, cv::Mat &out)
148     {
149         cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
150         cv::Sobel(temp_in, out, ddepth, dx, dy, ksize, scale, delta, borderType);
151     }
152 };
153
154 GAPI_OCV_KERNEL(GCPUSobelXY, cv::gapi::imgproc::GSobelXY)
155 {
156     static void run(const cv::Mat& in, int ddepth, int order, int ksize, double scale, double delta, int borderType,
157                     const cv::Scalar& bordVal, cv::Mat &out_dx, cv::Mat &out_dy)
158     {
159         cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
160         cv::Sobel(temp_in, out_dx, ddepth, order, 0, ksize, scale, delta, borderType);
161         cv::Sobel(temp_in, out_dy, ddepth, 0, order, ksize, scale, delta, borderType);
162     }
163 };
164
165 GAPI_OCV_KERNEL(GCPUEqualizeHist, cv::gapi::imgproc::GEqHist)
166 {
167     static void run(const cv::Mat& in, cv::Mat &out)
168     {
169         cv::equalizeHist(in, out);
170     }
171 };
172
173 GAPI_OCV_KERNEL(GCPUCanny, cv::gapi::imgproc::GCanny)
174 {
175     static void run(const cv::Mat& in, double thr1, double thr2, int apSize, bool l2gradient, cv::Mat &out)
176     {
177         cv::Canny(in, out, thr1, thr2, apSize, l2gradient);
178     }
179 };
180
181 GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
182 {
183     static void run(const cv::Mat& in, cv::Mat &out)
184     {
185         cv::cvtColor(in, out, cv::COLOR_RGB2YUV);
186     }
187 };
188
189 GAPI_OCV_KERNEL(GCPUYUV2RGB, cv::gapi::imgproc::GYUV2RGB)
190 {
191     static void run(const cv::Mat& in, cv::Mat &out)
192     {
193         cv::cvtColor(in, out, cv::COLOR_YUV2RGB);
194     }
195 };
196
197 GAPI_OCV_KERNEL(GCPUNV12toRGB, cv::gapi::imgproc::GNV12toRGB)
198 {
199     static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
200     {
201         cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2RGB_NV12);
202     }
203 };
204
205 GAPI_OCV_KERNEL(GCPUNV12toBGR, cv::gapi::imgproc::GNV12toBGR)
206 {
207     static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
208     {
209         cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2BGR_NV12);
210     }
211 };
212
213 GAPI_OCV_KERNEL(GCPURGB2Lab, cv::gapi::imgproc::GRGB2Lab)
214 {
215     static void run(const cv::Mat& in, cv::Mat &out)
216     {
217         cv::cvtColor(in, out, cv::COLOR_RGB2Lab);
218     }
219 };
220
221 GAPI_OCV_KERNEL(GCPUBGR2LUV, cv::gapi::imgproc::GBGR2LUV)
222 {
223     static void run(const cv::Mat& in, cv::Mat &out)
224     {
225         cv::cvtColor(in, out, cv::COLOR_BGR2Luv);
226     }
227 };
228
229 GAPI_OCV_KERNEL(GCPUBGR2YUV, cv::gapi::imgproc::GBGR2YUV)
230 {
231     static void run(const cv::Mat& in, cv::Mat &out)
232     {
233         cv::cvtColor(in, out, cv::COLOR_BGR2YUV);
234     }
235 };
236
237 GAPI_OCV_KERNEL(GCPULUV2BGR, cv::gapi::imgproc::GLUV2BGR)
238 {
239     static void run(const cv::Mat& in, cv::Mat &out)
240     {
241         cv::cvtColor(in, out, cv::COLOR_Luv2BGR);
242     }
243 };
244
245 GAPI_OCV_KERNEL(GCPUYUV2BGR, cv::gapi::imgproc::GYUV2BGR)
246 {
247     static void run(const cv::Mat& in, cv::Mat &out)
248     {
249         cv::cvtColor(in, out, cv::COLOR_YUV2BGR);
250     }
251 };
252
253 GAPI_OCV_KERNEL(GCPURGB2Gray, cv::gapi::imgproc::GRGB2Gray)
254 {
255     static void run(const cv::Mat& in, cv::Mat &out)
256     {
257         cv::cvtColor(in, out, cv::COLOR_RGB2GRAY);
258     }
259 };
260
261 GAPI_OCV_KERNEL(GCPUBGR2Gray, cv::gapi::imgproc::GBGR2Gray)
262 {
263     static void run(const cv::Mat& in, cv::Mat &out)
264     {
265         cv::cvtColor(in, out, cv::COLOR_BGR2GRAY);
266     }
267 };
268
269 GAPI_OCV_KERNEL(GCPURGB2GrayCustom, cv::gapi::imgproc::GRGB2GrayCustom)
270 {
271     static void run(const cv::Mat& in, float rY, float bY, float gY, cv::Mat &out)
272     {
273         cv::Mat planes[3];
274         cv::split(in, planes);
275         out = planes[0]*rY + planes[1]*bY + planes[2]*gY;
276     }
277 };
278
279 cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()
280 {
281     static auto pkg = cv::gapi::kernels
282         < GCPUFilter2D
283         , GCPUSepFilter
284         , GCPUBoxFilter
285         , GCPUBlur
286         , GCPUGaussBlur
287         , GCPUMedianBlur
288         , GCPUErode
289         , GCPUDilate
290         , GCPUSobel
291         , GCPUSobelXY
292         , GCPUCanny
293         , GCPUEqualizeHist
294         , GCPURGB2YUV
295         , GCPUYUV2RGB
296         , GCPUNV12toRGB
297         , GCPUNV12toBGR
298         , GCPURGB2Lab
299         , GCPUBGR2LUV
300         , GCPUBGR2YUV
301         , GCPUYUV2BGR
302         , GCPULUV2BGR
303         , GCPUBGR2Gray
304         , GCPURGB2Gray
305         , GCPURGB2GrayCustom
306         >();
307     return pkg;
308 }