From a0a3b8b56df52c07f436c8cb6e7c5dad2b51bd98 Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Thu, 31 Oct 2013 16:25:13 +0400 Subject: [PATCH] Some tests for UMat --- modules/core/test/test_precomp.hpp | 123 ++++++++ modules/core/test/test_umat.cpp | 621 +++++++++++++++++++++++++------------ 2 files changed, 554 insertions(+), 190 deletions(-) diff --git a/modules/core/test/test_precomp.hpp b/modules/core/test/test_precomp.hpp index d981cea..ffd264f 100644 --- a/modules/core/test/test_precomp.hpp +++ b/modules/core/test/test_precomp.hpp @@ -15,4 +15,127 @@ #include "opencv2/core/private.hpp" +#define MWIDTH 256 +#define MHEIGHT 256 + +#define MIN_VALUE 171 +#define MAX_VALUE 357 + +#define RNG_SEED 123456 + +template +struct TSTestWithParam : public ::testing::TestWithParam +{ + cv::RNG rng; + + TSTestWithParam() + { + rng = cv::RNG(RNG_SEED); + } + + int randomInt(int minVal, int maxVal) + { + return rng.uniform(minVal, maxVal); + } + + double randomDouble(double minVal, double maxVal) + { + return rng.uniform(minVal, maxVal); + } + + double randomDoubleLog(double minVal, double maxVal) + { + double logMin = log((double)minVal + 1); + double logMax = log((double)maxVal + 1); + double pow = rng.uniform(logMin, logMax); + double v = exp(pow) - 1; + CV_Assert(v >= minVal && (v < maxVal || (v == minVal && v == maxVal))); + return v; + } + + cv::Size randomSize(int minVal, int maxVal) + { +#if 1 + return cv::Size((int)randomDoubleLog(minVal, maxVal), (int)randomDoubleLog(minVal, maxVal)); +#else + return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal)); +#endif + } + + cv::Size randomSize(int minValX, int maxValX, int minValY, int maxValY) + { +#if 1 + return cv::Size(randomDoubleLog(minValX, maxValX), randomDoubleLog(minValY, maxValY)); +#else + return cv::Size(randomInt(minVal, maxVal), randomInt(minVal, maxVal)); +#endif + } + + cv::Scalar randomScalar(double minVal, double maxVal) + { + return cv::Scalar(randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal), randomDouble(minVal, maxVal)); + } + + cv::Mat randomMat(cv::Size size, int type, double minVal, double maxVal, bool useRoi = false) + { + cv::RNG dataRng(rng.next()); + return cvtest::randomMat(dataRng, size, type, minVal, maxVal, useRoi); + } + +}; + +#define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > > + +#define GET_PARAM(k) std::tr1::get< k >(GetParam()) + +#define UMAT_TEST_CHANNELS testing::Values(1, 2, 3, 4/*, 5*/) + +#define UMAT_TEST_SIZES testing::Values(cv::Size(1,1), cv::Size(1,128), cv::Size(128,1), cv::Size(128, 128), cv::Size(59, 113), cv::Size(640,480), cv::Size(751,373), cv::Size(2000, 2000)) + +#define UMAT_TEST_DEPTH testing::Values(CV_8S, CV_8U, CV_16S, CV_16U, CV_32F, CV_32S, CV_64F) + +# define CORE_TEST_P(test_case_name, test_name) \ + class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : \ + public test_case_name { \ + public: \ + GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() { } \ + virtual void TestBody(); \ + void CoreTestBody(); \ + private: \ + static int AddToRegistry() \ + { \ + ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \ + GetTestCasePatternHolder(\ + #test_case_name, __FILE__, __LINE__)->AddTestPattern(\ + #test_case_name, \ + #test_name, \ + new ::testing::internal::TestMetaFactory< \ + GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>()); \ + return 0; \ + } \ + \ + static int gtest_registering_dummy_; \ + GTEST_DISALLOW_COPY_AND_ASSIGN_(\ + GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \ + }; \ + \ + int GTEST_TEST_CLASS_NAME_(test_case_name, \ + test_name)::gtest_registering_dummy_ = \ + GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \ + \ + void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() \ + { \ + try \ + { \ + CoreTestBody(); \ + } \ + catch (...) \ + { \ + std::cout << "Something wrong in CoreTestBody running" << std::endl; \ + throw; \ + } \ + } \ + \ + void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::CoreTestBody() + #endif diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index 765a631..e6fcbd1 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -40,257 +40,456 @@ //M*/ #include "test_precomp.hpp" - -#include -#include #include "opencv2/core/ocl.hpp" +using namespace cvtest; +using namespace testing; using namespace cv; -using namespace std; -class CV_UMatTest : - public cvtest::BaseTest -{ -public: - CV_UMatTest() {} - ~CV_UMatTest() {} -protected: - void run(int); +#define EXPECT_MAT_NEAR(mat1, mat2, eps) \ +{ \ + ASSERT_EQ(mat1.type(), mat2.type()); \ + ASSERT_EQ(mat1.size(), mat2.size()); \ + EXPECT_LE(cv::norm(mat1, mat2), eps); \ +}\ - struct test_excep - { - test_excep(const string& _s=string("")) : s(_s) { } - string s; - }; +////////////////////////////////////////////////////////////// Basic Tests ///////////////////////////////////////////////////////////////////// - bool TestUMat(); +PARAM_TEST_CASE(UMatBasicTests, int, int, Size, bool) +{ + Mat a, b, roi_a, roi_b; + UMat ua, ub, roi_ua, roi_ub; + int type; + int depth; + int cn; + Size size; + bool useRoi; + Size roi_size; + Rect roi; + virtual void SetUp() + { + depth = GET_PARAM(0); + cn = GET_PARAM(1); + size = GET_PARAM(2); + useRoi = GET_PARAM(3); + type = CV_MAKE_TYPE(depth, cn); + a = randomMat(size, type, -100, 100); + b = randomMat(size, type, -100, 100); + a.copyTo(ua); + b.copyTo(ub); + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + roi = Rect(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + } +}; - void checkDiff(const Mat& m1, const Mat& m2, const string& s) +CORE_TEST_P(UMatBasicTests, createUMat) +{ + if(useRoi) { - if (norm(m1, m2, NORM_INF) != 0) - throw test_excep(s); + ua = UMat(ua, roi); } - void checkDiffF(const Mat& m1, const Mat& m2, const string& s) + int dims = randomInt(2,6); + int _sz[CV_MAX_DIM]; + for( int i = 0; i 1e-5) - throw test_excep(s); + _sz[i] = randomInt(1,50); } -}; - -#define STR(a) STR2(a) -#define STR2(a) #a + int *sz = _sz; + int new_depth = randomInt(CV_8S, CV_64F); + int new_cn = randomInt(1,4); + ua.create(dims, sz, CV_MAKE_TYPE(new_depth, new_cn)); -#define CHECK_DIFF(a, b) checkDiff(a, b, "(" #a ") != (" #b ") at l." STR(__LINE__)) -#define CHECK_DIFF_FLT(a, b) checkDiffF(a, b, "(" #a ") !=(eps) (" #b ") at l." STR(__LINE__)) + for(int i = 0; iprintf(cvtest::TS::LOG, "%s\n", e.s.c_str()); - ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); - return false; + depth = GET_PARAM(0); + cn = GET_PARAM(1); + size = GET_PARAM(2); + type = CV_MAKE_TYPE(depth, cn); } - return true; -} +}; -void CV_UMatTest::run( int /* start_from */) +CORE_TEST_P(UMatTestRoi, createRoi) { - printf("Use OpenCL: %s\nHave OpenCL: %s\n", - ocl::useOpenCL() ? "TRUE" : "FALSE", - ocl::haveOpenCL() ? "TRUE" : "FALSE" ); + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + a = randomMat(size, type, -100, 100); + Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + roi_a = Mat(a, roi); + a.copyTo(ua); + roi_ua = UMat(ua, roi); + + EXPECT_MAT_NEAR(roi_a, roi_ua, 0); +} - if (!TestUMat()) - return; +CORE_TEST_P(UMatTestRoi, locateRoi) +{ + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + a = randomMat(size, type, -100, 100); + Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + roi_a = Mat(a, roi); + a.copyTo(ua); + roi_ua = UMat(ua,roi); + Size sz, usz; + Point p, up; + roi_a.locateROI(sz, p); + roi_ua.locateROI(usz, up); + ASSERT_EQ(sz, usz); + ASSERT_EQ(p, up); +} - ts->set_failed_test_info(cvtest::TS::OK); +CORE_TEST_P(UMatTestRoi, adjustRoi) +{ + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + a = randomMat(size, type, -100, 100); + Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + a.copyTo(ua); + roi_ua = UMat( ua, roi); + int adjLeft = randomInt(-(roi_ua.cols/2), (size.width-1)/2); + int adjRight = randomInt(-(roi_ua.cols/2), (size.width-1)/2); + int adjTop = randomInt(-(roi_ua.rows/2), (size.height-1)/2); + int adjBot = randomInt(-(roi_ua.rows/2), (size.height-1)/2); + roi_ua.adjustROI(adjTop, adjBot, adjLeft, adjRight); + roi_shift_x = max(0, roi.x-adjLeft); + roi_shift_y = max(0, roi.y-adjTop); + Rect new_roi( roi_shift_x, roi_shift_y, min(roi.width+adjRight+adjLeft, size.width-roi_shift_x), min(roi.height+adjBot+adjTop, size.height-roi_shift_y) ); + UMat test_roi = UMat(ua, new_roi); + EXPECT_MAT_NEAR(roi_ua, test_roi, 0); } -TEST(Core_UMat, base) { CV_UMatTest test; test.safe_run(); } +INSTANTIATE_TEST_CASE_P(UMat, UMatTestRoi, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES )); + +/////////////////////////////////////////////////////////////// Size //////////////////////////////////////////////////////////////////// -TEST(Core_UMat, getUMat) +PARAM_TEST_CASE(UMatTestSizeOperations, int, int, Size, bool) { + Mat a, b, roi_a, roi_b; + UMat ua, ub, roi_ua, roi_ub; + int type; + int depth; + int cn; + Size size; + Size roi_size; + bool useRoi; + virtual void SetUp() { - int a[3] = { 1, 2, 3 }; - Mat m = Mat(1, 1, CV_32SC3, a); - UMat u = m.getUMat(ACCESS_READ); - EXPECT_NE((void*)NULL, u.u); + depth = GET_PARAM(0); + cn = GET_PARAM(1); + size = GET_PARAM(2); + useRoi = GET_PARAM(3); + type = CV_MAKE_TYPE(depth, cn); } +}; +CORE_TEST_P(UMatTestSizeOperations, copySize) +{ + Size s = randomSize(1,300); + a = randomMat(size, type, -100, 100); + b = randomMat(s, type, -100, 100); + a.copyTo(ua); + b.copyTo(ub); + if(useRoi) { - Mat m(10, 10, CV_8UC1), ref; - for (int y = 0; y < m.rows; ++y) - { - uchar * const ptr = m.ptr(y); - for (int x = 0; x < m.cols; ++x) - ptr[x] = (uchar)(x + y * 2); - } + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + ua = UMat(ua,roi); + + roi_shift_x = randomInt(0, s.width-1); + roi_shift_y = randomInt(0, s.height-1); + roi_size = Size(s.width - roi_shift_x, s.height - roi_shift_y); + roi = Rect(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + ub = UMat(ub, roi); + } + ua.copySize(ub); + ASSERT_EQ(ua.size, ub.size); +} - ref = m.clone(); - Rect r(1, 1, 8, 8); - ref(r).setTo(17); +INSTANTIATE_TEST_CASE_P(UMat, UMatTestSizeOperations, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() )); - { - UMat um = m(r).getUMat(ACCESS_WRITE); - um.setTo(17); - } +///////////////////////////////////////////////////////////////// UMat operations //////////////////////////////////////////////////////////////////////////// - double err = norm(m, ref, NORM_INF); - if (err > 0) - { - std::cout << "m: " << std::endl << m << std::endl; - std::cout << "ref: " << std::endl << ref << std::endl; - } - EXPECT_EQ(0., err); +PARAM_TEST_CASE(UMatTestUMatOperations, int, int, Size, bool) +{ + Mat a, b; + UMat ua, ub; + int type; + int depth; + int cn; + Size size; + Size roi_size; + bool useRoi; + virtual void SetUp() + { + depth = GET_PARAM(0); + cn = GET_PARAM(1); + size = GET_PARAM(2); + useRoi = GET_PARAM(3); + type = CV_MAKE_TYPE(depth, cn); } -} +}; -TEST(UMat, Sync) +CORE_TEST_P(UMatTestUMatOperations, diag) { - UMat um(10, 10, CV_8UC1); - + a = randomMat(size, type, -100, 100); + a.copyTo(ua); + Mat new_diag; + if(useRoi) { - Mat m = um.getMat(ACCESS_WRITE); - m.setTo(cv::Scalar::all(17)); + int roi_shift_x = randomInt(0, size.width-1); + int roi_shift_y = randomInt(0, size.height-1); + roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); + Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); + ua = UMat(ua,roi); + a = Mat(a, roi); } - - um.setTo(cv::Scalar::all(19)); - - EXPECT_EQ(0, cv::norm(um.getMat(ACCESS_READ), cv::Mat(um.size(), um.type(), 19), NORM_INF)); + int n = randomInt(0, ua.cols-1); + ub = ua.diag(n); + b = a.diag(n); + EXPECT_MAT_NEAR(b, ub, 0); + new_diag = randomMat(Size(ua.rows, 1), type, -100, 100); + new_diag.copyTo(ub); + ua = cv::UMat::diag(ub); + EXPECT_MAT_NEAR(ua.diag(), new_diag.t(), 0); } -#define EXPECT_MAT_NEAR(m1, m2) ASSERT_EQ(0, cv::norm(m1, m1, cv::NORM_INF)) - -TEST(UMat, setOpenCL) +CORE_TEST_P(UMatTestUMatOperations, dotUMat) { - // save the current state - bool useOCL = ocl::useOpenCL(); - - Mat m = (Mat_(3,3)<<0,1,2,3,4,5,6,7,8); - - ocl::setUseOpenCL(true); - UMat um1; - m.copyTo(um1); - - ocl::setUseOpenCL(false); - UMat um2; - m.copyTo(um2); - - ocl::setUseOpenCL(true); - countNonZero(um1); - countNonZero(um2); - - um1.copyTo(um2); - EXPECT_MAT_NEAR(um1, um2); - EXPECT_MAT_NEAR(um1, m); - um2.copyTo(um1); - EXPECT_MAT_NEAR(um1, m); - EXPECT_MAT_NEAR(um1, um2); - - ocl::setUseOpenCL(false); - countNonZero(um1); - countNonZero(um2); + a = randomMat(size, type, -100, 100); + b = randomMat(size, type, -100, 100); + a.copyTo(ua); + b.copyTo(ub); + //ASSERT_EQ(ua.dot(ub), a.dot(b)); UMat::dot doesn't compiles +} - um1.copyTo(um2); - EXPECT_MAT_NEAR(um1, um2); - EXPECT_MAT_NEAR(um1, m); - um2.copyTo(um1); - EXPECT_MAT_NEAR(um1, um2); - EXPECT_MAT_NEAR(um1, m); +INSTANTIATE_TEST_CASE_P(UMat, UMatTestUMatOperations, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() )); - // reset state to the previous one - ocl::setUseOpenCL(useOCL); -} +///////////////////////////////////////////////////////////////// OpenCL //////////////////////////////////////////////////////////////////////////// TEST(UMat, BufferPoolGrowing) { @@ -300,7 +499,7 @@ TEST(UMat, BufferPoolGrowing) const int ITERATIONS = 200; #endif const Size sz(1920, 1080); - BufferPoolController* c = ocl::getOpenCLAllocator()->getBufferPoolController(); + BufferPoolController* c = cv::ocl::getOpenCLAllocator()->getBufferPoolController(); if (c) { size_t oldMaxReservedSize = c->getMaxReservedSize(); @@ -319,3 +518,45 @@ TEST(UMat, BufferPoolGrowing) std::cout << "Skipped, no OpenCL" << std::endl; } } + +TEST(UMat, setOpenCL) +{ + // save the current state + bool useOCL = cv::ocl::useOpenCL(); + + Mat m = (Mat_(3,3)<<0,1,2,3,4,5,6,7,8); + + cv::ocl::setUseOpenCL(true); + UMat um1; + m.copyTo(um1); + + cv::ocl::setUseOpenCL(false); + UMat um2; + m.copyTo(um2); + + cv::ocl::setUseOpenCL(true); + countNonZero(um1); + countNonZero(um2); + + um1.copyTo(um2); + EXPECT_MAT_NEAR(um1, um2, 0); + EXPECT_MAT_NEAR(um1, m, 0); + um2.copyTo(um1); + EXPECT_MAT_NEAR(um1, m, 0); + EXPECT_MAT_NEAR(um1, um2, 0); + + cv::ocl::setUseOpenCL(false); + countNonZero(um1); + countNonZero(um2); + + um1.copyTo(um2); + EXPECT_MAT_NEAR(um1, um2, 0); + EXPECT_MAT_NEAR(um1, m, 0); + um2.copyTo(um1); + EXPECT_MAT_NEAR(um1, um2, 0); + EXPECT_MAT_NEAR(um1, m, 0); + + // reset state to the previous one + cv::ocl::setUseOpenCL(useOCL); +} + -- 2.7.4