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