From 290078e0d078722b2f700b6f7e0f80cee31b83e5 Mon Sep 17 00:00:00 2001 From: Dmitry Budnikov Date: Mon, 5 Aug 2019 17:46:28 +0300 Subject: [PATCH] copy kernels --- modules/gapi/include/opencv2/gapi/core.hpp | 19 ++++++++++ modules/gapi/src/api/kernels_core.cpp | 5 +++ modules/gapi/src/backends/cpu/gcpucore.cpp | 9 +++++ .../gapi/src/backends/fluid/gfluidcore.cpp | 35 +++++++++++++++++++ modules/gapi/src/backends/ocl/goclcore.cpp | 9 +++++ modules/gapi/test/common/gapi_core_tests.hpp | 1 + .../gapi/test/common/gapi_core_tests_inl.hpp | 26 ++++++++++++++ modules/gapi/test/cpu/gapi_core_tests_cpu.cpp | 8 +++++ .../gapi/test/cpu/gapi_core_tests_fluid.cpp | 8 +++++ modules/gapi/test/gpu/gapi_core_tests_gpu.cpp | 8 +++++ 10 files changed, 128 insertions(+) diff --git a/modules/gapi/include/opencv2/gapi/core.hpp b/modules/gapi/include/opencv2/gapi/core.hpp index a38d7470a4..a154fb4f65 100644 --- a/modules/gapi/include/opencv2/gapi/core.hpp +++ b/modules/gapi/include/opencv2/gapi/core.hpp @@ -440,6 +440,12 @@ namespace core { } }; + G_TYPED_KERNEL(GCopy, , "org.opencv.core.transform.copy") { + static GMatDesc outMeta(GMatDesc in) { + return in; + } + }; + G_TYPED_KERNEL(GConcatHor, , "org.opencv.imgproc.transform.concatHor") { static GMatDesc outMeta(GMatDesc l, GMatDesc r) { return l.withSizeDelta(+r.size.width, 0); @@ -1497,6 +1503,19 @@ Output matrix must be of the same depth as input one, size is specified by given */ GAPI_EXPORTS GMat crop(const GMat& src, const Rect& rect); +/** @brief Copies a 2D matrix. + +The function copies the matrix. + +Output matrix must be of the same size and depth as input one. + +@note Function textual ID is "org.opencv.core.transform.copy" + +@param src input matrix. +@sa crop +*/ +GAPI_EXPORTS GMat copy(const GMat& src); + /** @brief Applies horizontal concatenation to given matrices. The function horizontally concatenates two GMat matrices (with the same number of rows). diff --git a/modules/gapi/src/api/kernels_core.cpp b/modules/gapi/src/api/kernels_core.cpp index b7bfc6b4c5..ef15c93275 100644 --- a/modules/gapi/src/api/kernels_core.cpp +++ b/modules/gapi/src/api/kernels_core.cpp @@ -323,6 +323,11 @@ GMat crop(const GMat& src, const Rect& rect) return core::GCrop::on(src, rect); } +GMat copy(const GMat& src) +{ + return core::GCopy::on(src); +} + GMat concatHor(const GMat& src1, const GMat& src2) { return core::GConcatHor::on(src1, src2); diff --git a/modules/gapi/src/backends/cpu/gcpucore.cpp b/modules/gapi/src/backends/cpu/gcpucore.cpp index 1c1ac581a8..2762a11eb9 100644 --- a/modules/gapi/src/backends/cpu/gcpucore.cpp +++ b/modules/gapi/src/backends/cpu/gcpucore.cpp @@ -501,6 +501,14 @@ GAPI_OCV_KERNEL(GCPUCrop, cv::gapi::core::GCrop) } }; +GAPI_OCV_KERNEL(GCPUCopy, cv::gapi::core::GCopy) +{ + static void run(const cv::Mat& in, cv::Mat& out) + { + cv::Mat(in).copyTo(out); + } +}; + GAPI_OCV_KERNEL(GCPUConcatHor, cv::gapi::core::GConcatHor) { static void run(const cv::Mat& in1, const cv::Mat& in2, cv::Mat& out) @@ -611,6 +619,7 @@ cv::gapi::GKernelPackage cv::gapi::core::cpu::kernels() , GCPURemap , GCPUFlip , GCPUCrop + , GCPUCopy , GCPUConcatHor , GCPUConcatVert , GCPULUT diff --git a/modules/gapi/src/backends/fluid/gfluidcore.cpp b/modules/gapi/src/backends/fluid/gfluidcore.cpp index 49d6d2e233..c30104b321 100644 --- a/modules/gapi/src/backends/fluid/gfluidcore.cpp +++ b/modules/gapi/src/backends/fluid/gfluidcore.cpp @@ -2117,6 +2117,40 @@ GAPI_FLUID_KERNEL(GFluidSqrt, cv::gapi::core::GSqrt, false) } }; +GAPI_FLUID_KERNEL(GFluidCopy, cv::gapi::core::GCopy, false) +{ + static const int Window = 1; + + static void run(const View &src, Buffer &dst) + { + const auto *in = src.InLine(0); + auto *out = dst.OutLine(); + + GAPI_DbgAssert(dst.length() == src.length()); + GAPI_DbgAssert(dst.meta().chan == src.meta().chan); + GAPI_DbgAssert(dst.meta().depth == src.meta().depth); + + int width = src.length(); + int elem_size = CV_ELEM_SIZE(CV_MAKETYPE(src.meta().depth, src.meta().chan)); + + int w = 0; // cycle counter + + #if CV_SIMD128 + for (; w <= width*elem_size-16; w+=16) + { + v_uint8x16 a; + a = v_load(&in[w]); + v_store(&out[w], a); + } + #endif + + for (; w < width*elem_size; w++) + { + out[w] = in[w]; + } + } +}; + } // namespace fliud } // namespace gapi } // namespace cv @@ -2172,6 +2206,7 @@ cv::gapi::GKernelPackage cv::gapi::core::fluid::kernels() ,GFluidInRange ,GFluidResize ,GFluidSqrt + ,GFluidCopy #if 0 ,GFluidMean -- not fluid ,GFluidSum -- not fluid diff --git a/modules/gapi/src/backends/ocl/goclcore.cpp b/modules/gapi/src/backends/ocl/goclcore.cpp index 9741100219..ea46d4e309 100644 --- a/modules/gapi/src/backends/ocl/goclcore.cpp +++ b/modules/gapi/src/backends/ocl/goclcore.cpp @@ -482,6 +482,14 @@ GAPI_OCL_KERNEL(GOCLCrop, cv::gapi::core::GCrop) } }; +GAPI_OCL_KERNEL(GOCLCopy, cv::gapi::core::GCopy) +{ + static void run(const cv::UMat& in, cv::UMat& out) + { + cv::UMat(in).copyTo(out); + } +}; + GAPI_OCL_KERNEL(GOCLConcatHor, cv::gapi::core::GConcatHor) { static void run(const cv::UMat& in1, const cv::UMat& in2, cv::UMat& out) @@ -573,6 +581,7 @@ cv::gapi::GKernelPackage cv::gapi::core::ocl::kernels() , GOCLRemap , GOCLFlip , GOCLCrop + , GOCLCopy , GOCLConcatHor , GOCLConcatVert , GOCLLUT diff --git a/modules/gapi/test/common/gapi_core_tests.hpp b/modules/gapi/test/common/gapi_core_tests.hpp index 5644c19e3d..b1593822f6 100644 --- a/modules/gapi/test/common/gapi_core_tests.hpp +++ b/modules/gapi/test/common/gapi_core_tests.hpp @@ -100,6 +100,7 @@ GAPI_TEST_FIXTURE(Merge4Test, initMatsRandU, <>, 0) GAPI_TEST_FIXTURE(RemapTest, initMatrixRandU, <>, 0) GAPI_TEST_FIXTURE(FlipTest, initMatrixRandU, FIXTURE_API(int), 1, flipCode) GAPI_TEST_FIXTURE(CropTest, initMatrixRandU, FIXTURE_API(cv::Rect), 1, rect_to) +GAPI_TEST_FIXTURE(CopyTest, initMatrixRandU, <>, 0) GAPI_TEST_FIXTURE(ConcatHorTest, initNothing, <>, 0) GAPI_TEST_FIXTURE(ConcatVertTest, initNothing, <>, 0) GAPI_TEST_FIXTURE(ConcatVertVecTest, initNothing, <>, 0) diff --git a/modules/gapi/test/common/gapi_core_tests_inl.hpp b/modules/gapi/test/common/gapi_core_tests_inl.hpp index 5b22ef98b5..199cab2181 100644 --- a/modules/gapi/test/common/gapi_core_tests_inl.hpp +++ b/modules/gapi/test/common/gapi_core_tests_inl.hpp @@ -1002,6 +1002,32 @@ TEST_P(CropTest, AccuracyTest) } } +TEST_P(CopyTest, AccuracyTest) +{ + cv::Size sz_out = sz; + if (dtype != -1) + { + out_mat_gapi = cv::Mat(sz_out, dtype); + out_mat_ocv = cv::Mat(sz_out, dtype); + } + + // G-API code ////////////////////////////////////////////////////////////// + cv::GMat in; + auto out = cv::gapi::copy(in); + + cv::GComputation c(in, out); + c.apply(in_mat1, out_mat_gapi, getCompileArgs()); + // OpenCV code ///////////////////////////////////////////////////////////// + { + cv::Mat(in_mat1).copyTo(out_mat_ocv); + } + // Comparison ////////////////////////////////////////////////////////////// + { + EXPECT_EQ(0, cv::countNonZero(out_mat_ocv != out_mat_gapi)); + EXPECT_EQ(out_mat_gapi.size(), sz_out); + } +} + TEST_P(ConcatHorTest, AccuracyTest) { cv::Size sz_out = sz; diff --git a/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp b/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp index 665d525bc1..658bd1b53d 100644 --- a/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp +++ b/modules/gapi/test/cpu/gapi_core_tests_cpu.cpp @@ -365,6 +365,14 @@ INSTANTIATE_TEST_CASE_P(CropTestCPU, CropTest, Values(CORE_CPU), Values(cv::Rect(10, 8, 20, 35), cv::Rect(4, 10, 37, 50)))); +INSTANTIATE_TEST_CASE_P(CopyTestCPU, CopyTest, + Combine(Values( CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1 ), + Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(-1), + Values(CORE_CPU))); + INSTANTIATE_TEST_CASE_P(LUTTestCPU, LUTTest, Combine(Values(CV_8UC1, CV_8UC3), Values(cv::Size(1280, 720), diff --git a/modules/gapi/test/cpu/gapi_core_tests_fluid.cpp b/modules/gapi/test/cpu/gapi_core_tests_fluid.cpp index be158d029f..6ec9e0c2a0 100644 --- a/modules/gapi/test/cpu/gapi_core_tests_fluid.cpp +++ b/modules/gapi/test/cpu/gapi_core_tests_fluid.cpp @@ -275,6 +275,14 @@ INSTANTIATE_TEST_CASE_P(ReInitOutTestFluid, ReInitOutTest, Values(cv::Size(640, 400), cv::Size(10, 480)))); +INSTANTIATE_TEST_CASE_P(CopyTestFluid, CopyTest, + Combine(Values( CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1 ), + Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(-1), + Values(CORE_FLUID))); + //---------------------------------------------------------------------- // FIXME: Clean-up test configurations which are enabled already #if 0 diff --git a/modules/gapi/test/gpu/gapi_core_tests_gpu.cpp b/modules/gapi/test/gpu/gapi_core_tests_gpu.cpp index 0f67688f83..6d5d50ff0e 100644 --- a/modules/gapi/test/gpu/gapi_core_tests_gpu.cpp +++ b/modules/gapi/test/gpu/gapi_core_tests_gpu.cpp @@ -336,6 +336,14 @@ INSTANTIATE_TEST_CASE_P(CropTestGPU, CropTest, Values(CORE_GPU), Values(cv::Rect(10, 8, 20, 35), cv::Rect(4, 10, 37, 50)))); +INSTANTIATE_TEST_CASE_P(CopyTestGPU, CopyTest, + Combine(Values( CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1 ), + Values(cv::Size(1280, 720), + cv::Size(640, 480), + cv::Size(128, 128)), + Values(-1), + Values(CORE_GPU))); + INSTANTIATE_TEST_CASE_P(LUTTestGPU, LUTTest, Combine(Values(CV_8UC1, CV_8UC3), Values(cv::Size(1280, 720),