Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / cpu / gcpucore.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-2019 Intel Corporation
6
7
8 #include "precomp.hpp"
9
10 #include "opencv2/gapi/core.hpp"
11 #include "opencv2/gapi/cpu/core.hpp"
12 #include "backends/cpu/gcpucore.hpp"
13
14 GAPI_OCV_KERNEL(GCPUAdd, cv::gapi::core::GAdd)
15 {
16     static void run(const cv::Mat& a, const cv::Mat& b, int dtype, cv::Mat& out)
17     {
18         cv::add(a, b, out, cv::noArray(), dtype);
19     }
20 };
21
22 GAPI_OCV_KERNEL(GCPUAddC, cv::gapi::core::GAddC)
23 {
24     static void run(const cv::Mat& a, const cv::Scalar& b, int dtype, cv::Mat& out)
25     {
26         cv::add(a, b, out, cv::noArray(), dtype);
27     }
28 };
29
30 GAPI_OCV_KERNEL(GCPUSub, cv::gapi::core::GSub)
31 {
32     static void run(const cv::Mat& a, const cv::Mat& b, int dtype, cv::Mat& out)
33     {
34         cv::subtract(a, b, out, cv::noArray(), dtype);
35     }
36 };
37
38 GAPI_OCV_KERNEL(GCPUSubC, cv::gapi::core::GSubC)
39 {
40     static void run(const cv::Mat& a, const cv::Scalar& b, int dtype, cv::Mat& out)
41     {
42         cv::subtract(a, b, out, cv::noArray(), dtype);
43     }
44 };
45
46 GAPI_OCV_KERNEL(GCPUSubRC, cv::gapi::core::GSubRC)
47 {
48     static void run(const cv::Scalar& a, const cv::Mat& b, int dtype, cv::Mat& out)
49     {
50         cv::subtract(a, b, out, cv::noArray(), dtype);
51     }
52 };
53
54 GAPI_OCV_KERNEL(GCPUMul, cv::gapi::core::GMul)
55 {
56     static void run(const cv::Mat& a, const cv::Mat& b, double scale, int dtype, cv::Mat& out)
57     {
58         cv::multiply(a, b, out, scale, dtype);
59     }
60 };
61
62 GAPI_OCV_KERNEL(GCPUMulCOld, cv::gapi::core::GMulCOld)
63 {
64     static void run(const cv::Mat& a, double b, int dtype, cv::Mat& out)
65     {
66         cv::multiply(a, b, out, 1, dtype);
67     }
68 };
69
70 GAPI_OCV_KERNEL(GCPUMulC, cv::gapi::core::GMulC)
71 {
72     static void run(const cv::Mat& a, const cv::Scalar& b, int dtype, cv::Mat& out)
73     {
74         cv::multiply(a, b, out, 1, dtype);
75     }
76 };
77
78 GAPI_OCV_KERNEL(GCPUDiv, cv::gapi::core::GDiv)
79 {
80     static void run(const cv::Mat& a, const cv::Mat& b, double scale, int dtype, cv::Mat& out)
81     {
82         cv::divide(a, b, out, scale, dtype);
83     }
84 };
85
86 GAPI_OCV_KERNEL(GCPUDivC, cv::gapi::core::GDivC)
87 {
88     static void run(const cv::Mat& a, const cv::Scalar& b, double scale, int dtype, cv::Mat& out)
89     {
90         cv::divide(a, b, out, scale, dtype);
91     }
92 };
93
94 GAPI_OCV_KERNEL(GCPUDivRC, cv::gapi::core::GDivRC)
95 {
96     static void run(const cv::Scalar& a, const cv::Mat& b, double scale, int dtype, cv::Mat& out)
97     {
98         cv::divide(a, b, out, scale, dtype);
99     }
100 };
101
102 GAPI_OCV_KERNEL(GCPUMask, cv::gapi::core::GMask)
103 {
104     static void run(const cv::Mat& in, const cv::Mat& mask, cv::Mat& out)
105     {
106         out = cv::Mat::zeros(in.size(), in.type());
107         in.copyTo(out, mask);
108     }
109 };
110
111 GAPI_OCV_KERNEL(GCPUMean, cv::gapi::core::GMean)
112 {
113     static void run(const cv::Mat& in, cv::Scalar& out)
114     {
115         out = cv::mean(in);
116     }
117 };
118
119 GAPI_OCV_KERNEL(GCPUPolarToCart, cv::gapi::core::GPolarToCart)
120 {
121     static void run(const cv::Mat& magn, const cv::Mat& angle, bool angleInDegrees, cv::Mat& outx, cv::Mat& outy)
122     {
123         cv::polarToCart(magn, angle, outx, outy, angleInDegrees);
124     }
125 };
126
127 GAPI_OCV_KERNEL(GCPUCartToPolar, cv::gapi::core::GCartToPolar)
128 {
129     static void run(const cv::Mat& x, const cv::Mat& y, bool angleInDegrees, cv::Mat& outmagn, cv::Mat& outangle)
130     {
131         cv::cartToPolar(x, y, outmagn, outangle, angleInDegrees);
132     }
133 };
134
135 GAPI_OCV_KERNEL(GCPUPhase, cv::gapi::core::GPhase)
136 {
137     static void run(const cv::Mat &x, const cv::Mat &y, bool angleInDegrees, cv::Mat &out)
138     {
139         cv::phase(x, y, out, angleInDegrees);
140     }
141 };
142
143 GAPI_OCV_KERNEL(GCPUCmpGT, cv::gapi::core::GCmpGT)
144 {
145     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
146     {
147         cv::compare(a, b, out, cv::CMP_GT);
148     }
149 };
150
151 GAPI_OCV_KERNEL(GCPUCmpGE, cv::gapi::core::GCmpGE)
152 {
153     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
154     {
155         cv::compare(a, b, out, cv::CMP_GE);
156     }
157 };
158
159 GAPI_OCV_KERNEL(GCPUCmpLE, cv::gapi::core::GCmpLE)
160 {
161     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
162     {
163         cv::compare(a, b, out, cv::CMP_LE);
164     }
165 };
166
167 GAPI_OCV_KERNEL(GCPUCmpLT, cv::gapi::core::GCmpLT)
168 {
169     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
170     {
171         cv::compare(a, b, out, cv::CMP_LT);
172     }
173 };
174
175 GAPI_OCV_KERNEL(GCPUCmpEQ, cv::gapi::core::GCmpEQ)
176 {
177     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
178     {
179         cv::compare(a, b, out, cv::CMP_EQ);
180     }
181 };
182
183 GAPI_OCV_KERNEL(GCPUCmpNE, cv::gapi::core::GCmpNE)
184 {
185     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
186     {
187         cv::compare(a, b, out, cv::CMP_NE);
188     }
189 };
190
191 GAPI_OCV_KERNEL(GCPUCmpGTScalar, cv::gapi::core::GCmpGTScalar)
192 {
193     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
194     {
195         cv::compare(a, b, out, cv::CMP_GT);
196     }
197 };
198
199 GAPI_OCV_KERNEL(GCPUCmpGEScalar, cv::gapi::core::GCmpGEScalar)
200 {
201     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
202     {
203         cv::compare(a, b, out, cv::CMP_GE);
204     }
205 };
206
207 GAPI_OCV_KERNEL(GCPUCmpLEScalar, cv::gapi::core::GCmpLEScalar)
208 {
209     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
210     {
211         cv::compare(a, b, out, cv::CMP_LE);
212     }
213 };
214
215 GAPI_OCV_KERNEL(GCPUCmpLTScalar, cv::gapi::core::GCmpLTScalar)
216 {
217     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
218     {
219         cv::compare(a, b, out, cv::CMP_LT);
220     }
221 };
222
223 GAPI_OCV_KERNEL(GCPUCmpEQScalar, cv::gapi::core::GCmpEQScalar)
224 {
225     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
226     {
227         cv::compare(a, b, out, cv::CMP_EQ);
228     }
229 };
230
231 GAPI_OCV_KERNEL(GCPUCmpNEScalar, cv::gapi::core::GCmpNEScalar)
232 {
233     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
234     {
235         cv::compare(a, b, out, cv::CMP_NE);
236     }
237 };
238
239 GAPI_OCV_KERNEL(GCPUAnd, cv::gapi::core::GAnd)
240 {
241     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
242     {
243         cv::bitwise_and(a, b, out);
244     }
245 };
246
247 GAPI_OCV_KERNEL(GCPUAndS, cv::gapi::core::GAndS)
248 {
249     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
250     {
251         cv::bitwise_and(a, b, out);
252     }
253 };
254
255 GAPI_OCV_KERNEL(GCPUOr, cv::gapi::core::GOr)
256 {
257     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
258     {
259         cv::bitwise_or(a, b, out);
260     }
261 };
262
263 GAPI_OCV_KERNEL(GCPUOrS, cv::gapi::core::GOrS)
264 {
265     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
266     {
267         cv::bitwise_or(a, b, out);
268     }
269 };
270
271 GAPI_OCV_KERNEL(GCPUXor, cv::gapi::core::GXor)
272 {
273     static void run(const cv::Mat& a, const cv::Mat& b, cv::Mat& out)
274     {
275         cv::bitwise_xor(a, b, out);
276     }
277 };
278
279 GAPI_OCV_KERNEL(GCPUXorS, cv::gapi::core::GXorS)
280 {
281     static void run(const cv::Mat& a, const cv::Scalar& b, cv::Mat& out)
282     {
283         cv::bitwise_xor(a, b, out);
284     }
285 };
286
287 GAPI_OCV_KERNEL(GCPUNot, cv::gapi::core::GNot)
288 {
289     static void run(const cv::Mat& a, cv::Mat& out)
290     {
291         cv::bitwise_not(a, out);
292     }
293 };
294
295 GAPI_OCV_KERNEL(GCPUSelect, cv::gapi::core::GSelect)
296 {
297     static void run(const cv::Mat& src1, const cv::Mat& src2, const cv::Mat& mask, cv::Mat& out)
298     {
299         src2.copyTo(out);
300         src1.copyTo(out, mask);
301     }
302 };
303
304 GAPI_OCV_KERNEL(GCPUMin, cv::gapi::core::GMin)
305 {
306     static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out)
307     {
308         out = cv::min(in1, in2);
309     }
310 };
311
312 GAPI_OCV_KERNEL(GCPUMax, cv::gapi::core::GMax)
313 {
314     static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out)
315     {
316         out = cv::max(in1, in2);
317     }
318 };
319
320 GAPI_OCV_KERNEL(GCPUAbsDiff, cv::gapi::core::GAbsDiff)
321 {
322     static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out)
323     {
324         cv::absdiff(in1, in2, out);
325     }
326 };
327
328 GAPI_OCV_KERNEL(GCPUAbsDiffC, cv::gapi::core::GAbsDiffC)
329 {
330     static void run(const cv::Mat& in1, const cv::Scalar& in2, cv::Mat& out)
331     {
332         cv::absdiff(in1, in2, out);
333     }
334 };
335
336 GAPI_OCV_KERNEL(GCPUSum, cv::gapi::core::GSum)
337 {
338     static void run(const cv::Mat& in, cv::Scalar& out)
339     {
340         out = cv::sum(in);
341     }
342 };
343
344 GAPI_OCV_KERNEL(GCPUAddW, cv::gapi::core::GAddW)
345 {
346     static void run(const cv::Mat& in1, double alpha, const cv::Mat& in2, double beta, double gamma, int dtype, cv::Mat& out)
347     {
348         cv::addWeighted(in1, alpha, in2, beta, gamma, out, dtype);
349     }
350 };
351
352 GAPI_OCV_KERNEL(GCPUNormL1, cv::gapi::core::GNormL1)
353 {
354     static void run(const cv::Mat& in, cv::Scalar& out)
355     {
356         out = cv::norm(in, cv::NORM_L1);
357     }
358 };
359
360 GAPI_OCV_KERNEL(GCPUNormL2, cv::gapi::core::GNormL2)
361 {
362     static void run(const cv::Mat& in, cv::Scalar& out)
363     {
364         out = cv::norm(in, cv::NORM_L2);
365     }
366 };
367
368 GAPI_OCV_KERNEL(GCPUNormInf, cv::gapi::core::GNormInf)
369 {
370     static void run(const cv::Mat& in, cv::Scalar& out)
371     {
372         out = cv::norm(in, cv::NORM_INF);
373     }
374 };
375
376 GAPI_OCV_KERNEL(GCPUIntegral, cv::gapi::core::GIntegral)
377 {
378     static void run(const cv::Mat& in, int sdepth, int sqdepth, cv::Mat& out, cv::Mat& outSq)
379     {
380         cv::integral(in, out, outSq, sdepth, sqdepth);
381     }
382 };
383
384 GAPI_OCV_KERNEL(GCPUThreshold, cv::gapi::core::GThreshold)
385 {
386     static void run(const cv::Mat& in, const cv::Scalar& a, const cv::Scalar& b, int type, cv::Mat& out)
387     {
388         cv::threshold(in, out, a.val[0], b.val[0], type);
389     }
390 };
391
392 GAPI_OCV_KERNEL(GCPUThresholdOT, cv::gapi::core::GThresholdOT)
393 {
394     static void run(const cv::Mat& in, const cv::Scalar& b, int type, cv::Mat& out, cv::Scalar& outScalar)
395     {
396         outScalar = cv::threshold(in, out, b.val[0], b.val[0], type);
397     }
398 };
399
400
401 GAPI_OCV_KERNEL(GCPUInRange, cv::gapi::core::GInRange)
402 {
403     static void run(const cv::Mat& in, const cv::Scalar& low, const cv::Scalar& up, cv::Mat& out)
404     {
405         cv::inRange(in, low, up, out);
406     }
407 };
408
409 GAPI_OCV_KERNEL(GCPUSplit3, cv::gapi::core::GSplit3)
410 {
411     static void run(const cv::Mat& in, cv::Mat &m1, cv::Mat &m2, cv::Mat &m3)
412     {
413         std::vector<cv::Mat> outMats = {m1, m2, m3};
414         cv::split(in, outMats);
415
416         // Write back FIXME: Write a helper or avoid this nonsence completely!
417         m1 = outMats[0];
418         m2 = outMats[1];
419         m3 = outMats[2];
420     }
421 };
422
423 GAPI_OCV_KERNEL(GCPUSplit4, cv::gapi::core::GSplit4)
424 {
425     static void run(const cv::Mat& in, cv::Mat &m1, cv::Mat &m2, cv::Mat &m3, cv::Mat &m4)
426     {
427         std::vector<cv::Mat> outMats = {m1, m2, m3, m4};
428         cv::split(in, outMats);
429
430         // Write back FIXME: Write a helper or avoid this nonsence completely!
431         m1 = outMats[0];
432         m2 = outMats[1];
433         m3 = outMats[2];
434         m4 = outMats[3];
435     }
436 };
437
438 GAPI_OCV_KERNEL(GCPUMerge3, cv::gapi::core::GMerge3)
439 {
440     static void run(const cv::Mat& in1, const cv::Mat& in2, const cv::Mat& in3, cv::Mat &out)
441     {
442         std::vector<cv::Mat> inMats = {in1, in2, in3};
443         cv::merge(inMats, out);
444     }
445 };
446
447 GAPI_OCV_KERNEL(GCPUMerge4, cv::gapi::core::GMerge4)
448 {
449     static void run(const cv::Mat& in1, const cv::Mat& in2, const cv::Mat& in3, const cv::Mat& in4, cv::Mat &out)
450     {
451         std::vector<cv::Mat> inMats = {in1, in2, in3, in4};
452         cv::merge(inMats, out);
453     }
454 };
455
456 GAPI_OCV_KERNEL(GCPUResize, cv::gapi::core::GResize)
457 {
458     static void run(const cv::Mat& in, cv::Size sz, double fx, double fy, int interp, cv::Mat &out)
459     {
460         cv::resize(in, out, sz, fx, fy, interp);
461     }
462 };
463
464 GAPI_OCV_KERNEL(GCPURemap, cv::gapi::core::GRemap)
465 {
466     static void run(const cv::Mat& in, const cv::Mat& x, const cv::Mat& y, int a, int b, cv::Scalar s, cv::Mat& out)
467     {
468         cv::remap(in, out, x, y, a, b, s);
469     }
470 };
471
472 GAPI_OCV_KERNEL(GCPUFlip, cv::gapi::core::GFlip)
473 {
474     static void run(const cv::Mat& in, int code, cv::Mat& out)
475     {
476         cv::flip(in, out, code);
477     }
478 };
479
480 GAPI_OCV_KERNEL(GCPUCrop, cv::gapi::core::GCrop)
481 {
482     static void run(const cv::Mat& in, cv::Rect rect, cv::Mat& out)
483     {
484         cv::Mat(in, rect).copyTo(out);
485     }
486 };
487
488 GAPI_OCV_KERNEL(GCPUConcatHor, cv::gapi::core::GConcatHor)
489 {
490     static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out)
491     {
492         cv::hconcat(in1, in2, out);
493     }
494 };
495
496 GAPI_OCV_KERNEL(GCPUConcatVert, cv::gapi::core::GConcatVert)
497 {
498     static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out)
499     {
500         cv::vconcat(in1, in2, out);
501     }
502 };
503
504 GAPI_OCV_KERNEL(GCPULUT, cv::gapi::core::GLUT)
505 {
506     static void run(const cv::Mat& in, const cv::Mat& lut, cv::Mat& out)
507     {
508         cv::LUT(in, lut, out);
509     }
510 };
511
512 GAPI_OCV_KERNEL(GCPUConvertTo, cv::gapi::core::GConvertTo)
513 {
514     static void run(const cv::Mat& in, int rtype, double alpha, double beta, cv::Mat& out)
515     {
516         in.convertTo(out, rtype, alpha, beta);
517     }
518 };
519
520 GAPI_OCV_KERNEL(GCPUSqrt, cv::gapi::core::GSqrt)
521 {
522     static void run(const cv::Mat& in, cv::Mat &out)
523     {
524         cv::sqrt(in, out);
525     }
526 };
527
528 cv::gapi::GKernelPackage cv::gapi::core::cpu::kernels()
529 {
530     static auto pkg = cv::gapi::kernels
531         <  GCPUAdd
532          , GCPUAddC
533          , GCPUSub
534          , GCPUSubC
535          , GCPUSubRC
536          , GCPUMul
537          , GCPUMulC
538          , GCPUMulCOld
539          , GCPUDiv
540          , GCPUDivC
541          , GCPUDivRC
542          , GCPUMean
543          , GCPUMask
544          , GCPUPolarToCart
545          , GCPUCartToPolar
546          , GCPUPhase
547          , GCPUCmpGT
548          , GCPUCmpGE
549          , GCPUCmpLE
550          , GCPUCmpLT
551          , GCPUCmpEQ
552          , GCPUCmpNE
553          , GCPUCmpGTScalar
554          , GCPUCmpGEScalar
555          , GCPUCmpLEScalar
556          , GCPUCmpLTScalar
557          , GCPUCmpEQScalar
558          , GCPUCmpNEScalar
559          , GCPUAnd
560          , GCPUAndS
561          , GCPUOr
562          , GCPUOrS
563          , GCPUXor
564          , GCPUXorS
565          , GCPUNot
566          , GCPUSelect
567          , GCPUMin
568          , GCPUMax
569          , GCPUAbsDiff
570          , GCPUAbsDiffC
571          , GCPUSum
572          , GCPUAddW
573          , GCPUNormL1
574          , GCPUNormL2
575          , GCPUNormInf
576          , GCPUIntegral
577          , GCPUThreshold
578          , GCPUThresholdOT
579          , GCPUInRange
580          , GCPUSplit3
581          , GCPUSplit4
582          , GCPUResize
583          , GCPUMerge3
584          , GCPUMerge4
585          , GCPURemap
586          , GCPUFlip
587          , GCPUCrop
588          , GCPUConcatHor
589          , GCPUConcatVert
590          , GCPULUT
591          , GCPUConvertTo
592          , GCPUSqrt
593          >();
594     return pkg;
595 }