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.
5 // Copyright (C) 2018 Intel Corporation
10 #include <opencv2/gapi/imgproc.hpp>
11 #include <opencv2/gapi/cpu/imgproc.hpp>
12 #include <opencv2/gapi/cpu/gcpukernel.hpp>
14 #include "backends/fluid/gfluidimgproc_func.hpp"
17 cv::Mat add_border(const cv::Mat& in, const int ksize, const int borderType, const cv::Scalar& bordVal){
18 if( borderType == cv::BORDER_CONSTANT )
21 int add = (ksize - 1) / 2;
22 cv::copyMakeBorder(in, temp_in, add, add, add, add, borderType, bordVal);
23 return temp_in(cv::Rect(add, add, in.cols, in.rows));
29 GAPI_OCV_KERNEL(GCPUSepFilter, cv::gapi::imgproc::GSepFilter)
31 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,
32 int border, const cv::Scalar& bordVal, cv::Mat &out)
34 if( border == cv::BORDER_CONSTANT )
37 int width_add = (kernY.cols - 1) / 2;
38 int height_add = (kernX.rows - 1) / 2;
39 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal);
40 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
41 cv::sepFilter2D(temp_in(rect), out, ddepth, kernX, kernY, anchor, delta.val[0], border);
44 cv::sepFilter2D(in, out, ddepth, kernX, kernY, anchor, delta.val[0], border);
48 GAPI_OCV_KERNEL(GCPUBoxFilter, cv::gapi::imgproc::GBoxFilter)
50 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)
52 if( borderType == cv::BORDER_CONSTANT )
55 int width_add = (ksize.width - 1) / 2;
56 int height_add = (ksize.height - 1) / 2;
57 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
58 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
59 cv::boxFilter(temp_in(rect), out, ddepth, ksize, anchor, normalize, borderType);
62 cv::boxFilter(in, out, ddepth, ksize, anchor, normalize, borderType);
66 GAPI_OCV_KERNEL(GCPUBlur, cv::gapi::imgproc::GBlur)
68 static void run(const cv::Mat& in, const cv::Size& ksize, const cv::Point& anchor, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
70 if( borderType == cv::BORDER_CONSTANT )
73 int width_add = (ksize.width - 1) / 2;
74 int height_add = (ksize.height - 1) / 2;
75 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
76 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
77 cv::blur(temp_in(rect), out, ksize, anchor, borderType);
80 cv::blur(in, out, ksize, anchor, borderType);
85 GAPI_OCV_KERNEL(GCPUFilter2D, cv::gapi::imgproc::GFilter2D)
87 static void run(const cv::Mat& in, int ddepth, const cv::Mat& k, const cv::Point& anchor, const cv::Scalar& delta, int border,
88 const cv::Scalar& bordVal, cv::Mat &out)
90 if( border == cv::BORDER_CONSTANT )
93 int width_add = (k.cols - 1) / 2;
94 int height_add = (k.rows - 1) / 2;
95 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal );
96 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
97 cv::filter2D(temp_in(rect), out, ddepth, k, anchor, delta.val[0], border);
100 cv::filter2D(in, out, ddepth, k, anchor, delta.val[0], border);
104 GAPI_OCV_KERNEL(GCPUGaussBlur, cv::gapi::imgproc::GGaussBlur)
106 static void run(const cv::Mat& in, const cv::Size& ksize, double sigmaX, double sigmaY, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
108 if( borderType == cv::BORDER_CONSTANT )
111 int width_add = (ksize.width - 1) / 2;
112 int height_add = (ksize.height - 1) / 2;
113 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal );
114 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
115 cv::GaussianBlur(temp_in(rect), out, ksize, sigmaX, sigmaY, borderType);
118 cv::GaussianBlur(in, out, ksize, sigmaX, sigmaY, borderType);
122 GAPI_OCV_KERNEL(GCPUMedianBlur, cv::gapi::imgproc::GMedianBlur)
124 static void run(const cv::Mat& in, int ksize, cv::Mat &out)
126 cv::medianBlur(in, out, ksize);
130 GAPI_OCV_KERNEL(GCPUErode, cv::gapi::imgproc::GErode)
132 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)
134 cv::erode(in, out, kernel, anchor, iterations, borderType, borderValue);
138 GAPI_OCV_KERNEL(GCPUDilate, cv::gapi::imgproc::GDilate)
140 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)
142 cv::dilate(in, out, kernel, anchor, iterations, borderType, borderValue);
146 GAPI_OCV_KERNEL(GCPUSobel, cv::gapi::imgproc::GSobel)
148 static void run(const cv::Mat& in, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType,
149 const cv::Scalar& bordVal, cv::Mat &out)
151 cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
152 cv::Sobel(temp_in, out, ddepth, dx, dy, ksize, scale, delta, borderType);
156 GAPI_OCV_KERNEL(GCPUSobelXY, cv::gapi::imgproc::GSobelXY)
158 static void run(const cv::Mat& in, int ddepth, int order, int ksize, double scale, double delta, int borderType,
159 const cv::Scalar& bordVal, cv::Mat &out_dx, cv::Mat &out_dy)
161 cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
162 cv::Sobel(temp_in, out_dx, ddepth, order, 0, ksize, scale, delta, borderType);
163 cv::Sobel(temp_in, out_dy, ddepth, 0, order, ksize, scale, delta, borderType);
167 GAPI_OCV_KERNEL(GCPUEqualizeHist, cv::gapi::imgproc::GEqHist)
169 static void run(const cv::Mat& in, cv::Mat &out)
171 cv::equalizeHist(in, out);
175 GAPI_OCV_KERNEL(GCPUCanny, cv::gapi::imgproc::GCanny)
177 static void run(const cv::Mat& in, double thr1, double thr2, int apSize, bool l2gradient, cv::Mat &out)
179 cv::Canny(in, out, thr1, thr2, apSize, l2gradient);
183 GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
185 static void run(const cv::Mat& in, cv::Mat &out)
187 cv::cvtColor(in, out, cv::COLOR_RGB2YUV);
191 GAPI_OCV_KERNEL(GCPUYUV2RGB, cv::gapi::imgproc::GYUV2RGB)
193 static void run(const cv::Mat& in, cv::Mat &out)
195 cv::cvtColor(in, out, cv::COLOR_YUV2RGB);
199 GAPI_OCV_KERNEL(GCPUNV12toRGB, cv::gapi::imgproc::GNV12toRGB)
201 static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
203 cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2RGB_NV12);
207 GAPI_OCV_KERNEL(GCPUNV12toBGR, cv::gapi::imgproc::GNV12toBGR)
209 static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
211 cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2BGR_NV12);
215 GAPI_OCV_KERNEL(GCPURGB2Lab, cv::gapi::imgproc::GRGB2Lab)
217 static void run(const cv::Mat& in, cv::Mat &out)
219 cv::cvtColor(in, out, cv::COLOR_RGB2Lab);
223 GAPI_OCV_KERNEL(GCPUBGR2LUV, cv::gapi::imgproc::GBGR2LUV)
225 static void run(const cv::Mat& in, cv::Mat &out)
227 cv::cvtColor(in, out, cv::COLOR_BGR2Luv);
231 GAPI_OCV_KERNEL(GCPUBGR2YUV, cv::gapi::imgproc::GBGR2YUV)
233 static void run(const cv::Mat& in, cv::Mat &out)
235 cv::cvtColor(in, out, cv::COLOR_BGR2YUV);
239 GAPI_OCV_KERNEL(GCPULUV2BGR, cv::gapi::imgproc::GLUV2BGR)
241 static void run(const cv::Mat& in, cv::Mat &out)
243 cv::cvtColor(in, out, cv::COLOR_Luv2BGR);
247 GAPI_OCV_KERNEL(GCPUYUV2BGR, cv::gapi::imgproc::GYUV2BGR)
249 static void run(const cv::Mat& in, cv::Mat &out)
251 cv::cvtColor(in, out, cv::COLOR_YUV2BGR);
255 GAPI_OCV_KERNEL(GCPURGB2Gray, cv::gapi::imgproc::GRGB2Gray)
257 static void run(const cv::Mat& in, cv::Mat &out)
259 cv::cvtColor(in, out, cv::COLOR_RGB2GRAY);
263 GAPI_OCV_KERNEL(GCPUBGR2Gray, cv::gapi::imgproc::GBGR2Gray)
265 static void run(const cv::Mat& in, cv::Mat &out)
267 cv::cvtColor(in, out, cv::COLOR_BGR2GRAY);
271 GAPI_OCV_KERNEL(GCPURGB2GrayCustom, cv::gapi::imgproc::GRGB2GrayCustom)
273 static void run(const cv::Mat& in, float rY, float bY, float gY, cv::Mat &out)
276 cv::split(in, planes);
277 out = planes[0]*rY + planes[1]*bY + planes[2]*gY;
281 GAPI_OCV_KERNEL(GCPUBayerGR2RGB, cv::gapi::imgproc::GBayerGR2RGB)
283 static void run(const cv::Mat& in, cv::Mat &out)
285 cv::cvtColor(in, out, cv::COLOR_BayerGR2RGB);
289 GAPI_OCV_KERNEL(GCPURGB2HSV, cv::gapi::imgproc::GRGB2HSV)
291 static void run(const cv::Mat& in, cv::Mat &out)
293 cv::cvtColor(in, out, cv::COLOR_RGB2HSV);
297 GAPI_OCV_KERNEL(GCPURGB2YUV422, cv::gapi::imgproc::GRGB2YUV422)
299 static void run(const cv::Mat& in, cv::Mat &out)
301 out.create(in.size(), CV_8UC2);
303 for (int i = 0; i < in.rows; ++i)
305 const uchar* in_line_p = in.ptr<uchar>(i);
306 uchar* out_line_p = out.ptr<uchar>(i);
307 cv::gapi::fluid::run_rgb2yuv422_impl(out_line_p, in_line_p, in.cols);
312 static void toPlanar(const cv::Mat& in, cv::Mat& out)
314 GAPI_Assert(out.depth() == in.depth());
315 GAPI_Assert(out.channels() == 1);
316 GAPI_Assert(in.channels() == 3);
317 GAPI_Assert(out.cols == in.cols);
318 GAPI_Assert(out.rows == 3*in.rows);
320 std::vector<cv::Mat> outs(3);
321 for (int i = 0; i < 3; i++) {
322 outs[i] = out(cv::Rect(0, i*in.rows, in.cols, in.rows));
328 GAPI_OCV_KERNEL(GCPUNV12toRGBp, cv::gapi::imgproc::GNV12toRGBp)
330 static void run(const cv::Mat& inY, const cv::Mat& inUV, cv::Mat& out)
333 cv::cvtColorTwoPlane(inY, inUV, rgb, cv::COLOR_YUV2RGB_NV12);
338 GAPI_OCV_KERNEL(GCPUNV12toBGRp, cv::gapi::imgproc::GNV12toBGRp)
340 static void run(const cv::Mat& inY, const cv::Mat& inUV, cv::Mat& out)
343 cv::cvtColorTwoPlane(inY, inUV, rgb, cv::COLOR_YUV2BGR_NV12);
349 cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()
351 static auto pkg = cv::gapi::kernels