Path: blob/master/modules/gapi/src/backends/cpu/gcpuimgproc.cpp
16344 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#include "precomp.hpp"89#include "opencv2/gapi/imgproc.hpp"10#include "opencv2/gapi/cpu/imgproc.hpp"11#include "backends/cpu/gcpuimgproc.hpp"1213GAPI_OCV_KERNEL(GCPUSepFilter, cv::gapi::imgproc::GSepFilter)14{15static void run(const cv::Mat& in, int ddepth, const cv::Mat& kernX, const cv::Mat& kernY, const cv::Point& anchor, const cv::Scalar& delta,16int border, const cv::Scalar& bordVal, cv::Mat &out)17{18if( border == cv::BORDER_CONSTANT )19{20cv::Mat temp_in;21int width_add = (kernY.cols - 1) / 2;22int height_add = (kernX.rows - 1) / 2;23cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal);24cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);25cv::sepFilter2D(temp_in(rect), out, ddepth, kernX, kernY, anchor, delta.val[0], border);26}27else28cv::sepFilter2D(in, out, ddepth, kernX, kernY, anchor, delta.val[0], border);29}30};3132GAPI_OCV_KERNEL(GCPUBoxFilter, cv::gapi::imgproc::GBoxFilter)33{34static 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)35{36if( borderType == cv::BORDER_CONSTANT )37{38cv::Mat temp_in;39int width_add = (ksize.width - 1) / 2;40int height_add = (ksize.height - 1) / 2;41cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);42cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);43cv::boxFilter(temp_in(rect), out, ddepth, ksize, anchor, normalize, borderType);44}45else46cv::boxFilter(in, out, ddepth, ksize, anchor, normalize, borderType);47}48};4950GAPI_OCV_KERNEL(GCPUBlur, cv::gapi::imgproc::GBlur)51{52static void run(const cv::Mat& in, const cv::Size& ksize, const cv::Point& anchor, int borderType, const cv::Scalar& bordVal, cv::Mat &out)53{54if( borderType == cv::BORDER_CONSTANT )55{56cv::Mat temp_in;57int width_add = (ksize.width - 1) / 2;58int height_add = (ksize.height - 1) / 2;59cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal);60cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);61cv::blur(temp_in(rect), out, ksize, anchor, borderType);62}63else64cv::blur(in, out, ksize, anchor, borderType);65}66};676869GAPI_OCV_KERNEL(GCPUFilter2D, cv::gapi::imgproc::GFilter2D)70{71static void run(const cv::Mat& in, int ddepth, const cv::Mat& k, const cv::Point& anchor, const cv::Scalar& delta, int border,72const cv::Scalar& bordVal, cv::Mat &out)73{74if( border == cv::BORDER_CONSTANT )75{76cv::Mat temp_in;77int width_add = (k.cols - 1) / 2;78int height_add = (k.rows - 1) / 2;79cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, border, bordVal );80cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);81cv::filter2D(temp_in(rect), out, ddepth, k, anchor, delta.val[0], border);82}83else84cv::filter2D(in, out, ddepth, k, anchor, delta.val[0], border);85}86};8788GAPI_OCV_KERNEL(GCPUGaussBlur, cv::gapi::imgproc::GGaussBlur)89{90static void run(const cv::Mat& in, const cv::Size& ksize, double sigmaX, double sigmaY, int borderType, const cv::Scalar& bordVal, cv::Mat &out)91{92if( borderType == cv::BORDER_CONSTANT )93{94cv::Mat temp_in;95int width_add = (ksize.width - 1) / 2;96int height_add = (ksize.height - 1) / 2;97cv::copyMakeBorder(in, temp_in, height_add, height_add, width_add, width_add, borderType, bordVal );98cv::Rect rect = cv::Rect(height_add, width_add, in.cols, in.rows);99cv::GaussianBlur(temp_in(rect), out, ksize, sigmaX, sigmaY, borderType);100}101else102cv::GaussianBlur(in, out, ksize, sigmaX, sigmaY, borderType);103}104};105106GAPI_OCV_KERNEL(GCPUMedianBlur, cv::gapi::imgproc::GMedianBlur)107{108static void run(const cv::Mat& in, int ksize, cv::Mat &out)109{110cv::medianBlur(in, out, ksize);111}112};113114GAPI_OCV_KERNEL(GCPUErode, cv::gapi::imgproc::GErode)115{116static 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)117{118cv::erode(in, out, kernel, anchor, iterations, borderType, borderValue);119}120};121122GAPI_OCV_KERNEL(GCPUDilate, cv::gapi::imgproc::GDilate)123{124static 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)125{126cv::dilate(in, out, kernel, anchor, iterations, borderType, borderValue);127}128};129130GAPI_OCV_KERNEL(GCPUSobel, cv::gapi::imgproc::GSobel)131{132static void run(const cv::Mat& in, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType,133const cv::Scalar& bordVal, cv::Mat &out)134{135if( borderType == cv::BORDER_CONSTANT )136{137cv::Mat temp_in;138int add = (ksize - 1) / 2;139cv::copyMakeBorder(in, temp_in, add, add, add, add, borderType, bordVal );140cv::Rect rect = cv::Rect(add, add, in.cols, in.rows);141cv::Sobel(temp_in(rect), out, ddepth, dx, dy, ksize, scale, delta, borderType);142}143else144cv::Sobel(in, out, ddepth, dx, dy, ksize, scale, delta, borderType);145}146};147148GAPI_OCV_KERNEL(GCPUEqualizeHist, cv::gapi::imgproc::GEqHist)149{150static void run(const cv::Mat& in, cv::Mat &out)151{152cv::equalizeHist(in, out);153}154};155156GAPI_OCV_KERNEL(GCPUCanny, cv::gapi::imgproc::GCanny)157{158static void run(const cv::Mat& in, double thr1, double thr2, int apSize, bool l2gradient, cv::Mat &out)159{160cv::Canny(in, out, thr1, thr2, apSize, l2gradient);161}162};163164GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)165{166static void run(const cv::Mat& in, cv::Mat &out)167{168cv::cvtColor(in, out, cv::COLOR_RGB2YUV);169}170};171172GAPI_OCV_KERNEL(GCPUYUV2RGB, cv::gapi::imgproc::GYUV2RGB)173{174static void run(const cv::Mat& in, cv::Mat &out)175{176cv::cvtColor(in, out, cv::COLOR_YUV2RGB);177}178};179180GAPI_OCV_KERNEL(GCPURGB2Lab, cv::gapi::imgproc::GRGB2Lab)181{182static void run(const cv::Mat& in, cv::Mat &out)183{184cv::cvtColor(in, out, cv::COLOR_RGB2Lab);185}186};187188GAPI_OCV_KERNEL(GCPUBGR2LUV, cv::gapi::imgproc::GBGR2LUV)189{190static void run(const cv::Mat& in, cv::Mat &out)191{192cv::cvtColor(in, out, cv::COLOR_BGR2Luv);193}194};195196GAPI_OCV_KERNEL(GCPUBGR2YUV, cv::gapi::imgproc::GBGR2YUV)197{198static void run(const cv::Mat& in, cv::Mat &out)199{200cv::cvtColor(in, out, cv::COLOR_BGR2YUV);201}202};203204GAPI_OCV_KERNEL(GCPULUV2BGR, cv::gapi::imgproc::GLUV2BGR)205{206static void run(const cv::Mat& in, cv::Mat &out)207{208cv::cvtColor(in, out, cv::COLOR_Luv2BGR);209}210};211212GAPI_OCV_KERNEL(GCPUYUV2BGR, cv::gapi::imgproc::GYUV2BGR)213{214static void run(const cv::Mat& in, cv::Mat &out)215{216cv::cvtColor(in, out, cv::COLOR_YUV2BGR);217}218};219220GAPI_OCV_KERNEL(GCPURGB2Gray, cv::gapi::imgproc::GRGB2Gray)221{222static void run(const cv::Mat& in, cv::Mat &out)223{224cv::cvtColor(in, out, cv::COLOR_RGB2GRAY);225}226};227228GAPI_OCV_KERNEL(GCPUBGR2Gray, cv::gapi::imgproc::GBGR2Gray)229{230static void run(const cv::Mat& in, cv::Mat &out)231{232cv::cvtColor(in, out, cv::COLOR_BGR2GRAY);233}234};235236GAPI_OCV_KERNEL(GCPURGB2GrayCustom, cv::gapi::imgproc::GRGB2GrayCustom)237{238static void run(const cv::Mat& in, float rY, float bY, float gY, cv::Mat &out)239{240cv::Mat planes[3];241cv::split(in, planes);242out = planes[0]*rY + planes[1]*bY + planes[2]*gY;243}244};245246cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()247{248static auto pkg = cv::gapi::kernels249< GCPUFilter2D250, GCPUSepFilter251, GCPUBoxFilter252, GCPUBlur253, GCPUGaussBlur254, GCPUMedianBlur255, GCPUErode256, GCPUDilate257, GCPUSobel258, GCPUCanny259, GCPUEqualizeHist260, GCPURGB2YUV261, GCPUYUV2RGB262, GCPURGB2Lab263, GCPUBGR2LUV264, GCPUBGR2YUV265, GCPUYUV2BGR266, GCPULUV2BGR267, GCPUBGR2Gray268, GCPURGB2Gray269, GCPURGB2GrayCustom270>();271return pkg;272}273274275