Merge pull request #1644 from ilya-lavrenov:ocl_matrix_operations

This commit is contained in:
Andrey Pavlenko 2013-10-21 15:59:19 +04:00 committed by OpenCV Buildbot
commit e82fd12a67

View File

@ -48,382 +48,176 @@
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL
using namespace cvtest; using namespace cv;
using namespace testing; using namespace testing;
using namespace std; using namespace std;
////////////////////////////////converto///////////////////////////////////////////////// ////////////////////////////////converto/////////////////////////////////////////////////
PARAM_TEST_CASE(ConvertToTestBase, MatType, MatType, int, bool) PARAM_TEST_CASE(MatrixTestBase, MatDepth, MatDepth, int, bool)
{ {
int src_depth, dst_depth; int src_depth, cn, dstType;
int cn, dst_type;
bool use_roi; bool use_roi;
// src mat Mat src, dst, src_roi, dst_roi;
cv::Mat mat; ocl::oclMat gdst, gsrc, gdst_roi, gsrc_roi;
cv::Mat dst;
// set up roi
int roicols, roirows;
int srcx, srcy;
int dstx, dsty;
// src mat with roi
cv::Mat mat_roi;
cv::Mat dst_roi;
// ocl dst mat for testing
cv::ocl::oclMat gdst_whole;
// ocl mat with roi
cv::ocl::oclMat gsrc;
cv::ocl::oclMat gdst;
virtual void SetUp() virtual void SetUp()
{ {
src_depth = GET_PARAM(0); src_depth = GET_PARAM(0);
dst_depth = GET_PARAM(1);
cn = GET_PARAM(2); cn = GET_PARAM(2);
int src_type = CV_MAKE_TYPE(src_depth, cn); dstType = CV_MAKE_TYPE(GET_PARAM(1), cn);
dst_type = CV_MAKE_TYPE(dst_depth, cn);
use_roi = GET_PARAM(3); use_roi = GET_PARAM(3);
mat = randomMat(randomSize(MIN_VALUE, MAX_VALUE), src_type, 5, 136, false);
dst = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : mat.size(), dst_type, 5, 136, false);
} }
void random_roi() virtual void random_roi()
{ {
if (use_roi) Size roiSize = randomSize(1, MAX_VALUE);
{ Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
// randomize ROI randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(src_depth, cn), -MAX_VALUE, MAX_VALUE);
roicols = rng.uniform(1, MIN_VALUE);
roirows = rng.uniform(1, MIN_VALUE);
srcx = rng.uniform(0, mat.cols - roicols);
srcy = rng.uniform(0, mat.rows - roirows);
dstx = rng.uniform(0, dst.cols - roicols);
dsty = rng.uniform(0, dst.rows - roirows);
}
else
{
roicols = mat.cols;
roirows = mat.rows;
srcx = srcy = 0;
dstx = dsty = 0;
}
mat_roi = mat(Rect(srcx, srcy, roicols, roirows)); Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
dst_roi = dst(Rect(dstx, dsty, roicols, roirows)); randomSubMat(dst, dst_roi, roiSize, dstBorder, dstType, 5, 16);
gdst_whole = dst; generateOclMat(gsrc, gsrc_roi, src, roiSize, srcBorder);
gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows)); generateOclMat(gdst, gdst_roi, dst, roiSize, dstBorder);
gsrc = mat_roi;
} }
}; };
typedef ConvertToTestBase ConvertTo; typedef MatrixTestBase ConvertTo;
OCL_TEST_P(ConvertTo, Accuracy) OCL_TEST_P(ConvertTo, Accuracy)
{ {
if((src_depth == CV_64F || dst_depth == CV_64F) &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
mat_roi.convertTo(dst_roi, dst_type); src_roi.convertTo(dst_roi, dstType);
gsrc.convertTo(gdst, dst_type); gsrc_roi.convertTo(gdst_roi, dstType);
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), src_depth == CV_64F ? 1.0 : 0.0); EXPECT_MAT_NEAR(dst, Mat(gdst), src_depth == CV_64F ? 1.0 : 0.0);
EXPECT_MAT_NEAR(dst_roi, Mat(gdst), src_depth == CV_64F ? 1.0 : 0.0); EXPECT_MAT_NEAR(dst_roi, Mat(gdst_roi), src_depth == CV_64F ? 1.0 : 0.0);
} }
} }
///////////////////////////////////////////copyto///////////////////////////////////////////////////////////// ///////////////////////////////////////////copyto/////////////////////////////////////////////////////////////
PARAM_TEST_CASE(CopyToTestBase, MatType, int, bool) struct CopyTo :
public MatrixTestBase
{ {
bool use_roi; Mat mask, mask_roi;
ocl::oclMat gmask, gmask_roi;
cv::Mat src, mask, dst; virtual void random_roi()
// set up roi
int roicols,roirows;
int srcx, srcy;
int dstx, dsty;
int maskx,masky;
// src mat with roi
cv::Mat src_roi;
cv::Mat mask_roi;
cv::Mat dst_roi;
// ocl dst mat for testing
cv::ocl::oclMat gdst_whole;
// ocl mat with roi
cv::ocl::oclMat gsrc, gdst, gmask;
virtual void SetUp()
{ {
int type = CV_MAKETYPE(GET_PARAM(0), GET_PARAM(1)); int type = CV_MAKE_TYPE(src_depth, cn);
use_roi = GET_PARAM(2); Size roiSize = randomSize(1, MAX_VALUE);
Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
src = randomMat(randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false); Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
dst = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), type, 5, 16, false); randomSubMat(dst, dst_roi, roiSize, dstBorder, type, 5, 16);
mask = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), CV_8UC1, 0, 2, false);
cv::threshold(mask, mask, 0.5, 255., CV_8UC1); Border maskBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
} randomSubMat(mask, mask_roi, roiSize, maskBorder, CV_8UC1, 5, 16);
void random_roi() generateOclMat(gsrc, gsrc_roi, src, roiSize, srcBorder);
{ generateOclMat(gdst, gdst_roi, dst, roiSize, dstBorder);
if (use_roi) generateOclMat(gmask, gmask_roi, mask, roiSize, maskBorder);
{
// randomize ROI
roicols = rng.uniform(1, MIN_VALUE);
roirows = rng.uniform(1, MIN_VALUE);
srcx = rng.uniform(0, src.cols - roicols);
srcy = rng.uniform(0, src.rows - roirows);
dstx = rng.uniform(0, dst.cols - roicols);
dsty = rng.uniform(0, dst.rows - roirows);
maskx = rng.uniform(0, mask.cols - roicols);
masky = rng.uniform(0, mask.rows - roirows);
}
else
{
roicols = src.cols;
roirows = src.rows;
srcx = srcy = 0;
dstx = dsty = 0;
maskx = masky = 0;
}
src_roi = src(Rect(srcx, srcy, roicols, roirows));
mask_roi = mask(Rect(maskx, masky, roicols, roirows));
dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
gdst_whole = dst;
gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
gsrc = src_roi;
gmask = mask_roi;
} }
}; };
typedef CopyToTestBase CopyTo;
OCL_TEST_P(CopyTo, Without_mask) OCL_TEST_P(CopyTo, Without_mask)
{ {
if((src.depth() == CV_64F) &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
src_roi.copyTo(dst_roi); src_roi.copyTo(dst_roi);
gsrc.copyTo(gdst); gsrc_roi.copyTo(gdst_roi);
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0); EXPECT_MAT_NEAR(dst, Mat(gdst), 0.0);
EXPECT_MAT_NEAR(dst_roi, Mat(gdst_roi), 0.0);
} }
} }
OCL_TEST_P(CopyTo, With_mask) OCL_TEST_P(CopyTo, With_mask)
{ {
if(src.depth() == CV_64F &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
src_roi.copyTo(dst_roi, mask_roi); src_roi.copyTo(dst_roi, mask_roi);
gsrc.copyTo(gdst, gmask); gsrc_roi.copyTo(gdst_roi, gmask_roi);
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0); EXPECT_MAT_NEAR(dst, Mat(gdst), 0.0);
EXPECT_MAT_NEAR(dst_roi, Mat(gdst_roi), 0.0);
} }
} }
/////////////////////////////////////////// setTo ///////////////////////////////////////////////////////////// /////////////////////////////////////////// setTo /////////////////////////////////////////////////////////////
PARAM_TEST_CASE(SetToTestBase, MatType, int, bool) typedef CopyTo SetTo;
{
int depth, channels;
bool use_roi;
cv::Scalar val;
cv::Mat src;
cv::Mat mask;
// set up roi
int roicols, roirows;
int srcx, srcy;
int maskx, masky;
// src mat with roi
cv::Mat src_roi;
cv::Mat mask_roi;
// ocl dst mat for testing
cv::ocl::oclMat gsrc_whole;
// ocl mat with roi
cv::ocl::oclMat gsrc;
cv::ocl::oclMat gmask;
virtual void SetUp()
{
depth = GET_PARAM(0);
channels = GET_PARAM(1);
use_roi = GET_PARAM(2);
int type = CV_MAKE_TYPE(depth, channels);
src = randomMat(randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false);
mask = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), CV_8UC1, 0, 2, false);
cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
val = cv::Scalar(rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0),
rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0));
}
void random_roi()
{
if (use_roi)
{
// randomize ROI
roicols = rng.uniform(1, MIN_VALUE);
roirows = rng.uniform(1, MIN_VALUE);
srcx = rng.uniform(0, src.cols - roicols);
srcy = rng.uniform(0, src.rows - roirows);
maskx = rng.uniform(0, mask.cols - roicols);
masky = rng.uniform(0, mask.rows - roirows);
}
else
{
roicols = src.cols;
roirows = src.rows;
srcx = srcy = 0;
maskx = masky = 0;
}
src_roi = src(Rect(srcx, srcy, roicols, roirows));
mask_roi = mask(Rect(maskx, masky, roicols, roirows));
gsrc_whole = src;
gsrc = gsrc_whole(Rect(srcx, srcy, roicols, roirows));
gmask = mask_roi;
}
};
typedef SetToTestBase SetTo;
OCL_TEST_P(SetTo, Without_mask) OCL_TEST_P(SetTo, Without_mask)
{ {
if(depth == CV_64F &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
Scalar scalar = randomScalar(-MAX_VALUE, MAX_VALUE);
src_roi.setTo(val); src_roi.setTo(scalar);
gsrc.setTo(val); gsrc_roi.setTo(scalar);
EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.); EXPECT_MAT_NEAR(dst, Mat(gdst), 0.0);
EXPECT_MAT_NEAR(dst_roi, Mat(gdst_roi), 0.0);;
} }
} }
OCL_TEST_P(SetTo, With_mask) OCL_TEST_P(SetTo, With_mask)
{ {
if(depth == CV_64F &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
Scalar scalar = randomScalar(-MAX_VALUE, MAX_VALUE);
src_roi.setTo(val, mask_roi); src_roi.setTo(scalar, mask_roi);
gsrc.setTo(val, gmask); gsrc_roi.setTo(scalar, gmask_roi);
EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.); EXPECT_MAT_NEAR(src, Mat(gsrc), 1.);
EXPECT_MAT_NEAR(src_roi, Mat(gsrc_roi), 1.);
} }
} }
// convertC3C4 // convertC3C4
PARAM_TEST_CASE(convertC3C4, MatType, bool) PARAM_TEST_CASE(convertC3C4, MatDepth, bool)
{ {
int depth; int depth;
bool use_roi; bool use_roi;
//src mat Mat src, src_roi;
cv::Mat src; ocl::oclMat gsrc, gsrc_roi;
// set up roi
int roicols, roirows;
int srcx, srcy;
//src mat with roi
cv::Mat src_roi;
//ocl mat with roi
cv::ocl::oclMat gsrc_roi;
virtual void SetUp() virtual void SetUp()
{ {
depth = GET_PARAM(0); depth = GET_PARAM(0);
use_roi = GET_PARAM(1); use_roi = GET_PARAM(1);
int type = CV_MAKE_TYPE(depth, 3);
src = randomMat(randomSize(1, MAX_VALUE), type, 0, 40, false);
} }
void random_roi() void random_roi()
{ {
if (use_roi) int type = CV_MAKE_TYPE(depth, 3);
{ Size roiSize = randomSize(1, MAX_VALUE);
//randomize ROI Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
roicols = rng.uniform(1, src.cols); randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
roirows = rng.uniform(1, src.rows); generateOclMat(gsrc, gsrc_roi, src, roiSize, srcBorder);
srcx = rng.uniform(0, src.cols - roicols);
srcy = rng.uniform(0, src.rows - roirows);
}
else
{
roicols = src.cols;
roirows = src.rows;
srcx = srcy = 0;
}
src_roi = src(Rect(srcx, srcy, roicols, roirows));
} }
}; };
OCL_TEST_P(convertC3C4, Accuracy) OCL_TEST_P(convertC3C4, Accuracy)
{ {
if(depth == CV_64F &&
!cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
{
return; // returns silently
}
for (int j = 0; j < LOOP_TIMES; j++) for (int j = 0; j < LOOP_TIMES; j++)
{ {
random_roi(); random_roi();
@ -431,20 +225,23 @@ OCL_TEST_P(convertC3C4, Accuracy)
gsrc_roi = src_roi; gsrc_roi = src_roi;
EXPECT_MAT_NEAR(src_roi, Mat(gsrc_roi), 0.0); EXPECT_MAT_NEAR(src_roi, Mat(gsrc_roi), 0.0);
EXPECT_MAT_NEAR(src, Mat(gsrc), 0.0);
} }
} }
INSTANTIATE_TEST_CASE_P(MatrixOperation, ConvertTo, Combine( INSTANTIATE_TEST_CASE_P(MatrixOperation, ConvertTo, Combine(
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
Range(1, 5), Bool())); testing::Range(1, 5), Bool()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, CopyTo, Combine( INSTANTIATE_TEST_CASE_P(MatrixOperation, CopyTo, Combine(
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
Values(MatDepth(0)), // not used
testing::Range(1, 5), Bool())); testing::Range(1, 5), Bool()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, SetTo, Combine( INSTANTIATE_TEST_CASE_P(MatrixOperation, SetTo, Combine(
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F), Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
Values((MatDepth)0), // not used
testing::Range(1, 5), Bool())); testing::Range(1, 5), Bool()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, convertC3C4, Combine( INSTANTIATE_TEST_CASE_P(MatrixOperation, convertC3C4, Combine(