test_filters.cpp 17.1 KB
Newer Older
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
/*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.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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"

44
#ifdef HAVE_CUDA
45

46
namespace
47
{
48
    double checkNorm(const cv::Mat& m1, const cv::Mat& m2, const cv::Size& ksize)
49
    {
50 51 52 53
        cv::Rect roi(ksize.width, ksize.height, m1.cols - 2 * ksize.width, m1.rows - 2 * ksize.height);
        cv::Mat m1ROI = m1(roi);
        cv::Mat m2ROI = m2(roi);
        return ::checkNorm(m1ROI, m2ROI);
54 55
    }

56
    double checkNorm(const cv::Mat& m1, const cv::Mat& m2, int ksize)
57
    {
58
        return checkNorm(m1, m2, cv::Size(ksize, ksize));
59
    }
60 61 62 63 64 65 66
}

#define EXPECT_MAT_NEAR_KSIZE(mat1, mat2, ksize, eps) \
    { \
        ASSERT_EQ(mat1.type(), mat2.type()); \
        ASSERT_EQ(mat1.size(), mat2.size()); \
        EXPECT_LE(checkNorm(mat1, mat2, ksize), eps); \
67 68
    }

69 70 71
/////////////////////////////////////////////////////////////////////////////////////////////////
// blur

72
struct Blur : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, int, int> >
73 74 75
{
    cv::gpu::DeviceInfo devInfo;
    cv::Size ksize;
76 77 78
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
79 80 81

    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
82
    
83
    virtual void SetUp()
84
    {
85 86 87 88
        devInfo = std::tr1::get<0>(GetParam());
        ksize = cv::Size(std::tr1::get<1>(GetParam()), std::tr1::get<2>(GetParam()));

        cv::gpu::setDevice(devInfo.deviceID());
89 90 91 92 93 94
                
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
95 96 97

        cv::blur(img_rgba, dst_gold_rgba, ksize);
        cv::blur(img_gray, dst_gold_gray, ksize);
98
    }
99 100 101 102 103 104
};

TEST_P(Blur, Accuracy)
{
    PRINT_PARAM(devInfo);
    PRINT_PARAM(ksize);
105

106 107
    cv::Mat dst_rgba;
    cv::Mat dst_gray;
108

109 110 111
    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;
112

113 114
        cv::gpu::blur(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, ksize);
        cv::gpu::blur(cv::gpu::GpuMat(img_gray), dev_dst_gray, ksize);
115

116 117 118
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
119

120 121
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, ksize, 1.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, ksize, 1.0);
122 123
}

124 125 126 127 128 129 130 131
INSTANTIATE_TEST_CASE_P(Filter, Blur, testing::Combine(
                        testing::ValuesIn(devices()), 
                        testing::Values(3, 5, 7), 
                        testing::Values(3, 5, 7)));

/////////////////////////////////////////////////////////////////////////////////////////////////
// sobel

132
struct Sobel : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, int, std::pair<int, int> > >
133
{
134 135 136
    cv::gpu::DeviceInfo devInfo;
    int ksize;
    int dx, dy;
137 138 139
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
140 141 142 143 144

    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
145
    {
146 147 148 149
        devInfo = std::tr1::get<0>(GetParam());
        ksize = std::tr1::get<1>(GetParam());
        std::pair<int, int> d = std::tr1::get<2>(GetParam());
        dx = d.first; dy = d.second;
150

151 152
        cv::gpu::setDevice(devInfo.deviceID());
        
153 154 155 156 157 158
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
        
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        cv::Sobel(img_rgba, dst_gold_rgba, -1, dx, dy, ksize);
        cv::Sobel(img_gray, dst_gold_gray, -1, dx, dy, ksize);
    }
};

TEST_P(Sobel, Accuracy)
{
    PRINT_PARAM(devInfo);
    PRINT_PARAM(ksize);
    PRINT_PARAM(dx);
    PRINT_PARAM(dy);

    cv::Mat dst_rgba;
    cv::Mat dst_gray;
173

174 175 176
    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;
177

178 179
        cv::gpu::Sobel(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, -1, dx, dy, ksize);
        cv::gpu::Sobel(cv::gpu::GpuMat(img_gray), dev_dst_gray, -1, dx, dy, ksize);
180

181 182 183
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
184

185 186 187 188 189 190 191 192
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, ksize, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, ksize, 0.0);
}

INSTANTIATE_TEST_CASE_P(Filter, Sobel, testing::Combine(
                        testing::ValuesIn(devices()), 
                        testing::Values(3, 5, 7), 
                        testing::Values(std::make_pair(1, 0), std::make_pair(0, 1), std::make_pair(1, 1), std::make_pair(2, 0), std::make_pair(2, 1), std::make_pair(0, 2), std::make_pair(1, 2), std::make_pair(2, 2))));
193

194 195 196
/////////////////////////////////////////////////////////////////////////////////////////////////
// scharr

197
struct Scharr : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, std::pair<int, int> > >
198 199 200
{
    cv::gpu::DeviceInfo devInfo;
    int dx, dy;
201 202 203
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
204

205 206 207 208 209 210 211 212 213 214
    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
    {
        devInfo = std::tr1::get<0>(GetParam());
        std::pair<int, int> d = std::tr1::get<1>(GetParam());
        dx = d.first; dy = d.second;

        cv::gpu::setDevice(devInfo.deviceID());
215 216 217 218 219 220
        
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
221 222 223

        cv::Scharr(img_rgba, dst_gold_rgba, -1, dx, dy);
        cv::Scharr(img_gray, dst_gold_gray, -1, dx, dy);
224 225 226
    }
};

227
TEST_P(Scharr, Accuracy)
228
{
229 230 231
    PRINT_PARAM(devInfo);
    PRINT_PARAM(dx);
    PRINT_PARAM(dy);
232

233 234
    cv::Mat dst_rgba;
    cv::Mat dst_gray;
235

236 237 238
    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;
239

240 241
        cv::gpu::Scharr(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, -1, dx, dy);
        cv::gpu::Scharr(cv::gpu::GpuMat(img_gray), dev_dst_gray, -1, dx, dy);
242

243 244 245
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
246

247 248 249
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, 3, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, 3, 0.0);
}
250

251 252 253
INSTANTIATE_TEST_CASE_P(Filter, Scharr, testing::Combine(
                        testing::ValuesIn(devices()),
                        testing::Values(std::make_pair(1, 0), std::make_pair(0, 1))));
254

255 256
/////////////////////////////////////////////////////////////////////////////////////////////////
// gaussianBlur
257

258
struct GaussianBlur : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, int, int> >
259
{
260 261
    cv::gpu::DeviceInfo devInfo;
    cv::Size ksize;
262 263 264
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
265 266

    double sigma1, sigma2;
267

268 269 270 271
    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
272
    {
273 274
        devInfo = std::tr1::get<0>(GetParam());
        ksize = cv::Size(std::tr1::get<1>(GetParam()), std::tr1::get<2>(GetParam()));
275

276 277
        cv::gpu::setDevice(devInfo.deviceID());
        
278 279 280 281 282 283
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
        
284
        cv::RNG& rng = cvtest::TS::ptr()->get_rng();
285

286 287 288 289 290
        sigma1 = rng.uniform(0.1, 1.0); 
        sigma2 = rng.uniform(0.1, 1.0);
        
        cv::GaussianBlur(img_rgba, dst_gold_rgba, ksize, sigma1, sigma2);
        cv::GaussianBlur(img_gray, dst_gold_gray, ksize, sigma1, sigma2);
291 292 293
    }
};

294
TEST_P(GaussianBlur, Accuracy)
295
{
296 297 298 299
    PRINT_PARAM(devInfo);
    PRINT_PARAM(ksize);
    PRINT_PARAM(sigma1);
    PRINT_PARAM(sigma2);
300

301 302
    cv::Mat dst_rgba;
    cv::Mat dst_gray;
303

304 305 306
    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;
307

308 309
        cv::gpu::GaussianBlur(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, ksize, sigma1, sigma2);
        cv::gpu::GaussianBlur(cv::gpu::GpuMat(img_gray), dev_dst_gray, ksize, sigma1, sigma2);
310

311 312 313
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
314

315 316 317 318 319 320 321 322
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, ksize, 3.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, ksize, 3.0);
}

INSTANTIATE_TEST_CASE_P(Filter, GaussianBlur, testing::Combine(
                        testing::ValuesIn(devices()), 
                        testing::Values(3, 5, 7), 
                        testing::Values(3, 5, 7)));
323

324 325 326
/////////////////////////////////////////////////////////////////////////////////////////////////
// laplacian

327
struct Laplacian : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, int> >
328 329 330
{
    cv::gpu::DeviceInfo devInfo;
    int ksize;
331 332 333
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
334 335 336 337 338 339 340 341

    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
    {
        devInfo = std::tr1::get<0>(GetParam());
        ksize = std::tr1::get<1>(GetParam());
342

343
        cv::gpu::setDevice(devInfo.deviceID());
344 345 346 347 348 349
        
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
350

351 352
        cv::Laplacian(img_rgba, dst_gold_rgba, -1, ksize);
        cv::Laplacian(img_gray, dst_gold_gray, -1, ksize);
353 354 355
    }
};

356
TEST_P(Laplacian, Accuracy)
357
{
358 359
    PRINT_PARAM(devInfo);
    PRINT_PARAM(ksize);
360

361 362
    cv::Mat dst_rgba;
    cv::Mat dst_gray;
363

364 365 366
    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;
367

368 369
        cv::gpu::Laplacian(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, -1, ksize);
        cv::gpu::Laplacian(cv::gpu::GpuMat(img_gray), dev_dst_gray, -1, ksize);
370

371 372 373
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
374

375 376 377
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, 3, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, 3, 0.0);
}
378

379 380 381 382 383 384
INSTANTIATE_TEST_CASE_P(Filter, Laplacian, testing::Combine(
                        testing::ValuesIn(devices()),
                        testing::Values(1, 3)));

/////////////////////////////////////////////////////////////////////////////////////////////////
// erode
385

386
struct Erode : testing::TestWithParam<cv::gpu::DeviceInfo>
387
{
388
    cv::gpu::DeviceInfo devInfo;
389 390 391
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
392 393

    cv::Mat kernel;
394

395 396 397 398
    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
399
    {
400
        devInfo = GetParam();
401

402
        cv::gpu::setDevice(devInfo.deviceID());
403

404
        kernel = cv::Mat::ones(3, 3, CV_8U);
405 406 407 408 409 410
        
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
411

412 413
        cv::erode(img_rgba, dst_gold_rgba, kernel);
        cv::erode(img_gray, dst_gold_gray, kernel);
414 415 416
    }
};

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
TEST_P(Erode, Accuracy)
{
    PRINT_PARAM(devInfo);

    cv::Mat dst_rgba;
    cv::Mat dst_gray;

    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;

        cv::gpu::erode(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, kernel);
        cv::gpu::erode(cv::gpu::GpuMat(img_gray), dev_dst_gray, kernel);

        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );

    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, 3, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, 3, 0.0);
}

INSTANTIATE_TEST_CASE_P(Filter, Erode, testing::ValuesIn(devices()));

/////////////////////////////////////////////////////////////////////////////////////////////////
// dilate

444
struct Dilate : testing::TestWithParam<cv::gpu::DeviceInfo>
445
{
446
    cv::gpu::DeviceInfo devInfo;
447 448 449
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
450 451

    cv::Mat kernel;
452

453 454 455 456
    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
457
    {
458
        devInfo = GetParam();
459

460
        cv::gpu::setDevice(devInfo.deviceID());
461

462
        kernel = cv::Mat::ones(3, 3, CV_8U);
463 464 465 466 467 468
        
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
469 470 471

        cv::dilate(img_rgba, dst_gold_rgba, kernel);
        cv::dilate(img_gray, dst_gold_gray, kernel);
472 473 474
    }
};

475
TEST_P(Dilate, Accuracy)
476
{
477 478 479 480 481 482 483 484 485 486 487
    PRINT_PARAM(devInfo);

    cv::Mat dst_rgba;
    cv::Mat dst_gray;

    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;

        cv::gpu::dilate(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, kernel);
        cv::gpu::dilate(cv::gpu::GpuMat(img_gray), dev_dst_gray, kernel);
488

489 490 491
        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );
492

493 494 495 496 497
    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, 3, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, 3, 0.0);
}

INSTANTIATE_TEST_CASE_P(Filter, Dilate, testing::ValuesIn(devices()));
498

499 500
/////////////////////////////////////////////////////////////////////////////////////////////////
// morphEx
501

502 503 504
static const int morphOps[] = {cv::MORPH_OPEN, CV_MOP_CLOSE, CV_MOP_GRADIENT, CV_MOP_TOPHAT, CV_MOP_BLACKHAT};
static const char* morphOps_str[] = {"MORPH_OPEN", "MOP_CLOSE", "MOP_GRADIENT", "MOP_TOPHAT", "MOP_BLACKHAT"};

505
struct MorphEx : testing::TestWithParam< std::tr1::tuple<cv::gpu::DeviceInfo, int> >
506 507 508
{
    cv::gpu::DeviceInfo devInfo;
    int morphOpsIdx;
509 510 511
    
    cv::Mat img_rgba;
    cv::Mat img_gray;
512

513
    cv::Mat kernel;
514

515 516 517 518 519 520 521 522 523
    cv::Mat dst_gold_rgba;
    cv::Mat dst_gold_gray;
    
    virtual void SetUp()
    {
        devInfo = std::tr1::get<0>(GetParam());
        morphOpsIdx = std::tr1::get<1>(GetParam());

        cv::gpu::setDevice(devInfo.deviceID());
524 525 526 527 528 529
        
        cv::Mat img = readImage("stereobp/aloe-L.png");
        ASSERT_FALSE(img.empty());
        
        cv::cvtColor(img, img_rgba, CV_BGR2BGRA);
        cv::cvtColor(img, img_gray, CV_BGR2GRAY);
530 531

        kernel = cv::Mat::ones(3, 3, CV_8U);
532
        
533 534
        cv::morphologyEx(img_rgba, dst_gold_rgba, morphOps[morphOpsIdx], kernel);
        cv::morphologyEx(img_gray, dst_gold_gray, morphOps[morphOpsIdx], kernel);
535 536 537
    }
};

538 539 540 541 542 543 544 545 546 547 548 549 550 551
TEST_P(MorphEx, Accuracy)
{
    const char* morphOpStr = morphOps_str[morphOpsIdx];

    PRINT_PARAM(devInfo);
    PRINT_PARAM(morphOpStr);

    cv::Mat dst_rgba;
    cv::Mat dst_gray;

    ASSERT_NO_THROW(
        cv::gpu::GpuMat dev_dst_rgba;
        cv::gpu::GpuMat dev_dst_gray;

552 553
        cv::gpu::morphologyEx(cv::gpu::GpuMat(img_rgba), dev_dst_rgba, morphOps[morphOpsIdx], kernel);
        cv::gpu::morphologyEx(cv::gpu::GpuMat(img_gray), dev_dst_gray, morphOps[morphOpsIdx], kernel);
554 555 556 557 558 559 560 561 562 563 564 565

        dev_dst_rgba.download(dst_rgba);
        dev_dst_gray.download(dst_gray);
    );

    EXPECT_MAT_NEAR_KSIZE(dst_gold_rgba, dst_rgba, 4, 0.0);
    EXPECT_MAT_NEAR_KSIZE(dst_gold_gray, dst_gray, 4, 0.0);
}

INSTANTIATE_TEST_CASE_P(Filter, MorphEx, testing::Combine(
                        testing::ValuesIn(devices()),
                        testing::Range(0, 5)));
566

567
#endif // HAVE_CUDA