test_gpumat.cpp 9.9 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*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.
//
//   * 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_CUDA

using namespace cvtest;

////////////////////////////////////////////////////////////////////////////////
// SetTo

wester committed
52
PARAM_TEST_CASE(SetTo, cv::gpu::DeviceInfo, cv::Size, MatType, UseRoi)
wester committed
53
{
wester committed
54
    cv::gpu::DeviceInfo devInfo;
wester committed
55 56 57 58 59 60 61 62 63 64 65
    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);

wester committed
66
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
67 68 69
    }
};

wester committed
70
GPU_TEST_P(SetTo, Zero)
wester committed
71 72 73
{
    cv::Scalar zero = cv::Scalar::all(0);

wester committed
74
    cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
75 76 77 78 79
    mat.setTo(zero);

    EXPECT_MAT_NEAR(cv::Mat::zeros(size, type), mat, 0.0);
}

wester committed
80
GPU_TEST_P(SetTo, SameVal)
wester committed
81 82 83
{
    cv::Scalar val = cv::Scalar::all(randomDouble(0.0, 255.0));

wester committed
84
    if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
85 86 87
    {
        try
        {
wester committed
88
            cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
89 90 91 92
            mat.setTo(val);
        }
        catch (const cv::Exception& e)
        {
wester committed
93
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
94 95 96 97
        }
    }
    else
    {
wester committed
98
        cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
99 100 101 102 103 104
        mat.setTo(val);

        EXPECT_MAT_NEAR(cv::Mat(size, type, val), mat, 0.0);
    }
}

wester committed
105
GPU_TEST_P(SetTo, DifferentVal)
wester committed
106 107 108
{
    cv::Scalar val = randomScalar(0.0, 255.0);

wester committed
109
    if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
110 111 112
    {
        try
        {
wester committed
113
            cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
114 115 116 117
            mat.setTo(val);
        }
        catch (const cv::Exception& e)
        {
wester committed
118
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
119 120 121 122
        }
    }
    else
    {
wester committed
123
        cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
124 125 126 127 128 129
        mat.setTo(val);

        EXPECT_MAT_NEAR(cv::Mat(size, type, val), mat, 0.0);
    }
}

wester committed
130
GPU_TEST_P(SetTo, Masked)
wester committed
131 132 133 134 135
{
    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);

wester committed
136
    if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
137 138 139
    {
        try
        {
wester committed
140
            cv::gpu::GpuMat mat = createMat(size, type, useRoi);
wester committed
141 142 143 144
            mat.setTo(val, loadMat(mask));
        }
        catch (const cv::Exception& e)
        {
wester committed
145
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
146 147 148 149
        }
    }
    else
    {
wester committed
150
        cv::gpu::GpuMat mat = loadMat(mat_gold, useRoi);
wester committed
151 152 153 154 155 156 157 158
        mat.setTo(val, loadMat(mask, useRoi));

        mat_gold.setTo(val, mask);

        EXPECT_MAT_NEAR(mat_gold, mat, 0.0);
    }
}

wester committed
159
INSTANTIATE_TEST_CASE_P(GPU_GpuMat, SetTo, testing::Combine(
wester committed
160 161 162 163 164 165 166 167
    ALL_DEVICES,
    DIFFERENT_SIZES,
    ALL_TYPES,
    WHOLE_SUBMAT));

////////////////////////////////////////////////////////////////////////////////
// CopyTo

wester committed
168
PARAM_TEST_CASE(CopyTo, cv::gpu::DeviceInfo, cv::Size, MatType, UseRoi)
wester committed
169
{
wester committed
170
    cv::gpu::DeviceInfo devInfo;
wester committed
171 172 173 174 175 176 177 178 179 180 181 182
    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);

wester committed
183
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
184 185 186
    }
};

wester committed
187
GPU_TEST_P(CopyTo, WithOutMask)
wester committed
188 189 190
{
    cv::Mat src = randomMat(size, type);

wester committed
191 192
    cv::gpu::GpuMat d_src = loadMat(src, useRoi);
    cv::gpu::GpuMat dst = createMat(size, type, useRoi);
wester committed
193 194 195 196 197
    d_src.copyTo(dst);

    EXPECT_MAT_NEAR(src, dst, 0.0);
}

wester committed
198
GPU_TEST_P(CopyTo, Masked)
wester committed
199 200 201 202
{
    cv::Mat src = randomMat(size, type);
    cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);

wester committed
203
    if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
204 205 206
    {
        try
        {
wester committed
207 208
            cv::gpu::GpuMat d_src = loadMat(src);
            cv::gpu::GpuMat dst;
wester committed
209 210 211 212
            d_src.copyTo(dst, loadMat(mask, useRoi));
        }
        catch (const cv::Exception& e)
        {
wester committed
213
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
214 215 216 217
        }
    }
    else
    {
wester committed
218 219
        cv::gpu::GpuMat d_src = loadMat(src, useRoi);
        cv::gpu::GpuMat dst = loadMat(cv::Mat::zeros(size, type), useRoi);
wester committed
220 221 222 223 224 225 226 227 228
        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);
    }
}

wester committed
229
INSTANTIATE_TEST_CASE_P(GPU_GpuMat, CopyTo, testing::Combine(
wester committed
230 231 232 233 234 235 236 237
    ALL_DEVICES,
    DIFFERENT_SIZES,
    ALL_TYPES,
    WHOLE_SUBMAT));

////////////////////////////////////////////////////////////////////////////////
// ConvertTo

wester committed
238
PARAM_TEST_CASE(ConvertTo, cv::gpu::DeviceInfo, cv::Size, MatDepth, MatDepth, UseRoi)
wester committed
239
{
wester committed
240
    cv::gpu::DeviceInfo devInfo;
wester committed
241 242 243 244 245 246 247 248 249 250 251 252 253
    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);

wester committed
254
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
255 256 257
    }
};

wester committed
258
GPU_TEST_P(ConvertTo, WithOutScaling)
wester committed
259 260 261
{
    cv::Mat src = randomMat(size, depth1);

wester committed
262
    if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
263 264 265
    {
        try
        {
wester committed
266 267
            cv::gpu::GpuMat d_src = loadMat(src);
            cv::gpu::GpuMat dst;
wester committed
268 269 270 271
            d_src.convertTo(dst, depth2);
        }
        catch (const cv::Exception& e)
        {
wester committed
272
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
273 274 275 276
        }
    }
    else
    {
wester committed
277 278
        cv::gpu::GpuMat d_src = loadMat(src, useRoi);
        cv::gpu::GpuMat dst = createMat(size, depth2, useRoi);
wester committed
279 280 281 282 283 284 285 286 287
        d_src.convertTo(dst, depth2);

        cv::Mat dst_gold;
        src.convertTo(dst_gold, depth2);

        EXPECT_MAT_NEAR(dst_gold, dst, depth2 < CV_32F ? 1.0 : 1e-4);
    }
}

wester committed
288
GPU_TEST_P(ConvertTo, WithScaling)
wester committed
289 290 291 292 293
{
    cv::Mat src = randomMat(size, depth1);
    double a = randomDouble(0.0, 1.0);
    double b = randomDouble(-10.0, 10.0);

wester committed
294
    if ((depth1 == CV_64F || depth2 == CV_64F) && !supportFeature(devInfo, cv::gpu::NATIVE_DOUBLE))
wester committed
295 296 297
    {
        try
        {
wester committed
298 299
            cv::gpu::GpuMat d_src = loadMat(src);
            cv::gpu::GpuMat dst;
wester committed
300 301 302 303
            d_src.convertTo(dst, depth2, a, b);
        }
        catch (const cv::Exception& e)
        {
wester committed
304
            ASSERT_EQ(CV_StsUnsupportedFormat, e.code);
wester committed
305 306 307 308
        }
    }
    else
    {
wester committed
309 310
        cv::gpu::GpuMat d_src = loadMat(src, useRoi);
        cv::gpu::GpuMat dst = createMat(size, depth2, useRoi);
wester committed
311 312 313 314 315 316 317 318 319
        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);
    }
}

wester committed
320
INSTANTIATE_TEST_CASE_P(GPU_GpuMat, ConvertTo, testing::Combine(
wester committed
321 322 323 324 325 326 327 328 329
    ALL_DEVICES,
    DIFFERENT_SIZES,
    ALL_DEPTH,
    ALL_DEPTH,
    WHOLE_SUBMAT));

////////////////////////////////////////////////////////////////////////////////
// ensureSizeIsEnough

wester committed
330
struct EnsureSizeIsEnough : testing::TestWithParam<cv::gpu::DeviceInfo>
wester committed
331 332 333
{
    virtual void SetUp()
    {
wester committed
334 335
        cv::gpu::DeviceInfo devInfo = GetParam();
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
336 337 338
    }
};

wester committed
339
GPU_TEST_P(EnsureSizeIsEnough, BufferReuse)
wester committed
340
{
wester committed
341 342
    cv::gpu::GpuMat buffer(100, 100, CV_8U);
    cv::gpu::GpuMat old = buffer;
wester committed
343 344

    // don't reallocate memory
wester committed
345
    cv::gpu::ensureSizeIsEnough(10, 20, CV_8U, buffer);
wester committed
346 347 348 349 350 351
    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
wester committed
352
    cv::gpu::ensureSizeIsEnough(20, 30, CV_8U, buffer);
wester committed
353 354 355 356 357 358
    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));
}

wester committed
359
INSTANTIATE_TEST_CASE_P(GPU_GpuMat, EnsureSizeIsEnough, ALL_DEVICES);
wester committed
360 361

#endif // HAVE_CUDA