From 48a084c236c261668cf9cbd2b583a9b6d4bf3583 Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Mon, 27 Jan 2014 18:34:19 +0400 Subject: [PATCH] OpenCL version of cv::buildPyramid --- modules/core/include/opencv2/core/mat.hpp | 1 + modules/core/src/matrix.cpp | 17 ++++++++++++ modules/imgproc/perf/opencl/perf_pyramid.cpp | 29 ++++++++++++++++++++ modules/imgproc/src/pyramids.cpp | 10 +++++++ 4 files changed, 57 insertions(+) diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 6c09efdf83..dcbac6b437 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -207,6 +207,7 @@ public: virtual bool fixedType() const; virtual bool needed() const; virtual Mat& getMatRef(int i=-1) const; + virtual UMat& getUMatRef(int i=-1) const; virtual cuda::GpuMat& getGpuMatRef() const; virtual ogl::Buffer& getOGlBufferRef() const; virtual cuda::CudaMem& getCudaMemRef() const; diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index c74e854f10..0f15ac1d6e 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -2484,6 +2484,23 @@ Mat& _OutputArray::getMatRef(int i) const } } +UMat& _OutputArray::getUMatRef(int i) const +{ + int k = kind(); + if( i < 0 ) + { + CV_Assert( k == UMAT ); + return *(UMat*)obj; + } + else + { + CV_Assert( k == STD_VECTOR_UMAT ); + std::vector& v = *(std::vector*)obj; + CV_Assert( i < (int)v.size() ); + return v[i]; + } +} + cuda::GpuMat& _OutputArray::getGpuMatRef() const { int k = kind(); diff --git a/modules/imgproc/perf/opencl/perf_pyramid.cpp b/modules/imgproc/perf/opencl/perf_pyramid.cpp index 4beba22801..55bb0679bb 100644 --- a/modules/imgproc/perf/opencl/perf_pyramid.cpp +++ b/modules/imgproc/perf/opencl/perf_pyramid.cpp @@ -100,6 +100,35 @@ OCL_PERF_TEST_P(PyrUpFixture, PyrUp, SANITY_CHECK(dst, eps); } +///////////// buildPyramid //////////////////////// + +typedef Size_MatType BuildPyramidFixture; + +OCL_PERF_TEST_P(BuildPyramidFixture, BuildPyramid, + ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES)) +{ + const Size_MatType_t params = GetParam(); + const Size srcSize = get<0>(params); + const int type = get<1>(params), maxLevel = 5; + const double eps = CV_MAT_DEPTH(type) <= CV_32S ? 1 : 1e-5; + + checkDeviceMaxMemoryAllocSize(srcSize, type); + + std::vector dst(maxLevel); + UMat src(srcSize, type); + declare.in(src, WARMUP_RNG); + + OCL_TEST_CYCLE() cv::buildPyramid(src, dst, maxLevel); + + UMat dst0 = dst[0], dst1 = dst[1], dst2 = dst[2], dst3 = dst[3], dst4 = dst[4]; + + SANITY_CHECK(dst0, eps); + SANITY_CHECK(dst1, eps); + SANITY_CHECK(dst2, eps); + SANITY_CHECK(dst3, eps); + SANITY_CHECK(dst4, eps); +} + } } // namespace cvtest::ocl #endif // HAVE_OPENCL diff --git a/modules/imgproc/src/pyramids.cpp b/modules/imgproc/src/pyramids.cpp index 6802e9eebf..24d7ef950b 100644 --- a/modules/imgproc/src/pyramids.cpp +++ b/modules/imgproc/src/pyramids.cpp @@ -556,6 +556,16 @@ void cv::pyrUp( InputArray _src, OutputArray _dst, const Size& _dsz, int borderT void cv::buildPyramid( InputArray _src, OutputArrayOfArrays _dst, int maxlevel, int borderType ) { + if (_src.dims() <= 2 && _dst.isUMatVector()) + { + UMat src = _src.getUMat(); + _dst.create( maxlevel + 1, 1, 0 ); + _dst.getUMatRef(0) = src; + for( int i = 1; i <= maxlevel; i++ ) + pyrDown( _dst.getUMatRef(i-1), _dst.getUMatRef(i), Size(), borderType ); + return; + } + Mat src = _src.getMat(); _dst.create( maxlevel + 1, 1, 0 ); _dst.getMatRef(0) = src;