Some tests for UMat

This commit is contained in:
Konstantin Matskevich 2013-10-31 16:25:13 +04:00
parent 863e9c699a
commit a0a3b8b56d
2 changed files with 575 additions and 211 deletions

View File

@ -15,4 +15,127 @@
#include "opencv2/core/private.hpp" #include "opencv2/core/private.hpp"
#define MWIDTH 256
#define MHEIGHT 256
#define MIN_VALUE 171
#define MAX_VALUE 357
#define RNG_SEED 123456
template <typename T>
struct TSTestWithParam : public ::testing::TestWithParam<T>
{
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>(\
#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 #endif

View File

@ -40,257 +40,456 @@
//M*/ //M*/
#include "test_precomp.hpp" #include "test_precomp.hpp"
#include <string>
#include <iostream>
#include "opencv2/core/ocl.hpp" #include "opencv2/core/ocl.hpp"
using namespace cvtest;
using namespace testing;
using namespace cv; using namespace cv;
using namespace std;
class CV_UMatTest : #define EXPECT_MAT_NEAR(mat1, mat2, eps) \
public cvtest::BaseTest { \
ASSERT_EQ(mat1.type(), mat2.type()); \
ASSERT_EQ(mat1.size(), mat2.size()); \
EXPECT_LE(cv::norm(mat1, mat2), eps); \
}\
////////////////////////////////////////////////////////////// Basic Tests /////////////////////////////////////////////////////////////////////
PARAM_TEST_CASE(UMatBasicTests, int, int, Size, bool)
{ {
public: Mat a, b, roi_a, roi_b;
CV_UMatTest() {} UMat ua, ub, roi_ua, roi_ub;
~CV_UMatTest() {} int type;
protected: int depth;
void run(int); int cn;
Size size;
struct test_excep bool useRoi;
Size roi_size;
Rect roi;
virtual void SetUp()
{ {
test_excep(const string& _s=string("")) : s(_s) { } depth = GET_PARAM(0);
string s; cn = GET_PARAM(1);
}; size = GET_PARAM(2);
useRoi = GET_PARAM(3);
bool TestUMat(); type = CV_MAKE_TYPE(depth, cn);
a = randomMat(size, type, -100, 100);
void checkDiff(const Mat& m1, const Mat& m2, const string& s) b = randomMat(size, type, -100, 100);
{ a.copyTo(ua);
if (norm(m1, m2, NORM_INF) != 0) b.copyTo(ub);
throw test_excep(s); int roi_shift_x = randomInt(0, size.width-1);
} int roi_shift_y = randomInt(0, size.height-1);
void checkDiffF(const Mat& m1, const Mat& m2, const string& s) 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);
if (norm(m1, m2, NORM_INF) > 1e-5)
throw test_excep(s);
} }
}; };
#define STR(a) STR2(a) CORE_TEST_P(UMatBasicTests, createUMat)
#define STR2(a) #a
#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__))
bool CV_UMatTest::TestUMat()
{ {
try if(useRoi)
{ {
Mat a(100, 100, CV_16SC2), b, c; ua = UMat(ua, roi);
randu(a, Scalar::all(-100), Scalar::all(100)); }
Rect roi(1, 3, 5, 4); int dims = randomInt(2,6);
Mat ra(a, roi), rb, rc, rc0; int _sz[CV_MAX_DIM];
UMat ua, ura, ub, urb, uc, urc; for( int i = 0; i<dims; i++)
{
_sz[i] = randomInt(1,50);
}
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));
for(int i = 0; i<dims; i++)
{
ASSERT_EQ(ua.size[i], sz[i]);
}
ASSERT_EQ(ua.dims, dims);
ASSERT_EQ(ua.type(), CV_MAKE_TYPE(new_depth, new_cn) );
Size new_size = randomSize(1, 1000);
ua.create(new_size, CV_MAKE_TYPE(new_depth, new_cn) );
ASSERT_EQ( ua.size(), new_size);
ASSERT_EQ(ua.type(), CV_MAKE_TYPE(new_depth, new_cn) );
ASSERT_EQ( ua.dims, 2);
}
CORE_TEST_P(UMatBasicTests, swap)
{
if(useRoi)
{
ua = UMat(ua,roi);
ub = UMat(ub,roi);
}
UMat uc = ua, ud = ub;
swap(ua,ub);
EXPECT_MAT_NEAR(ub,uc, 0);
EXPECT_MAT_NEAR(ud, ua, 0);
}
CORE_TEST_P(UMatBasicTests, base)
{
if(useRoi)
{
ua = UMat(ua,roi);
}
ub = ua.clone();
EXPECT_MAT_NEAR(ub,ua,0);
ASSERT_EQ(ua.channels(), cn);
ASSERT_EQ(ua.depth(), depth);
ASSERT_EQ(ua.type(), type);
ASSERT_EQ(ua.elemSize(), a.elemSize());
ASSERT_EQ(ua.elemSize1(), a.elemSize1());
ASSERT_EQ(ub.empty(), ub.cols*ub.rows == 0);
ub.release();
ASSERT_TRUE( ub.empty() );
if(useRoi && a.size() != ua.size())
{
ASSERT_EQ(ua.isSubmatrix(), true);
}
else
{
ASSERT_EQ(ua.isSubmatrix(), false);
}
int dims = randomInt(2,6);
int sz[CV_MAX_DIM];
size_t total = 1;
for(int i = 0; i<dims; i++)
{
sz[i] = randomInt(1,45);
total *= (size_t)sz[i];
}
int new_type = CV_MAKE_TYPE(randomInt(CV_8S,CV_64F),randomInt(1,4));
ub = UMat(dims, sz, new_type);
ASSERT_EQ(ub.total(), total);
}
CORE_TEST_P(UMatBasicTests, copyTo)
{
if(useRoi)
{
roi_ua = UMat(ua, roi);
roi_a = Mat(a, roi);
roi_a.copyTo(roi_ua);
EXPECT_MAT_NEAR(roi_a, roi_ua, 0);
roi_ua.copyTo(roi_a);
EXPECT_MAT_NEAR(roi_ua, roi_a, 0);
roi_ua.copyTo(ua);
EXPECT_MAT_NEAR(roi_ua, ua, 0);
ua.copyTo(a);
EXPECT_MAT_NEAR(ua, a, 0);
}
ua.copyTo(ub);
EXPECT_MAT_NEAR(ua, ub, 0);
int i = randomInt(0, ua.cols-1);
a.col(i).copyTo(ub);
EXPECT_MAT_NEAR(a.col(i), ub, 0);
ua.col(i).copyTo(ub);
EXPECT_MAT_NEAR(ua.col(i), ub, 0);
ua.col(i).copyTo(b);
EXPECT_MAT_NEAR(ua.col(i), b, 0);
i = randomInt(0, a.rows-1);
ua.row(i).copyTo(ub);
EXPECT_MAT_NEAR(ua.row(i), ub, 0);
a.row(i).copyTo(ub);
EXPECT_MAT_NEAR(a.row(i), ub, 0);
ua.row(i).copyTo(b);
EXPECT_MAT_NEAR(ua.row(i), b, 0);
}
CORE_TEST_P(UMatBasicTests, GetUMat)
{
if(useRoi)
{
a = Mat(a, roi);
ua = UMat(ua,roi);
}
ub = a.getUMat(ACCESS_RW);
EXPECT_MAT_NEAR(ub, ua, 0);
b = a.getUMat(ACCESS_RW).getMat(ACCESS_RW);
EXPECT_MAT_NEAR(b, a, 0);
b.release();
b = ua.getMat(ACCESS_RW);
EXPECT_MAT_NEAR(b, a, 0);
b.release();
ub = ua.getMat(ACCESS_RW).getUMat(ACCESS_RW);
EXPECT_MAT_NEAR(ub, ua, 0);
}
INSTANTIATE_TEST_CASE_P(UMat, UMatBasicTests, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() ) );
//////////////////////////////////////////////////////////////// Reshape ////////////////////////////////////////////////////////////////////////
PARAM_TEST_CASE(UMatTestReshape, int, int, Size, bool)
{
Mat a;
UMat ua, ub;
int type;
int depth;
int cn;
Size size;
bool useRoi;
Size roi_size;
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);
}
};
CORE_TEST_P(UMatTestReshape, reshape)
{
a = randomMat(size,type, -100, 100);
a.copyTo(ua); a.copyTo(ua);
ua.copyTo(b); if(useRoi)
CHECK_DIFF(a, b);
ura = ua(roi);
ura.copyTo(rb);
CHECK_DIFF(ra, rb);
ra += Scalar::all(1.f);
{ {
Mat temp = ura.getMat(ACCESS_RW); int roi_shift_x = randomInt(0, size.width-1);
temp += Scalar::all(1.f); 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).clone();
a = Mat(a, roi).clone();
} }
ra.copyTo(rb);
CHECK_DIFF(ra, rb);
b = a.clone(); int nChannels = randomInt(1,4);
ra = a(roi);
rb = b(roi); if ((ua.cols*ua.channels()*ua.rows)%nChannels != 0)
randu(b, Scalar::all(-100), Scalar::all(100)); {
EXPECT_ANY_THROW(ua.reshape(nChannels));
}
else
{
ub = ua.reshape(nChannels);
ASSERT_EQ(ub.channels(),nChannels);
ASSERT_EQ(ub.channels()*ub.cols*ub.rows, ua.channels()*ua.cols*ua.rows);
EXPECT_MAT_NEAR(ua.reshape(nChannels), a.reshape(nChannels), 0);
int new_rows = randomInt(1, INT_MAX);
if ( ((int)ua.total()*ua.channels())%(new_rows*nChannels) != 0)
{
EXPECT_ANY_THROW (ua.reshape(nChannels, new_rows) );
}
else
{
EXPECT_NO_THROW ( ub = ua.reshape(nChannels, new_rows) );
ASSERT_EQ(ub.channels(),nChannels);
ASSERT_EQ(ub.rows, new_rows);
ASSERT_EQ(ub.channels()*ub.cols*ub.rows, ua.channels()*ua.cols*ua.rows);
EXPECT_MAT_NEAR(ua.reshape(nChannels,new_rows), a.reshape(nChannels,new_rows), 0);
}
new_rows = (int)ua.total()*ua.channels()/(nChannels*randomInt(1, size.width*size.height));
if (new_rows == 0) new_rows = 1;
int new_cols = (int)ua.total()*ua.channels()/(new_rows*nChannels);
int sz[] = {new_rows, new_cols};
if( ((int)ua.total()*ua.channels()) % (new_rows*new_cols) != 0 )
{
EXPECT_ANY_THROW( ua.reshape(nChannels, ua.dims, sz) );
}
else
{
EXPECT_NO_THROW ( ub = ua.reshape(nChannels, ua.dims, sz) );
ASSERT_EQ(ub.channels(),nChannels);
ASSERT_EQ(ub.rows, new_rows);
ASSERT_EQ(ub.cols, new_cols);
ASSERT_EQ(ub.channels()*ub.cols*ub.rows, ua.channels()*ua.cols*ua.rows);
EXPECT_MAT_NEAR(ua.reshape(nChannels, ua.dims, sz), a.reshape(nChannels, a.dims, sz), 0);
}
}
}
INSTANTIATE_TEST_CASE_P(UMat, UMatTestReshape, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() ));
////////////////////////////////////////////////////////////////// ROI testing ///////////////////////////////////////////////////////////////
PARAM_TEST_CASE(UMatTestRoi, int, int, Size)
{
Mat a, roi_a;
UMat ua, roi_ua;
int type;
int depth;
int cn;
Size size;
Size roi_size;
virtual void SetUp()
{
depth = GET_PARAM(0);
cn = GET_PARAM(1);
size = GET_PARAM(2);
type = CV_MAKE_TYPE(depth, cn);
}
};
CORE_TEST_P(UMatTestRoi, createRoi)
{
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);
}
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);
}
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);
}
INSTANTIATE_TEST_CASE_P(UMat, UMatTestRoi, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES ));
/////////////////////////////////////////////////////////////// Size ////////////////////////////////////////////////////////////////////
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()
{
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); b.copyTo(ub);
urb = ub(roi); if(useRoi)
/*std::cout << "==============================================\nbefore op (CPU):\n";
std::cout << "ra: " << ra << std::endl;
std::cout << "rb: " << rb << std::endl;*/
ra.copyTo(ura);
rb.copyTo(urb);
ra.release();
rb.release();
ura.copyTo(ra);
urb.copyTo(rb);
/*std::cout << "==============================================\nbefore op (GPU):\n";
std::cout << "ra: " << ra << std::endl;
std::cout << "rb: " << rb << std::endl;*/
cv::max(ra, rb, rc);
cv::max(ura, urb, urc);
urc.copyTo(rc0);
/*std::cout << "==============================================\nafter op:\n";
std::cout << "rc: " << rc << std::endl;
std::cout << "rc0: " << rc0 << std::endl;*/
CHECK_DIFF(rc0, rc);
{ {
UMat tmp = rc0.getUMat(ACCESS_WRITE); int roi_shift_x = randomInt(0, size.width-1);
cv::max(ura, urb, tmp); 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);
} }
CHECK_DIFF(rc0, rc); ua.copySize(ub);
ASSERT_EQ(ua.size, ub.size);
ura.copyTo(urc);
cv::max(urc, urb, urc);
urc.copyTo(rc0);
CHECK_DIFF(rc0, rc);
rc = ra ^ rb;
cv::bitwise_xor(ura, urb, urc);
urc.copyTo(rc0);
/*std::cout << "==============================================\nafter op:\n";
std::cout << "ra: " << rc0 << std::endl;
std::cout << "rc: " << rc << std::endl;*/
CHECK_DIFF(rc0, rc);
rc = ra + rb;
cv::add(ura, urb, urc);
urc.copyTo(rc0);
CHECK_DIFF(rc0, rc);
cv::subtract(ra, Scalar::all(5), rc);
cv::subtract(ura, Scalar::all(5), urc);
urc.copyTo(rc0);
CHECK_DIFF(rc0, rc);
}
catch (const test_excep& e)
{
ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return false;
}
return true;
} }
void CV_UMatTest::run( int /* start_from */) INSTANTIATE_TEST_CASE_P(UMat, UMatTestSizeOperations, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() ));
///////////////////////////////////////////////////////////////// UMat operations ////////////////////////////////////////////////////////////////////////////
PARAM_TEST_CASE(UMatTestUMatOperations, int, int, Size, bool)
{ {
printf("Use OpenCL: %s\nHave OpenCL: %s\n", Mat a, b;
ocl::useOpenCL() ? "TRUE" : "FALSE", UMat ua, ub;
ocl::haveOpenCL() ? "TRUE" : "FALSE" ); 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);
}
};
if (!TestUMat()) CORE_TEST_P(UMatTestUMatOperations, diag)
return;
ts->set_failed_test_info(cvtest::TS::OK);
}
TEST(Core_UMat, base) { CV_UMatTest test; test.safe_run(); }
TEST(Core_UMat, getUMat)
{ {
a = randomMat(size, type, -100, 100);
a.copyTo(ua);
Mat new_diag;
if(useRoi)
{ {
int a[3] = { 1, 2, 3 }; int roi_shift_x = randomInt(0, size.width-1);
Mat m = Mat(1, 1, CV_32SC3, a); int roi_shift_y = randomInt(0, size.height-1);
UMat u = m.getUMat(ACCESS_READ); roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y);
EXPECT_NE((void*)NULL, u.u); Rect roi(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height);
} ua = UMat(ua,roi);
a = Mat(a, roi);
{
Mat m(10, 10, CV_8UC1), ref;
for (int y = 0; y < m.rows; ++y)
{
uchar * const ptr = m.ptr<uchar>(y);
for (int x = 0; x < m.cols; ++x)
ptr[x] = (uchar)(x + y * 2);
}
ref = m.clone();
Rect r(1, 1, 8, 8);
ref(r).setTo(17);
{
UMat um = m(r).getUMat(ACCESS_WRITE);
um.setTo(17);
}
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);
} }
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);
} }
TEST(UMat, Sync) CORE_TEST_P(UMatTestUMatOperations, dotUMat)
{ {
UMat um(10, 10, CV_8UC1); a = randomMat(size, type, -100, 100);
b = randomMat(size, type, -100, 100);
{ a.copyTo(ua);
Mat m = um.getMat(ACCESS_WRITE); b.copyTo(ub);
m.setTo(cv::Scalar::all(17)); //ASSERT_EQ(ua.dot(ub), a.dot(b)); UMat::dot doesn't compiles
}
um.setTo(cv::Scalar::all(19));
EXPECT_EQ(0, cv::norm(um.getMat(ACCESS_READ), cv::Mat(um.size(), um.type(), 19), NORM_INF));
} }
#define EXPECT_MAT_NEAR(m1, m2) ASSERT_EQ(0, cv::norm(m1, m1, cv::NORM_INF)) INSTANTIATE_TEST_CASE_P(UMat, UMatTestUMatOperations, Combine(UMAT_TEST_DEPTH, UMAT_TEST_CHANNELS, UMAT_TEST_SIZES, Bool() ));
TEST(UMat, setOpenCL) ///////////////////////////////////////////////////////////////// OpenCL ////////////////////////////////////////////////////////////////////////////
{
// save the current state
bool useOCL = ocl::useOpenCL();
Mat m = (Mat_<uchar>(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);
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);
// reset state to the previous one
ocl::setUseOpenCL(useOCL);
}
TEST(UMat, BufferPoolGrowing) TEST(UMat, BufferPoolGrowing)
{ {
@ -300,7 +499,7 @@ TEST(UMat, BufferPoolGrowing)
const int ITERATIONS = 200; const int ITERATIONS = 200;
#endif #endif
const Size sz(1920, 1080); const Size sz(1920, 1080);
BufferPoolController* c = ocl::getOpenCLAllocator()->getBufferPoolController(); BufferPoolController* c = cv::ocl::getOpenCLAllocator()->getBufferPoolController();
if (c) if (c)
{ {
size_t oldMaxReservedSize = c->getMaxReservedSize(); size_t oldMaxReservedSize = c->getMaxReservedSize();
@ -319,3 +518,45 @@ TEST(UMat, BufferPoolGrowing)
std::cout << "Skipped, no OpenCL" << std::endl; std::cout << "Skipped, no OpenCL" << std::endl;
} }
} }
TEST(UMat, setOpenCL)
{
// save the current state
bool useOCL = cv::ocl::useOpenCL();
Mat m = (Mat_<uchar>(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);
}