opencv/modules/gpu/test/test_matop.cpp

560 lines
14 KiB
C++
Raw Normal View History

2011-06-29 18:14:16 +08:00
/*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) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// 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 GpuMaterials 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 bpied warranties, including, but not limited to, the bpied
// 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_CUDA
using namespace cvtest;
using namespace testing;
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// merge
PARAM_TEST_CASE(Merge, cv::gpu::DeviceInfo, MatType, UseRoi)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int type;
bool useRoi;
2011-06-29 18:14:16 +08:00
cv::Size size;
std::vector<cv::Mat> src;
cv::Mat dst_gold;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
type = GET_PARAM(1);
useRoi = GET_PARAM(2);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
int depth = CV_MAT_DEPTH(type);
int num_channels = CV_MAT_CN(type);
src.reserve(num_channels);
for (int i = 0; i < num_channels; ++i)
src.push_back(cv::Mat(size, depth, cv::Scalar::all(i)));
cv::merge(src, dst_gold);
}
};
TEST_P(Merge, Accuracy)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Mat dst;
2012-02-06 19:31:52 +08:00
std::vector<cv::gpu::GpuMat> dev_src;
cv::gpu::GpuMat dev_dst;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
for (size_t i = 0; i < src.size(); ++i)
dev_src.push_back(loadMat(src[i], useRoi));
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::merge(dev_src, dev_dst);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_dst.download(dst);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
}
INSTANTIATE_TEST_CASE_P(MatOp, Merge, Combine(
ALL_DEVICES,
ALL_TYPES,
USE_ROI));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// split
PARAM_TEST_CASE(Split, cv::gpu::DeviceInfo, MatType, UseRoi)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int type;
bool useRoi;
2011-06-29 18:14:16 +08:00
cv::Size size;
cv::Mat src;
std::vector<cv::Mat> dst_gold;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
type = GET_PARAM(1);
useRoi = GET_PARAM(2);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
src.create(size, type);
src.setTo(cv::Scalar(1.0, 2.0, 3.0, 4.0));
cv::split(src, dst_gold);
}
};
TEST_P(Split, Accuracy)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
std::vector<cv::Mat> dst;
2012-02-06 19:31:52 +08:00
std::vector<cv::gpu::GpuMat> dev_dst;
2012-02-06 19:31:52 +08:00
cv::gpu::split(loadMat(src, useRoi), dev_dst);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dst.resize(dev_dst.size());
2012-02-06 19:31:52 +08:00
for (size_t i = 0; i < dev_dst.size(); ++i)
dev_dst[i].download(dst[i]);
2011-06-29 18:14:16 +08:00
ASSERT_EQ(dst_gold.size(), dst.size());
for (size_t i = 0; i < dst_gold.size(); ++i)
{
EXPECT_MAT_NEAR(dst_gold[i], dst[i], 0.0);
}
}
INSTANTIATE_TEST_CASE_P(MatOp, Split, Combine(
ALL_DEVICES,
ALL_TYPES,
USE_ROI));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// split_merge_consistency
PARAM_TEST_CASE(SplitMerge, cv::gpu::DeviceInfo, MatType)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int type;
cv::Size size;
cv::Mat orig;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
type = GET_PARAM(1);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
orig.create(size, type);
orig.setTo(cv::Scalar(1.0, 2.0, 3.0, 4.0));
}
};
TEST_P(SplitMerge, Consistency)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Mat final;
2012-02-06 19:31:52 +08:00
std::vector<cv::gpu::GpuMat> dev_vec;
cv::gpu::GpuMat dev_final;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::split(loadMat(orig), dev_vec);
cv::gpu::merge(dev_vec, dev_final);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_final.download(final);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(orig, final, 0.0);
}
INSTANTIATE_TEST_CASE_P(MatOp, SplitMerge, Combine(
ALL_DEVICES,
ALL_TYPES));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// setTo
PARAM_TEST_CASE(SetTo, cv::gpu::DeviceInfo, MatType, UseRoi)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int type;
bool useRoi;
2011-06-29 18:14:16 +08:00
cv::Size size;
cv::Mat mat_gold;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
type = GET_PARAM(1);
useRoi = GET_PARAM(2);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
mat_gold.create(size, type);
}
};
TEST_P(SetTo, Zero)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Scalar zero = cv::Scalar::all(0);
2011-06-29 18:14:16 +08:00
cv::Mat mat;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_mat = loadMat(mat_gold, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
mat_gold.setTo(zero);
dev_mat.setTo(zero);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_mat.download(mat);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
}
TEST_P(SetTo, SameVal)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Scalar s = cv::Scalar::all(1);
2011-06-29 18:14:16 +08:00
cv::Mat mat;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_mat(mat_gold);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
mat_gold.setTo(s);
dev_mat.setTo(s);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_mat.download(mat);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
}
TEST_P(SetTo, DifferentVal)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Scalar s = cv::Scalar(1, 2, 3, 4);
2011-06-29 18:14:16 +08:00
cv::Mat mat;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_mat = loadMat(mat_gold, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
mat_gold.setTo(s);
dev_mat.setTo(s);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_mat.download(mat);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
}
TEST_P(SetTo, Masked)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Scalar s = cv::Scalar(1, 2, 3, 4);
cv::RNG& rng = TS::ptr()->get_rng();
cv::Mat mask = randomMat(rng, mat_gold.size(), CV_8UC1, 0.0, 1.5, false);
cv::Mat mat;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_mat = loadMat(mat_gold, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
mat_gold.setTo(s, mask);
dev_mat.setTo(s, loadMat(mask, useRoi));
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_mat.download(mat);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
}
INSTANTIATE_TEST_CASE_P(MatOp, SetTo, Combine(
ALL_DEVICES,
ALL_TYPES,
USE_ROI));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// copyTo
PARAM_TEST_CASE(CopyTo, cv::gpu::DeviceInfo, MatType, UseRoi)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int type;
bool useRoi;
2011-06-29 18:14:16 +08:00
cv::Size size;
cv::Mat src;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
type = GET_PARAM(1);
useRoi = GET_PARAM(2);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
src = randomMat(rng, size, type, 0.0, 127.0, false);
2011-06-29 18:14:16 +08:00
}
};
TEST_P(CopyTo, WithoutMask)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Mat dst_gold;
src.copyTo(dst_gold);
cv::Mat dst;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_src = loadMat(src, useRoi);
cv::gpu::GpuMat dev_dst = loadMat(src, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_src.copyTo(dev_dst);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_dst.download(dst);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
}
TEST_P(CopyTo, Masked)
{
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
cv::Mat mask = randomMat(rng, src.size(), CV_8UC1, 0.0, 2.0, false);
2011-06-29 18:14:16 +08:00
cv::Mat zeroMat(src.size(), src.type(), cv::Scalar::all(0));
2011-06-29 18:14:16 +08:00
cv::Mat dst_gold = zeroMat.clone();
2011-06-29 18:14:16 +08:00
src.copyTo(dst_gold, mask);
cv::Mat dst;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_src = loadMat(src, useRoi);
cv::gpu::GpuMat dev_dst = loadMat(zeroMat, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_src.copyTo(dev_dst, loadMat(mask, useRoi));
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_dst.download(dst);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
}
INSTANTIATE_TEST_CASE_P(MatOp, CopyTo, Combine(
ALL_DEVICES,
ALL_TYPES,
USE_ROI));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// convertTo
PARAM_TEST_CASE(ConvertTo, cv::gpu::DeviceInfo, MatType, MatType, UseRoi)
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
int depth1;
int depth2;
bool useRoi;
2011-06-29 18:14:16 +08:00
cv::Size size;
cv::Mat src;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
depth1 = GET_PARAM(1);
depth2 = GET_PARAM(2);
useRoi = GET_PARAM(3);
2011-06-29 18:14:16 +08:00
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
size = cv::Size(rng.uniform(20, 150), rng.uniform(20, 150));
src = randomMat(rng, size, depth1, 0.0, 127.0, false);
2011-06-29 18:14:16 +08:00
}
};
TEST_P(ConvertTo, WithoutScaling)
{
if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::Mat dst_gold;
src.convertTo(dst_gold, depth2);
cv::Mat dst;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_src = loadMat(src, useRoi);
cv::gpu::GpuMat dev_dst;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_src.convertTo(dev_dst, depth2);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_dst.download(dst);
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
}
TEST_P(ConvertTo, WithScaling)
{
if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
return;
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
const double a = rng.uniform(0.0, 1.0);
const double b = rng.uniform(-10.0, 10.0);
cv::Mat dst_gold;
src.convertTo(dst_gold, depth2, a, b);
cv::Mat dst;
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_src = loadMat(src, useRoi);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat dev_dst;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_src.convertTo(dev_dst, depth2, a, b);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dev_dst.download(dst);
2011-06-29 18:14:16 +08:00
const double eps = depth2 < CV_32F ? 1 : 1e-4;
EXPECT_MAT_NEAR(dst_gold, dst, eps);
}
INSTANTIATE_TEST_CASE_P(MatOp, ConvertTo, Combine(
ALL_DEVICES,
TYPES(CV_8U, CV_64F, 1, 1),
TYPES(CV_8U, CV_64F, 1, 1),
USE_ROI));
2011-06-29 18:14:16 +08:00
////////////////////////////////////////////////////////////////////////////////
// async
struct Async : TestWithParam<cv::gpu::DeviceInfo>
2011-06-29 18:14:16 +08:00
{
cv::gpu::DeviceInfo devInfo;
cv::gpu::CudaMem src;
cv::Mat dst_gold0;
cv::Mat dst_gold1;
virtual void SetUp()
{
devInfo = GetParam();
cv::gpu::setDevice(devInfo.deviceID());
cv::RNG& rng = TS::ptr()->get_rng();
2011-06-29 18:14:16 +08:00
int rows = rng.uniform(100, 200);
int cols = rng.uniform(100, 200);
src = cv::gpu::CudaMem(cv::Mat::zeros(rows, cols, CV_8UC1));
dst_gold0 = cv::Mat(rows, cols, CV_8UC1, cv::Scalar::all(255));
dst_gold1 = cv::Mat(rows, cols, CV_8UC1, cv::Scalar::all(128));
}
};
TEST_P(Async, Accuracy)
{
cv::Mat dst0, dst1;
2012-02-06 19:31:52 +08:00
cv::gpu::CudaMem cpudst0;
cv::gpu::CudaMem cpudst1;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::GpuMat gpusrc;
cv::gpu::GpuMat gpudst0;
cv::gpu::GpuMat gpudst1(src.rows, src.cols, CV_8UC1);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
cv::gpu::Stream stream0;
cv::gpu::Stream stream1;
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
stream0.enqueueUpload(src, gpusrc);
cv::gpu::bitwise_not(gpusrc, gpudst0, cv::gpu::GpuMat(), stream0);
stream0.enqueueDownload(gpudst0, cpudst0);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
stream1.enqueueMemSet(gpudst1, cv::Scalar::all(128));
stream1.enqueueDownload(gpudst1, cpudst1);
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
stream0.waitForCompletion();
stream1.waitForCompletion();
2011-06-29 18:14:16 +08:00
2012-02-06 19:31:52 +08:00
dst0 = cpudst0.createMatHeader();
dst1 = cpudst1.createMatHeader();
2011-06-29 18:14:16 +08:00
EXPECT_MAT_NEAR(dst_gold0, dst0, 0.0);
EXPECT_MAT_NEAR(dst_gold1, dst1, 0.0);
}
INSTANTIATE_TEST_CASE_P(MatOp, Async, ALL_DEVICES);
2011-06-29 18:14:16 +08:00
#endif // HAVE_CUDA