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 "backends/cpu/gcpuimgproc.hpp"
15 cv::Mat add_border(const cv::Mat& in, const int ksize, const int borderType, const cv::Scalar& bordVal){
16 if( borderType == cv::BORDER_CONSTANT )
19 int add = (ksize - 1) / 2;
20 cv::copyMakeBorder(in, temp_in, add, add, add, add, borderType, bordVal);
21 return temp_in(cv::Rect(add, add, in.cols, in.rows));
27 GAPI_OCV_KERNEL(GCPUSepFilter, cv::gapi::imgproc::GSepFilter)
29 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,
30 int border, const cv::Scalar& bordVal, cv::Mat &out)
32 if( border == cv::BORDER_CONSTANT )
35 int width_add = (kernY.cols - 1) / 2;
36 int height_add = (kernX.rows - 1) / 2;
37 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal);
38 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
39 cv::sepFilter2D(temp_in(rect), out, ddepth, kernX, kernY, anchor, delta.val[0], border);
42 cv::sepFilter2D(in, out, ddepth, kernX, kernY, anchor, delta.val[0], border);
46 GAPI_OCV_KERNEL(GCPUBoxFilter, cv::gapi::imgproc::GBoxFilter)
48 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)
50 if( borderType == cv::BORDER_CONSTANT )
53 int width_add = (ksize.width - 1) / 2;
54 int height_add = (ksize.height - 1) / 2;
55 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
56 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
57 cv::boxFilter(temp_in(rect), out, ddepth, ksize, anchor, normalize, borderType);
60 cv::boxFilter(in, out, ddepth, ksize, anchor, normalize, borderType);
64 GAPI_OCV_KERNEL(GCPUBlur, cv::gapi::imgproc::GBlur)
66 static void run(const cv::Mat& in, const cv::Size& ksize, const cv::Point& anchor, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
68 if( borderType == cv::BORDER_CONSTANT )
71 int width_add = (ksize.width - 1) / 2;
72 int height_add = (ksize.height - 1) / 2;
73 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);
74 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
75 cv::blur(temp_in(rect), out, ksize, anchor, borderType);
78 cv::blur(in, out, ksize, anchor, borderType);
83 GAPI_OCV_KERNEL(GCPUFilter2D, cv::gapi::imgproc::GFilter2D)
85 static void run(const cv::Mat& in, int ddepth, const cv::Mat& k, const cv::Point& anchor, const cv::Scalar& delta, int border,
86 const cv::Scalar& bordVal, cv::Mat &out)
88 if( border == cv::BORDER_CONSTANT )
91 int width_add = (k.cols - 1) / 2;
92 int height_add = (k.rows - 1) / 2;
93 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal );
94 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
95 cv::filter2D(temp_in(rect), out, ddepth, k, anchor, delta.val[0], border);
98 cv::filter2D(in, out, ddepth, k, anchor, delta.val[0], border);
102 GAPI_OCV_KERNEL(GCPUGaussBlur, cv::gapi::imgproc::GGaussBlur)
104 static void run(const cv::Mat& in, const cv::Size& ksize, double sigmaX, double sigmaY, int borderType, const cv::Scalar& bordVal, cv::Mat &out)
106 if( borderType == cv::BORDER_CONSTANT )
109 int width_add = (ksize.width - 1) / 2;
110 int height_add = (ksize.height - 1) / 2;
111 cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal );
112 cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);
113 cv::GaussianBlur(temp_in(rect), out, ksize, sigmaX, sigmaY, borderType);
116 cv::GaussianBlur(in, out, ksize, sigmaX, sigmaY, borderType);
120 GAPI_OCV_KERNEL(GCPUMedianBlur, cv::gapi::imgproc::GMedianBlur)
122 static void run(const cv::Mat& in, int ksize, cv::Mat &out)
124 cv::medianBlur(in, out, ksize);
128 GAPI_OCV_KERNEL(GCPUErode, cv::gapi::imgproc::GErode)
130 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)
132 cv::erode(in, out, kernel, anchor, iterations, borderType, borderValue);
136 GAPI_OCV_KERNEL(GCPUDilate, cv::gapi::imgproc::GDilate)
138 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)
140 cv::dilate(in, out, kernel, anchor, iterations, borderType, borderValue);
144 GAPI_OCV_KERNEL(GCPUSobel, cv::gapi::imgproc::GSobel)
146 static void run(const cv::Mat& in, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType,
147 const cv::Scalar& bordVal, cv::Mat &out)
149 cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
150 cv::Sobel(temp_in, out, ddepth, dx, dy, ksize, scale, delta, borderType);
154 GAPI_OCV_KERNEL(GCPUSobelXY, cv::gapi::imgproc::GSobelXY)
156 static void run(const cv::Mat& in, int ddepth, int order, int ksize, double scale, double delta, int borderType,
157 const cv::Scalar& bordVal, cv::Mat &out_dx, cv::Mat &out_dy)
159 cv::Mat temp_in = add_border(in, ksize, borderType, bordVal);
160 cv::Sobel(temp_in, out_dx, ddepth, order, 0, ksize, scale, delta, borderType);
161 cv::Sobel(temp_in, out_dy, ddepth, 0, order, ksize, scale, delta, borderType);
165 GAPI_OCV_KERNEL(GCPUEqualizeHist, cv::gapi::imgproc::GEqHist)
167 static void run(const cv::Mat& in, cv::Mat &out)
169 cv::equalizeHist(in, out);
173 GAPI_OCV_KERNEL(GCPUCanny, cv::gapi::imgproc::GCanny)
175 static void run(const cv::Mat& in, double thr1, double thr2, int apSize, bool l2gradient, cv::Mat &out)
177 cv::Canny(in, out, thr1, thr2, apSize, l2gradient);
181 GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
183 static void run(const cv::Mat& in, cv::Mat &out)
185 cv::cvtColor(in, out, cv::COLOR_RGB2YUV);
189 GAPI_OCV_KERNEL(GCPUYUV2RGB, cv::gapi::imgproc::GYUV2RGB)
191 static void run(const cv::Mat& in, cv::Mat &out)
193 cv::cvtColor(in, out, cv::COLOR_YUV2RGB);
197 GAPI_OCV_KERNEL(GCPUNV12toRGB, cv::gapi::imgproc::GNV12toRGB)
199 static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
201 cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2RGB_NV12);
205 GAPI_OCV_KERNEL(GCPUNV12toBGR, cv::gapi::imgproc::GNV12toBGR)
207 static void run(const cv::Mat& in_y, const cv::Mat& in_uv, cv::Mat &out)
209 cv::cvtColorTwoPlane(in_y, in_uv, out, cv::COLOR_YUV2BGR_NV12);
213 GAPI_OCV_KERNEL(GCPURGB2Lab, cv::gapi::imgproc::GRGB2Lab)
215 static void run(const cv::Mat& in, cv::Mat &out)
217 cv::cvtColor(in, out, cv::COLOR_RGB2Lab);
221 GAPI_OCV_KERNEL(GCPUBGR2LUV, cv::gapi::imgproc::GBGR2LUV)
223 static void run(const cv::Mat& in, cv::Mat &out)
225 cv::cvtColor(in, out, cv::COLOR_BGR2Luv);
229 GAPI_OCV_KERNEL(GCPUBGR2YUV, cv::gapi::imgproc::GBGR2YUV)
231 static void run(const cv::Mat& in, cv::Mat &out)
233 cv::cvtColor(in, out, cv::COLOR_BGR2YUV);
237 GAPI_OCV_KERNEL(GCPULUV2BGR, cv::gapi::imgproc::GLUV2BGR)
239 static void run(const cv::Mat& in, cv::Mat &out)
241 cv::cvtColor(in, out, cv::COLOR_Luv2BGR);
245 GAPI_OCV_KERNEL(GCPUYUV2BGR, cv::gapi::imgproc::GYUV2BGR)
247 static void run(const cv::Mat& in, cv::Mat &out)
249 cv::cvtColor(in, out, cv::COLOR_YUV2BGR);
253 GAPI_OCV_KERNEL(GCPURGB2Gray, cv::gapi::imgproc::GRGB2Gray)
255 static void run(const cv::Mat& in, cv::Mat &out)
257 cv::cvtColor(in, out, cv::COLOR_RGB2GRAY);
261 GAPI_OCV_KERNEL(GCPUBGR2Gray, cv::gapi::imgproc::GBGR2Gray)
263 static void run(const cv::Mat& in, cv::Mat &out)
265 cv::cvtColor(in, out, cv::COLOR_BGR2GRAY);
269 GAPI_OCV_KERNEL(GCPURGB2GrayCustom, cv::gapi::imgproc::GRGB2GrayCustom)
271 static void run(const cv::Mat& in, float rY, float bY, float gY, cv::Mat &out)
274 cv::split(in, planes);
275 out = planes[0]*rY + planes[1]*bY + planes[2]*gY;
279 cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()
281 static auto pkg = cv::gapi::kernels