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