opencv/modules/cudaarithm/test/test_gpumat.cpp

413 lines
12 KiB
C++
Raw Normal View History

2012-10-17 07:18:30 +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 materials provided with the distribution.
2012-10-17 07:18:30 +08:00
//
// * 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
2012-10-17 07:18:30 +08:00
// 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"
2012-10-17 07:18:30 +08:00
#ifdef HAVE_CUDA
2014-12-19 22:07:44 +08:00
#include "opencv2/core/cuda.hpp"
#include "opencv2/ts/cuda_test.hpp"
namespace opencv_test { namespace {
2012-10-17 07:18:30 +08:00
////////////////////////////////////////////////////////////////////////////////
// SetTo
2014-12-19 22:07:44 +08:00
PARAM_TEST_CASE(GpuMat_SetTo, cv::cuda::DeviceInfo, cv::Size, MatType, UseRoi)
2012-10-17 07:18:30 +08:00
{
2013-08-28 19:45:13 +08:00
cv::cuda::DeviceInfo devInfo;
2012-10-17 07:18:30 +08:00
cv::Size size;
int type;
bool useRoi;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
size = GET_PARAM(1);
type = GET_PARAM(2);
useRoi = GET_PARAM(3);
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
}
};
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_SetTo, Zero)
2012-10-17 07:18:30 +08:00
{
cv::Scalar zero = cv::Scalar::all(0);
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(zero);
EXPECT_MAT_NEAR(cv::Mat::zeros(size, type), mat, 0.0);
}
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_SetTo, SameVal)
2012-10-17 07:18:30 +08:00
{
cv::Scalar val = cv::Scalar::all(randomDouble(0.0, 255.0));
2013-08-28 19:45:13 +08:00
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val);
EXPECT_MAT_NEAR(cv::Mat(size, type, val), mat, 0.0);
}
}
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_SetTo, DifferentVal)
2012-10-17 07:18:30 +08:00
{
cv::Scalar val = randomScalar(0.0, 255.0);
2013-08-28 19:45:13 +08:00
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val);
EXPECT_MAT_NEAR(cv::Mat(size, type, val), mat, 0.0);
}
}
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_SetTo, Masked)
2012-10-17 07:18:30 +08:00
{
cv::Scalar val = randomScalar(0.0, 255.0);
cv::Mat mat_gold = randomMat(size, type);
cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
2013-08-28 19:45:13 +08:00
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val, loadMat(mask));
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mat = loadMat(mat_gold, useRoi);
2012-10-17 07:18:30 +08:00
mat.setTo(val, loadMat(mask, useRoi));
mat_gold.setTo(val, mask);
EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
}
}
2014-12-19 22:07:44 +08:00
INSTANTIATE_TEST_CASE_P(CUDA, GpuMat_SetTo, testing::Combine(
2012-10-17 07:18:30 +08:00
ALL_DEVICES,
DIFFERENT_SIZES,
ALL_TYPES,
WHOLE_SUBMAT));
////////////////////////////////////////////////////////////////////////////////
// CopyTo
2014-12-19 22:07:44 +08:00
PARAM_TEST_CASE(GpuMat_CopyTo, cv::cuda::DeviceInfo, cv::Size, MatType, UseRoi)
2012-10-17 07:18:30 +08:00
{
2013-08-28 19:45:13 +08:00
cv::cuda::DeviceInfo devInfo;
2012-10-17 07:18:30 +08:00
cv::Size size;
int type;
bool useRoi;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
size = GET_PARAM(1);
type = GET_PARAM(2);
useRoi = GET_PARAM(3);
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
}
};
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_CopyTo, WithOutMask)
2012-10-17 07:18:30 +08:00
{
cv::Mat src = randomMat(size, type);
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src, useRoi);
cv::cuda::GpuMat dst = createMat(size, type, useRoi);
2012-10-17 07:18:30 +08:00
d_src.copyTo(dst);
EXPECT_MAT_NEAR(src, dst, 0.0);
}
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_CopyTo, Masked)
2012-10-17 07:18:30 +08:00
{
cv::Mat src = randomMat(size, type);
cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
2013-08-28 19:45:13 +08:00
if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src);
cv::cuda::GpuMat dst;
2012-10-17 07:18:30 +08:00
d_src.copyTo(dst, loadMat(mask, useRoi));
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src, useRoi);
cv::cuda::GpuMat dst = loadMat(cv::Mat::zeros(size, type), useRoi);
2012-10-17 07:18:30 +08:00
d_src.copyTo(dst, loadMat(mask, useRoi));
cv::Mat dst_gold = cv::Mat::zeros(size, type);
src.copyTo(dst_gold, mask);
EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
}
}
2014-12-19 22:07:44 +08:00
INSTANTIATE_TEST_CASE_P(CUDA, GpuMat_CopyTo, testing::Combine(
2012-10-17 07:18:30 +08:00
ALL_DEVICES,
DIFFERENT_SIZES,
ALL_TYPES,
WHOLE_SUBMAT));
////////////////////////////////////////////////////////////////////////////////
// ConvertTo
2014-12-19 22:07:44 +08:00
PARAM_TEST_CASE(GpuMat_ConvertTo, cv::cuda::DeviceInfo, cv::Size, MatDepth, MatDepth, UseRoi)
2012-10-17 07:18:30 +08:00
{
2013-08-28 19:45:13 +08:00
cv::cuda::DeviceInfo devInfo;
2012-10-17 07:18:30 +08:00
cv::Size size;
int depth1;
int depth2;
bool useRoi;
virtual void SetUp()
{
devInfo = GET_PARAM(0);
size = GET_PARAM(1);
depth1 = GET_PARAM(2);
depth2 = GET_PARAM(3);
useRoi = GET_PARAM(4);
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
}
};
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_ConvertTo, WithOutScaling)
2012-10-17 07:18:30 +08:00
{
cv::Mat src = randomMat(size, depth1);
2013-08-28 19:45:13 +08:00
if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src);
cv::cuda::GpuMat dst;
2012-10-17 07:18:30 +08:00
d_src.convertTo(dst, depth2);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src, useRoi);
cv::cuda::GpuMat dst = createMat(size, depth2, useRoi);
2012-10-17 07:18:30 +08:00
d_src.convertTo(dst, depth2);
cv::Mat dst_gold;
src.convertTo(dst_gold, depth2);
2014-02-06 16:41:19 +08:00
EXPECT_MAT_NEAR(dst_gold, dst, depth2 < CV_32F ? 1.0 : 1e-4);
2012-10-17 07:18:30 +08:00
}
}
2014-12-19 22:07:44 +08:00
CUDA_TEST_P(GpuMat_ConvertTo, WithScaling)
2012-10-17 07:18:30 +08:00
{
cv::Mat src = randomMat(size, depth1);
double a = randomDouble(0.0, 1.0);
double b = randomDouble(-10.0, 10.0);
2013-08-28 19:45:13 +08:00
if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
2012-10-17 07:18:30 +08:00
{
try
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src);
cv::cuda::GpuMat dst;
2012-10-17 07:18:30 +08:00
d_src.convertTo(dst, depth2, a, b);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_src = loadMat(src, useRoi);
cv::cuda::GpuMat dst = createMat(size, depth2, useRoi);
2012-10-17 07:18:30 +08:00
d_src.convertTo(dst, depth2, a, b);
cv::Mat dst_gold;
src.convertTo(dst_gold, depth2, a, b);
EXPECT_MAT_NEAR(dst_gold, dst, depth2 < CV_32F ? 1.0 : 1e-4);
}
}
2014-12-19 22:07:44 +08:00
INSTANTIATE_TEST_CASE_P(CUDA, GpuMat_ConvertTo, testing::Combine(
2012-10-17 07:18:30 +08:00
ALL_DEVICES,
DIFFERENT_SIZES,
ALL_DEPTH,
ALL_DEPTH,
WHOLE_SUBMAT));
2013-02-21 20:12:33 +08:00
////////////////////////////////////////////////////////////////////////////////
// ensureSizeIsEnough
2013-08-28 19:45:13 +08:00
struct EnsureSizeIsEnough : testing::TestWithParam<cv::cuda::DeviceInfo>
2013-02-21 20:12:33 +08:00
{
virtual void SetUp()
{
2013-08-28 19:45:13 +08:00
cv::cuda::DeviceInfo devInfo = GetParam();
cv::cuda::setDevice(devInfo.deviceID());
2013-02-21 20:12:33 +08:00
}
};
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(EnsureSizeIsEnough, BufferReuse)
2013-02-21 20:12:33 +08:00
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat buffer(100, 100, CV_8U);
cv::cuda::GpuMat old = buffer;
2013-02-21 20:12:33 +08:00
// don't reallocate memory
2013-08-28 19:45:13 +08:00
cv::cuda::ensureSizeIsEnough(10, 20, CV_8U, buffer);
2013-02-21 20:12:33 +08:00
EXPECT_EQ(10, buffer.rows);
EXPECT_EQ(20, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_EQ(reinterpret_cast<intptr_t>(old.data), reinterpret_cast<intptr_t>(buffer.data));
// don't reallocate memory
2013-08-28 19:45:13 +08:00
cv::cuda::ensureSizeIsEnough(20, 30, CV_8U, buffer);
2013-02-21 20:12:33 +08:00
EXPECT_EQ(20, buffer.rows);
EXPECT_EQ(30, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_EQ(reinterpret_cast<intptr_t>(old.data), reinterpret_cast<intptr_t>(buffer.data));
}
2014-12-19 22:07:44 +08:00
INSTANTIATE_TEST_CASE_P(CUDA, EnsureSizeIsEnough, ALL_DEVICES);
2013-02-21 20:12:33 +08:00
////////////////////////////////////////////////////////////////////////////////
// createContinuous
struct CreateContinuous : testing::TestWithParam<cv::cuda::DeviceInfo>
{
virtual void SetUp()
{
cv::cuda::DeviceInfo devInfo = GetParam();
cv::cuda::setDevice(devInfo.deviceID());
}
};
CUDA_TEST_P(CreateContinuous, BufferReuse)
{
cv::cuda::GpuMat buffer;
cv::cuda::createContinuous(100, 100, CV_8UC1, buffer);
EXPECT_EQ(100, buffer.rows);
EXPECT_EQ(100, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_TRUE(buffer.isContinuous());
EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step);
cv::cuda::createContinuous(10, 1000, CV_8UC1, buffer);
EXPECT_EQ(10, buffer.rows);
EXPECT_EQ(1000, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_TRUE(buffer.isContinuous());
EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step);
cv::cuda::createContinuous(10, 10, CV_8UC1, buffer);
EXPECT_EQ(10, buffer.rows);
EXPECT_EQ(10, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_TRUE(buffer.isContinuous());
EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step);
cv::cuda::createContinuous(100, 100, CV_8UC1, buffer);
EXPECT_EQ(100, buffer.rows);
EXPECT_EQ(100, buffer.cols);
EXPECT_EQ(CV_8UC1, buffer.type());
EXPECT_TRUE(buffer.isContinuous());
EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step);
}
INSTANTIATE_TEST_CASE_P(CUDA, CreateContinuous, ALL_DEVICES);
}} // namespace
2012-10-17 07:18:30 +08:00
#endif // HAVE_CUDA