opencv/modules/ocl/test/test_matrix_operation.cpp

434 lines
12 KiB
C++
Raw Normal View History

/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
2013-05-08 15:21:53 +08:00
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Jia Haipeng, jiahaipeng95@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other oclMaterials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#ifdef HAVE_OPENCL
using namespace cvtest;
using namespace testing;
using namespace std;
////////////////////////////////converto/////////////////////////////////////////////////
2013-09-11 17:35:39 +08:00
PARAM_TEST_CASE(ConvertToTestBase, MatType, MatType, int, bool)
{
2013-09-11 17:35:39 +08:00
int src_depth, dst_depth;
int cn, dst_type;
bool use_roi;
2013-09-11 17:35:39 +08:00
// src mat
cv::Mat mat;
cv::Mat dst;
// set up roi
2013-09-11 17:35:39 +08:00
int roicols, roirows;
int srcx, srcy;
int dstx, dsty;
2013-09-11 17:35:39 +08:00
// src mat with roi
cv::Mat mat_roi;
cv::Mat dst_roi;
2013-09-11 17:35:39 +08:00
// ocl dst mat for testing
cv::ocl::oclMat gdst_whole;
2013-09-11 17:35:39 +08:00
// ocl mat with roi
cv::ocl::oclMat gsrc;
cv::ocl::oclMat gdst;
virtual void SetUp()
{
2013-09-11 17:35:39 +08:00
src_depth = GET_PARAM(0);
dst_depth = GET_PARAM(1);
cn = GET_PARAM(2);
int src_type = CV_MAKE_TYPE(src_depth, cn);
dst_type = CV_MAKE_TYPE(dst_depth, cn);
use_roi = GET_PARAM(3);
cv::RNG &rng = TS::ptr()->get_rng();
2013-09-11 17:35:39 +08:00
mat = randomMat(rng, randomSize(MIN_VALUE, MAX_VALUE), src_type, 5, 136, false);
dst = randomMat(rng, use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : mat.size(), dst_type, 5, 136, false);
}
void random_roi()
{
2013-09-11 17:35:39 +08:00
if (use_roi)
{
// randomize ROI
cv::RNG &rng = TS::ptr()->get_rng();
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));
2013-09-11 17:35:39 +08:00
dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
gdst_whole = dst;
gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
gsrc = mat_roi;
}
};
2013-09-11 17:35:39 +08:00
typedef ConvertToTestBase ConvertTo;
TEST_P(ConvertTo, Accuracy)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
random_roi();
mat_roi.convertTo(dst_roi, dst_type);
gsrc.convertTo(gdst, dst_type);
2013-09-11 17:35:39 +08:00
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), src_depth == CV_64F ? 1.0 : 0.0);
EXPECT_MAT_NEAR(dst_roi, Mat(gdst), src_depth == CV_64F ? 1.0 : 0.0);
}
}
///////////////////////////////////////////copyto/////////////////////////////////////////////////////////////
PARAM_TEST_CASE(CopyToTestBase, MatType, int, bool)
{
2013-09-11 17:35:39 +08:00
bool use_roi;
cv::Mat src, mask, dst;
// set up roi
int roicols,roirows;
int srcx, srcy;
int dstx, dsty;
int maskx,masky;
2013-09-11 17:35:39 +08:00
// src mat with roi
cv::Mat src_roi;
cv::Mat mask_roi;
cv::Mat dst_roi;
2013-09-11 17:35:39 +08:00
// ocl dst mat for testing
cv::ocl::oclMat gdst_whole;
2013-09-11 17:35:39 +08:00
// ocl mat with roi
cv::ocl::oclMat gsrc, gdst, gmask;
virtual void SetUp()
{
int type = CV_MAKETYPE(GET_PARAM(0), GET_PARAM(1));
use_roi = GET_PARAM(2);
cv::RNG &rng = TS::ptr()->get_rng();
src = randomMat(rng, randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false);
dst = randomMat(rng, use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), type, 5, 16, false);
mask = randomMat(rng, use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), CV_8UC1, 0, 2, false);
cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
}
void random_roi()
{
2013-09-11 17:35:39 +08:00
if (use_roi)
{
// randomize ROI
cv::RNG &rng = TS::ptr()->get_rng();
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);
2013-09-11 17:35:39 +08:00
maskx = rng.uniform(0, mask.cols - roicols);
masky = rng.uniform(0, mask.rows - roirows);
}
else
{
roicols = src.cols;
roirows = src.rows;
2013-09-11 17:35:39 +08:00
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));
2013-09-11 17:35:39 +08:00
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;
}
};
2013-09-11 17:35:39 +08:00
typedef CopyToTestBase CopyTo;
TEST_P(CopyTo, Without_mask)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
random_roi();
src_roi.copyTo(dst_roi);
gsrc.copyTo(gdst);
2013-05-09 17:57:13 +08:00
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
}
}
TEST_P(CopyTo, With_mask)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
random_roi();
src_roi.copyTo(dst_roi, mask_roi);
gsrc.copyTo(gdst, gmask);
2013-05-09 17:57:13 +08:00
EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
}
}
2013-09-11 17:35:39 +08:00
/////////////////////////////////////////// setTo /////////////////////////////////////////////////////////////
PARAM_TEST_CASE(SetToTestBase, MatType, int, bool)
{
int depth, channels;
2013-09-11 17:35:39 +08:00
bool use_roi;
cv::Scalar val;
cv::Mat src;
cv::Mat mask;
// set up roi
int roicols, roirows;
int srcx, srcy;
int maskx, masky;
2013-09-11 17:35:39 +08:00
// src mat with roi
cv::Mat src_roi;
cv::Mat mask_roi;
2013-09-11 17:35:39 +08:00
// ocl dst mat for testing
cv::ocl::oclMat gsrc_whole;
2013-09-11 17:35:39 +08:00
// 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);
cv::RNG &rng = TS::ptr()->get_rng();
int type = CV_MAKE_TYPE(depth, channels);
src = randomMat(rng, randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false);
mask = randomMat(rng, 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()
{
2013-09-11 17:35:39 +08:00
if (use_roi)
{
// randomize ROI
cv::RNG &rng = TS::ptr()->get_rng();
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);
2013-09-11 17:35:39 +08:00
maskx = rng.uniform(0, mask.cols - roicols);
masky = rng.uniform(0, mask.rows - roirows);
}
else
{
roicols = src.cols;
roirows = src.rows;
2013-09-11 17:35:39 +08:00
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;
}
};
2013-09-11 17:35:39 +08:00
typedef SetToTestBase SetTo;
TEST_P(SetTo, Without_mask)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
random_roi();
src_roi.setTo(val);
gsrc.setTo(val);
EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.);
}
}
TEST_P(SetTo, With_mask)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
random_roi();
src_roi.setTo(val, mask_roi);
gsrc.setTo(val, gmask);
EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.);
}
}
2013-09-11 17:35:39 +08:00
// convertC3C4
PARAM_TEST_CASE(convertC3C4, MatType, bool)
{
2013-09-11 17:35:39 +08:00
int depth;
bool use_roi;
//src mat
2013-09-11 17:35:39 +08:00
cv::Mat src;
// set up roi
2013-09-11 17:35:39 +08:00
int roicols, roirows;
int srcx, srcy;
//src mat with roi
2013-09-11 17:35:39 +08:00
cv::Mat src_roi;
//ocl mat with roi
2013-09-11 17:35:39 +08:00
cv::ocl::oclMat gsrc_roi;
virtual void SetUp()
{
2013-09-11 17:35:39 +08:00
depth = GET_PARAM(0);
use_roi = GET_PARAM(1);
int type = CV_MAKE_TYPE(depth, 3);
2013-09-11 17:35:39 +08:00
cv::RNG &rng = TS::ptr()->get_rng();
src = randomMat(rng, randomSize(MIN_VALUE, MAX_VALUE), type, 0, 40, false);
}
void random_roi()
{
2013-09-11 17:35:39 +08:00
if (use_roi)
{
//randomize ROI
cv::RNG &rng = TS::ptr()->get_rng();
roicols = rng.uniform(1, src.cols);
roirows = rng.uniform(1, src.rows);
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));
}
};
TEST_P(convertC3C4, Accuracy)
{
2013-09-11 17:35:39 +08:00
for (int j = 0; j < LOOP_TIMES; j++)
{
2013-09-11 17:35:39 +08:00
random_roi();
2013-09-11 17:35:39 +08:00
gsrc_roi = src_roi;
2013-05-09 17:57:13 +08:00
2013-09-11 17:35:39 +08:00
EXPECT_MAT_NEAR(src_roi, Mat(gsrc_roi), 0.0);
}
}
INSTANTIATE_TEST_CASE_P(MatrixOperation, ConvertTo, Combine(
2013-09-11 17:35:39 +08:00
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()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, CopyTo, Combine(
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
testing::Range(1, 5), Bool()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, SetTo, Combine(
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
testing::Range(1, 5), Bool()));
INSTANTIATE_TEST_CASE_P(MatrixOperation, convertC3C4, Combine(
2013-09-11 17:35:39 +08:00
Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
Bool()));
#endif