Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / src / backends / gpu / ggpuimgproc.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/imgproc.hpp"
11 #include "opencv2/gapi/gpu/imgproc.hpp"
12 #include "backends/gpu/ggpuimgproc.hpp"
13
14
15 GAPI_GPU_KERNEL(GGPUSepFilter, cv::gapi::imgproc::GSepFilter)
16 {
17     static void run(const cv::UMat& in, int ddepth, const cv::Mat& kernX, const cv::Mat& kernY, const cv::Point& anchor, const cv::Scalar& delta,
18                     int border, const cv::Scalar& bordVal, cv::UMat &out)
19     {
20         if( border == cv::BORDER_CONSTANT )
21         {
22             cv::UMat temp_in;
23             int width_add = (kernY.cols - 1) / 2;
24             int height_add =  (kernX.rows - 1) / 2;
25             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal);
26             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
27             cv::sepFilter2D(temp_in(rect), out, ddepth, kernX, kernY, anchor, delta.val[0], border);
28         }
29         else
30             cv::sepFilter2D(in, out, ddepth, kernX, kernY, anchor, delta.val[0], border);
31     }
32 };
33
34 GAPI_GPU_KERNEL(GGPUBoxFilter, cv::gapi::imgproc::GBoxFilter)
35 {
36     static void run(const cv::UMat& in, int ddepth, const cv::Size& ksize, const cv::Point& anchor, bool normalize, int borderType, const cv::Scalar& bordVal, cv::UMat &out)
37     {
38         if( borderType == cv::BORDER_CONSTANT )
39         {
40             cv::UMat temp_in;
41             int width_add = (ksize.width - 1) / 2;
42             int height_add =  (ksize.height - 1) / 2;
43             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
44             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
45             cv::boxFilter(temp_in(rect), out, ddepth, ksize, anchor, normalize, borderType);
46         }
47         else
48             cv::boxFilter(in, out, ddepth, ksize, anchor, normalize, borderType);
49     }
50 };
51
52 GAPI_GPU_KERNEL(GGPUBlur, cv::gapi::imgproc::GBlur)
53 {
54     static void run(const cv::UMat& in, const cv::Size& ksize, const cv::Point& anchor, int borderType, const cv::Scalar& bordVal, cv::UMat &out)
55     {
56         if( borderType == cv::BORDER_CONSTANT )
57         {
58             cv::UMat temp_in;
59             int width_add = (ksize.width - 1) / 2;
60             int height_add =  (ksize.height - 1) / 2;
61             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
62             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
63             cv::blur(temp_in(rect), out, ksize, anchor, borderType);
64         }
65         else
66             cv::blur(in, out, ksize, anchor, borderType);
67     }
68 };
69
70
71 GAPI_GPU_KERNEL(GGPUFilter2D, cv::gapi::imgproc::GFilter2D)
72 {
73     static void run(const cv::UMat& in, int ddepth, const cv::Mat& k, const cv::Point& anchor, const cv::Scalar& delta, int border,
74                     const cv::Scalar& bordVal, cv::UMat &out)
75     {
76         if( border == cv::BORDER_CONSTANT )
77         {
78             cv::UMat temp_in;
79             int width_add = (k.cols - 1) / 2;
80             int height_add =  (k.rows - 1) / 2;
81             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal );
82             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
83             cv::filter2D(temp_in(rect), out, ddepth, k, anchor, delta.val[0], border);
84         }
85         else
86             cv::filter2D(in, out, ddepth, k, anchor, delta.val[0], border);
87     }
88 };
89
90 GAPI_GPU_KERNEL(GGPUGaussBlur, cv::gapi::imgproc::GGaussBlur)
91 {
92     static void run(const cv::UMat& in, const cv::Size& ksize, double sigmaX, double sigmaY, int borderType, const cv::Scalar& bordVal, cv::UMat &out)
93     {
94         if( borderType == cv::BORDER_CONSTANT )
95         {
96             cv::UMat temp_in;
97             int width_add = (ksize.width - 1) / 2;
98             int height_add =  (ksize.height - 1) / 2;
99             cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal );
100             cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
101             cv::GaussianBlur(temp_in(rect), out, ksize, sigmaX, sigmaY, borderType);
102         }
103         else
104             cv::GaussianBlur(in, out, ksize, sigmaX, sigmaY, borderType);
105     }
106 };
107
108 GAPI_GPU_KERNEL(GGPUMedianBlur, cv::gapi::imgproc::GMedianBlur)
109 {
110     static void run(const cv::UMat& in, int ksize, cv::UMat &out)
111     {
112         cv::medianBlur(in, out, ksize);
113     }
114 };
115
116 GAPI_GPU_KERNEL(GGPUErode, cv::gapi::imgproc::GErode)
117 {
118     static void run(const cv::UMat& in, const cv::Mat& kernel, const cv::Point& anchor, int iterations, int borderType, const cv::Scalar& borderValue, cv::UMat &out)
119     {
120         cv::erode(in, out, kernel, anchor, iterations, borderType, borderValue);
121     }
122 };
123
124 GAPI_GPU_KERNEL(GGPUDilate, cv::gapi::imgproc::GDilate)
125 {
126     static void run(const cv::UMat& in, const cv::Mat& kernel, const cv::Point& anchor, int iterations, int borderType, const cv::Scalar& borderValue, cv::UMat &out)
127     {
128         cv::dilate(in, out, kernel, anchor, iterations, borderType, borderValue);
129     }
130 };
131
132 GAPI_GPU_KERNEL(GGPUSobel, cv::gapi::imgproc::GSobel)
133 {
134     static void run(const cv::UMat& in, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType,
135                     const cv::Scalar& bordVal, cv::UMat &out)
136     {
137         if( borderType == cv::BORDER_CONSTANT )
138         {
139             cv::UMat temp_in;
140             int add = (ksize - 1) / 2;
141             cv::copyMakeBorder(in, temp_in, add, add, add, add, borderType, bordVal );
142             cv::Rect rect = cv::Rect(add, add, in.cols, in.rows);
143             cv::Sobel(temp_in(rect), out, ddepth, dx, dy, ksize, scale, delta, borderType);
144         }
145         else
146         cv::Sobel(in, out, ddepth, dx, dy, ksize, scale, delta, borderType);
147     }
148 };
149
150 GAPI_GPU_KERNEL(GGPUEqualizeHist, cv::gapi::imgproc::GEqHist)
151 {
152     static void run(const cv::UMat& in, cv::UMat &out)
153     {
154         cv::equalizeHist(in, out);
155     }
156 };
157
158 GAPI_GPU_KERNEL(GGPUCanny, cv::gapi::imgproc::GCanny)
159 {
160     static void run(const cv::UMat& in, double thr1, double thr2, int apSize, bool l2gradient, cv::UMat &out)
161     {
162         cv::Canny(in, out, thr1, thr2, apSize, l2gradient);
163     }
164 };
165
166 GAPI_GPU_KERNEL(GGPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
167 {
168     static void run(const cv::UMat& in, cv::UMat &out)
169     {
170         cv::cvtColor(in, out, cv::COLOR_RGB2YUV);
171     }
172 };
173
174 GAPI_GPU_KERNEL(GGPUYUV2RGB, cv::gapi::imgproc::GYUV2RGB)
175 {
176     static void run(const cv::UMat& in, cv::UMat &out)
177     {
178         cv::cvtColor(in, out, cv::COLOR_YUV2RGB);
179     }
180 };
181
182 GAPI_GPU_KERNEL(GGPURGB2Lab, cv::gapi::imgproc::GRGB2Lab)
183 {
184     static void run(const cv::UMat& in, cv::UMat &out)
185     {
186         cv::cvtColor(in, out, cv::COLOR_RGB2Lab);
187     }
188 };
189
190 GAPI_GPU_KERNEL(GGPUBGR2LUV, cv::gapi::imgproc::GBGR2LUV)
191 {
192     static void run(const cv::UMat& in, cv::UMat &out)
193     {
194         cv::cvtColor(in, out, cv::COLOR_BGR2Luv);
195     }
196 };
197
198 GAPI_GPU_KERNEL(GGPUBGR2YUV, cv::gapi::imgproc::GBGR2YUV)
199 {
200     static void run(const cv::UMat& in, cv::UMat &out)
201     {
202         cv::cvtColor(in, out, cv::COLOR_BGR2YUV);
203     }
204 };
205
206 GAPI_GPU_KERNEL(GGPULUV2BGR, cv::gapi::imgproc::GLUV2BGR)
207 {
208     static void run(const cv::UMat& in, cv::UMat &out)
209     {
210         cv::cvtColor(in, out, cv::COLOR_Luv2BGR);
211     }
212 };
213
214 GAPI_GPU_KERNEL(GGPUYUV2BGR, cv::gapi::imgproc::GYUV2BGR)
215 {
216     static void run(const cv::UMat& in, cv::UMat &out)
217     {
218         cv::cvtColor(in, out, cv::COLOR_YUV2BGR);
219     }
220 };
221
222 GAPI_GPU_KERNEL(GGPURGB2Gray, cv::gapi::imgproc::GRGB2Gray)
223 {
224     static void run(const cv::UMat& in, cv::UMat &out)
225     {
226         cv::cvtColor(in, out, cv::COLOR_RGB2GRAY);
227     }
228 };
229
230 GAPI_GPU_KERNEL(GGPUBGR2Gray, cv::gapi::imgproc::GBGR2Gray)
231 {
232     static void run(const cv::UMat& in, cv::UMat &out)
233     {
234         cv::cvtColor(in, out, cv::COLOR_BGR2GRAY);
235     }
236 };
237
238 GAPI_GPU_KERNEL(GGPURGB2GrayCustom, cv::gapi::imgproc::GRGB2GrayCustom)
239 {
240     //TODO: avoid copy
241     static void run(const cv::UMat& in, float rY, float bY, float gY, cv::UMat &out)
242     {
243         cv::Mat planes[3];
244         cv::split(in.getMat(cv::ACCESS_READ), planes);
245         cv::Mat tmp_out = (planes[0]*rY + planes[1]*bY + planes[2]*gY);
246         tmp_out.copyTo(out);
247     }
248 };
249
250
251 cv::gapi::GKernelPackage cv::gapi::imgproc::gpu::kernels()
252 {
253     static auto pkg = cv::gapi::kernels
254         < GGPUFilter2D
255         , GGPUSepFilter
256         , GGPUBoxFilter
257         , GGPUBlur
258         , GGPUGaussBlur
259         , GGPUMedianBlur
260         , GGPUErode
261         , GGPUDilate
262         , GGPUSobel
263         , GGPUCanny
264         , GGPUEqualizeHist
265         , GGPURGB2YUV
266         , GGPUYUV2RGB
267         , GGPURGB2Lab
268         , GGPUBGR2LUV
269         , GGPUBGR2YUV
270         , GGPUYUV2BGR
271         , GGPULUV2BGR
272         , GGPUBGR2Gray
273         , GGPURGB2Gray
274         , GGPURGB2GrayCustom
275         >();
276     return pkg;
277 }