test_features2d.cpp 23.1 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 52 53 54 55 56 57
/*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;

/////////////////////////////////////////////////////////////////////////////////////////////////
// FAST

namespace
{
    IMPLEMENT_PARAM_CLASS(FAST_Threshold, int)
    IMPLEMENT_PARAM_CLASS(FAST_NonmaxSuppression, bool)
}

wester committed
58
PARAM_TEST_CASE(FAST, cv::gpu::DeviceInfo, FAST_Threshold, FAST_NonmaxSuppression)
wester committed
59
{
wester committed
60
    cv::gpu::DeviceInfo devInfo;
wester committed
61 62 63 64 65 66 67 68 69
    int threshold;
    bool nonmaxSuppression;

    virtual void SetUp()
    {
        devInfo = GET_PARAM(0);
        threshold = GET_PARAM(1);
        nonmaxSuppression = GET_PARAM(2);

wester committed
70
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
71 72 73
    }
};

wester committed
74
GPU_TEST_P(FAST, Accuracy)
wester committed
75 76 77 78
{
    cv::Mat image = readImage("features2d/aloe.png", cv::IMREAD_GRAYSCALE);
    ASSERT_FALSE(image.empty());

wester committed
79 80
    cv::gpu::FAST_GPU fast(threshold);
    fast.nonmaxSuppression = nonmaxSuppression;
wester committed
81

wester committed
82
    if (!supportFeature(devInfo, cv::gpu::GLOBAL_ATOMICS))
wester committed
83 84 85 86
    {
        try
        {
            std::vector<cv::KeyPoint> keypoints;
wester committed
87
            fast(loadMat(image), cv::gpu::GpuMat(), keypoints);
wester committed
88 89 90
        }
        catch (const cv::Exception& e)
        {
wester committed
91
            ASSERT_EQ(CV_StsNotImplemented, e.code);
wester committed
92 93 94 95 96
        }
    }
    else
    {
        std::vector<cv::KeyPoint> keypoints;
wester committed
97
        fast(loadMat(image), cv::gpu::GpuMat(), keypoints);
wester committed
98 99 100 101 102 103 104 105

        std::vector<cv::KeyPoint> keypoints_gold;
        cv::FAST(image, keypoints_gold, threshold, nonmaxSuppression);

        ASSERT_KEYPOINTS_EQ(keypoints_gold, keypoints);
    }
}

wester committed
106
INSTANTIATE_TEST_CASE_P(GPU_Features2D, FAST, testing::Combine(
wester committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    ALL_DEVICES,
    testing::Values(FAST_Threshold(25), FAST_Threshold(50)),
    testing::Values(FAST_NonmaxSuppression(false), FAST_NonmaxSuppression(true))));

/////////////////////////////////////////////////////////////////////////////////////////////////
// ORB

namespace
{
    IMPLEMENT_PARAM_CLASS(ORB_FeaturesCount, int)
    IMPLEMENT_PARAM_CLASS(ORB_ScaleFactor, float)
    IMPLEMENT_PARAM_CLASS(ORB_LevelsCount, int)
    IMPLEMENT_PARAM_CLASS(ORB_EdgeThreshold, int)
    IMPLEMENT_PARAM_CLASS(ORB_firstLevel, int)
    IMPLEMENT_PARAM_CLASS(ORB_WTA_K, int)
    IMPLEMENT_PARAM_CLASS(ORB_PatchSize, int)
    IMPLEMENT_PARAM_CLASS(ORB_BlurForDescriptor, bool)
}

wester committed
126
CV_ENUM(ORB_ScoreType, ORB::HARRIS_SCORE, ORB::FAST_SCORE)
wester committed
127

wester committed
128
PARAM_TEST_CASE(ORB, cv::gpu::DeviceInfo, ORB_FeaturesCount, ORB_ScaleFactor, ORB_LevelsCount, ORB_EdgeThreshold, ORB_firstLevel, ORB_WTA_K, ORB_ScoreType, ORB_PatchSize, ORB_BlurForDescriptor)
wester committed
129
{
wester committed
130
    cv::gpu::DeviceInfo devInfo;
wester committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    int nFeatures;
    float scaleFactor;
    int nLevels;
    int edgeThreshold;
    int firstLevel;
    int WTA_K;
    int scoreType;
    int patchSize;
    bool blurForDescriptor;

    virtual void SetUp()
    {
        devInfo = GET_PARAM(0);
        nFeatures = GET_PARAM(1);
        scaleFactor = GET_PARAM(2);
        nLevels = GET_PARAM(3);
        edgeThreshold = GET_PARAM(4);
        firstLevel = GET_PARAM(5);
        WTA_K = GET_PARAM(6);
        scoreType = GET_PARAM(7);
        patchSize = GET_PARAM(8);
        blurForDescriptor = GET_PARAM(9);

wester committed
154
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
155 156 157
    }
};

wester committed
158
GPU_TEST_P(ORB, Accuracy)
wester committed
159 160 161 162 163 164 165
{
    cv::Mat image = readImage("features2d/aloe.png", cv::IMREAD_GRAYSCALE);
    ASSERT_FALSE(image.empty());

    cv::Mat mask(image.size(), CV_8UC1, cv::Scalar::all(1));
    mask(cv::Range(0, image.rows / 2), cv::Range(0, image.cols / 2)).setTo(cv::Scalar::all(0));

wester committed
166 167
    cv::gpu::ORB_GPU orb(nFeatures, scaleFactor, nLevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize);
    orb.blurForDescriptor = blurForDescriptor;
wester committed
168

wester committed
169
    if (!supportFeature(devInfo, cv::gpu::GLOBAL_ATOMICS))
wester committed
170 171 172 173
    {
        try
        {
            std::vector<cv::KeyPoint> keypoints;
wester committed
174 175
            cv::gpu::GpuMat descriptors;
            orb(loadMat(image), loadMat(mask), keypoints, descriptors);
wester committed
176 177 178
        }
        catch (const cv::Exception& e)
        {
wester committed
179
            ASSERT_EQ(CV_StsNotImplemented, e.code);
wester committed
180 181 182 183 184
        }
    }
    else
    {
        std::vector<cv::KeyPoint> keypoints;
wester committed
185 186
        cv::gpu::GpuMat descriptors;
        orb(loadMat(image), loadMat(mask), keypoints, descriptors);
wester committed
187

wester committed
188
        cv::ORB orb_gold(nFeatures, scaleFactor, nLevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize);
wester committed
189 190 191

        std::vector<cv::KeyPoint> keypoints_gold;
        cv::Mat descriptors_gold;
wester committed
192
        orb_gold(image, mask, keypoints_gold, descriptors_gold);
wester committed
193 194 195 196 197 198 199 200 201 202 203 204

        cv::BFMatcher matcher(cv::NORM_HAMMING);
        std::vector<cv::DMatch> matches;
        matcher.match(descriptors_gold, cv::Mat(descriptors), matches);

        int matchedCount = getMatchedPointsCount(keypoints_gold, keypoints, matches);
        double matchedRatio = static_cast<double>(matchedCount) / keypoints.size();

        EXPECT_GT(matchedRatio, 0.35);
    }
}

wester committed
205
INSTANTIATE_TEST_CASE_P(GPU_Features2D, ORB,  testing::Combine(
wester committed
206 207 208 209 210
    ALL_DEVICES,
    testing::Values(ORB_FeaturesCount(1000)),
    testing::Values(ORB_ScaleFactor(1.2f)),
    testing::Values(ORB_LevelsCount(4), ORB_LevelsCount(8)),
    testing::Values(ORB_EdgeThreshold(31)),
wester committed
211
    testing::Values(ORB_firstLevel(0), ORB_firstLevel(2)),
wester committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    testing::Values(ORB_WTA_K(2), ORB_WTA_K(3), ORB_WTA_K(4)),
    testing::Values(ORB_ScoreType(cv::ORB::HARRIS_SCORE)),
    testing::Values(ORB_PatchSize(31), ORB_PatchSize(29)),
    testing::Values(ORB_BlurForDescriptor(false), ORB_BlurForDescriptor(true))));

/////////////////////////////////////////////////////////////////////////////////////////////////
// BruteForceMatcher

namespace
{
    IMPLEMENT_PARAM_CLASS(DescriptorSize, int)
    IMPLEMENT_PARAM_CLASS(UseMask, bool)
}

wester committed
226
PARAM_TEST_CASE(BruteForceMatcher, cv::gpu::DeviceInfo, NormCode, DescriptorSize, UseMask)
wester committed
227
{
wester committed
228
    cv::gpu::DeviceInfo devInfo;
wester committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    int normCode;
    int dim;
    bool useMask;

    int queryDescCount;
    int countFactor;

    cv::Mat query, train;

    virtual void SetUp()
    {
        devInfo = GET_PARAM(0);
        normCode = GET_PARAM(1);
        dim = GET_PARAM(2);
        useMask = GET_PARAM(3);

wester committed
245
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

        queryDescCount = 300; // must be even number because we split train data in some cases in two
        countFactor = 4; // do not change it

        cv::RNG& rng = cvtest::TS::ptr()->get_rng();

        cv::Mat queryBuf, trainBuf;

        // Generate query descriptors randomly.
        // Descriptor vector elements are integer values.
        queryBuf.create(queryDescCount, dim, CV_32SC1);
        rng.fill(queryBuf, cv::RNG::UNIFORM, cv::Scalar::all(0), cv::Scalar::all(3));
        queryBuf.convertTo(queryBuf, CV_32FC1);

        // Generate train decriptors as follows:
        // copy each query descriptor to train set countFactor times
        // and perturb some one element of the copied descriptors in
        // in ascending order. General boundaries of the perturbation
        // are (0.f, 1.f).
        trainBuf.create(queryDescCount * countFactor, dim, CV_32FC1);
        float step = 1.f / countFactor;
        for (int qIdx = 0; qIdx < queryDescCount; qIdx++)
        {
            cv::Mat queryDescriptor = queryBuf.row(qIdx);
            for (int c = 0; c < countFactor; c++)
            {
                int tIdx = qIdx * countFactor + c;
                cv::Mat trainDescriptor = trainBuf.row(tIdx);
                queryDescriptor.copyTo(trainDescriptor);
                int elem = rng(dim);
                float diff = rng.uniform(step * c, step * (c + 1));
                trainDescriptor.at<float>(0, elem) += diff;
            }
        }

        queryBuf.convertTo(query, CV_32F);
        trainBuf.convertTo(train, CV_32F);
    }
};

wester committed
286
GPU_TEST_P(BruteForceMatcher, Match_Single)
wester committed
287
{
wester committed
288
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
289

wester committed
290
    cv::gpu::GpuMat mask;
wester committed
291 292 293 294 295 296 297
    if (useMask)
    {
        mask.create(query.rows, train.rows, CV_8UC1);
        mask.setTo(cv::Scalar::all(1));
    }

    std::vector<cv::DMatch> matches;
wester committed
298
    matcher.match(loadMat(query), loadMat(train), matches, mask);
wester committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        cv::DMatch match = matches[i];
        if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
            badCount++;
    }

    ASSERT_EQ(0, badCount);
}

wester committed
313 314
#ifndef OPENCV_TINY_GPU_MODULE
GPU_TEST_P(BruteForceMatcher, Match_Collection)
wester committed
315
{
wester committed
316
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
317

wester committed
318
    cv::gpu::GpuMat d_train(train);
wester committed
319 320

    // make add() twice to test such case
wester committed
321 322
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
wester committed
323 324

    // prepare masks (make first nearest match illegal)
wester committed
325
    std::vector<cv::gpu::GpuMat> masks(2);
wester committed
326 327
    for (int mi = 0; mi < 2; mi++)
    {
wester committed
328
        masks[mi] = cv::gpu::GpuMat(query.rows, train.rows/2, CV_8UC1, cv::Scalar::all(1));
wester committed
329 330 331 332 333 334
        for (int di = 0; di < queryDescCount/2; di++)
            masks[mi].col(di * countFactor).setTo(cv::Scalar::all(0));
    }

    std::vector<cv::DMatch> matches;
    if (useMask)
wester committed
335
        matcher.match(cv::gpu::GpuMat(query), matches, masks);
wester committed
336
    else
wester committed
337
        matcher.match(cv::gpu::GpuMat(query), matches);
wester committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    int shift = useMask ? 1 : 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        cv::DMatch match = matches[i];

        if ((int)i < queryDescCount / 2)
        {
            bool validQueryIdx = (match.queryIdx == (int)i);
            bool validTrainIdx = (match.trainIdx == (int)i * countFactor + shift);
            bool validImgIdx = (match.imgIdx == 0);
            if (!validQueryIdx || !validTrainIdx || !validImgIdx)
                badCount++;
        }
        else
        {
            bool validQueryIdx = (match.queryIdx == (int)i);
            bool validTrainIdx = (match.trainIdx == ((int)i - queryDescCount / 2) * countFactor + shift);
            bool validImgIdx = (match.imgIdx == 1);
            if (!validQueryIdx || !validTrainIdx || !validImgIdx)
                badCount++;
        }
    }

    ASSERT_EQ(0, badCount);
}
wester committed
367
#endif
wester committed
368

wester committed
369
GPU_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
wester committed
370
{
wester committed
371
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
372 373 374

    const int knn = 2;

wester committed
375
    cv::gpu::GpuMat mask;
wester committed
376 377 378 379 380 381 382
    if (useMask)
    {
        mask.create(query.rows, train.rows, CV_8UC1);
        mask.setTo(cv::Scalar::all(1));
    }

    std::vector< std::vector<cv::DMatch> > matches;
wester committed
383
    matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
wester committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        if ((int)matches[i].size() != knn)
            badCount++;
        else
        {
            int localBadCount = 0;
            for (int k = 0; k < knn; k++)
            {
                cv::DMatch match = matches[i][k];
                if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))
                    localBadCount++;
            }
            badCount += localBadCount > 0 ? 1 : 0;
        }
    }

    ASSERT_EQ(0, badCount);
}

wester committed
408
GPU_TEST_P(BruteForceMatcher, KnnMatch_3_Single)
wester committed
409
{
wester committed
410
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
411 412 413

    const int knn = 3;

wester committed
414
    cv::gpu::GpuMat mask;
wester committed
415 416 417 418 419 420 421
    if (useMask)
    {
        mask.create(query.rows, train.rows, CV_8UC1);
        mask.setTo(cv::Scalar::all(1));
    }

    std::vector< std::vector<cv::DMatch> > matches;
wester committed
422
    matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
wester committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        if ((int)matches[i].size() != knn)
            badCount++;
        else
        {
            int localBadCount = 0;
            for (int k = 0; k < knn; k++)
            {
                cv::DMatch match = matches[i][k];
                if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))
                    localBadCount++;
            }
            badCount += localBadCount > 0 ? 1 : 0;
        }
    }

    ASSERT_EQ(0, badCount);
}

wester committed
447 448
#ifndef OPENCV_TINY_GPU_MODULE
GPU_TEST_P(BruteForceMatcher, KnnMatch_2_Collection)
wester committed
449
{
wester committed
450
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
451 452 453

    const int knn = 2;

wester committed
454
    cv::gpu::GpuMat d_train(train);
wester committed
455 456

    // make add() twice to test such case
wester committed
457 458
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
wester committed
459 460

    // prepare masks (make first nearest match illegal)
wester committed
461
    std::vector<cv::gpu::GpuMat> masks(2);
wester committed
462 463
    for (int mi = 0; mi < 2; mi++ )
    {
wester committed
464
        masks[mi] = cv::gpu::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
wester committed
465 466 467 468 469 470 471
        for (int di = 0; di < queryDescCount / 2; di++)
            masks[mi].col(di * countFactor).setTo(cv::Scalar::all(0));
    }

    std::vector< std::vector<cv::DMatch> > matches;

    if (useMask)
wester committed
472
        matcher.knnMatch(cv::gpu::GpuMat(query), matches, knn, masks);
wester committed
473
    else
wester committed
474
        matcher.knnMatch(cv::gpu::GpuMat(query), matches, knn);
wester committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    int shift = useMask ? 1 : 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        if ((int)matches[i].size() != knn)
            badCount++;
        else
        {
            int localBadCount = 0;
            for (int k = 0; k < knn; k++)
            {
                cv::DMatch match = matches[i][k];
                {
                    if ((int)i < queryDescCount / 2)
                    {
                        if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k + shift) || (match.imgIdx != 0) )
                            localBadCount++;
                    }
                    else
                    {
                        if ((match.queryIdx != (int)i) || (match.trainIdx != ((int)i - queryDescCount / 2) * countFactor + k + shift) || (match.imgIdx != 1) )
                            localBadCount++;
                    }
                }
            }
            badCount += localBadCount > 0 ? 1 : 0;
        }
    }

    ASSERT_EQ(0, badCount);
}

wester committed
510
GPU_TEST_P(BruteForceMatcher, KnnMatch_3_Collection)
wester committed
511
{
wester committed
512
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
513 514 515

    const int knn = 3;

wester committed
516
    cv::gpu::GpuMat d_train(train);
wester committed
517 518

    // make add() twice to test such case
wester committed
519 520
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
wester committed
521 522

    // prepare masks (make first nearest match illegal)
wester committed
523
    std::vector<cv::gpu::GpuMat> masks(2);
wester committed
524 525
    for (int mi = 0; mi < 2; mi++ )
    {
wester committed
526
        masks[mi] = cv::gpu::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
wester committed
527 528 529 530 531 532 533
        for (int di = 0; di < queryDescCount / 2; di++)
            masks[mi].col(di * countFactor).setTo(cv::Scalar::all(0));
    }

    std::vector< std::vector<cv::DMatch> > matches;

    if (useMask)
wester committed
534
        matcher.knnMatch(cv::gpu::GpuMat(query), matches, knn, masks);
wester committed
535
    else
wester committed
536
        matcher.knnMatch(cv::gpu::GpuMat(query), matches, knn);
wester committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

    int badCount = 0;
    int shift = useMask ? 1 : 0;
    for (size_t i = 0; i < matches.size(); i++)
    {
        if ((int)matches[i].size() != knn)
            badCount++;
        else
        {
            int localBadCount = 0;
            for (int k = 0; k < knn; k++)
            {
                cv::DMatch match = matches[i][k];
                {
                    if ((int)i < queryDescCount / 2)
                    {
                        if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k + shift) || (match.imgIdx != 0) )
                            localBadCount++;
                    }
                    else
                    {
                        if ((match.queryIdx != (int)i) || (match.trainIdx != ((int)i - queryDescCount / 2) * countFactor + k + shift) || (match.imgIdx != 1) )
                            localBadCount++;
                    }
                }
            }
            badCount += localBadCount > 0 ? 1 : 0;
        }
    }

    ASSERT_EQ(0, badCount);
}
wester committed
571
#endif
wester committed
572

wester committed
573
GPU_TEST_P(BruteForceMatcher, RadiusMatch_Single)
wester committed
574
{
wester committed
575
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
576 577 578

    const float radius = 1.f / countFactor;

wester committed
579
    if (!supportFeature(devInfo, cv::gpu::GLOBAL_ATOMICS))
wester committed
580 581 582 583
    {
        try
        {
            std::vector< std::vector<cv::DMatch> > matches;
wester committed
584
            matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius);
wester committed
585 586 587
        }
        catch (const cv::Exception& e)
        {
wester committed
588
            ASSERT_EQ(CV_StsNotImplemented, e.code);
wester committed
589 590 591 592
        }
    }
    else
    {
wester committed
593
        cv::gpu::GpuMat mask;
wester committed
594 595 596 597 598 599 600
        if (useMask)
        {
            mask.create(query.rows, train.rows, CV_8UC1);
            mask.setTo(cv::Scalar::all(1));
        }

        std::vector< std::vector<cv::DMatch> > matches;
wester committed
601
        matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius, mask);
wester committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

        ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

        int badCount = 0;
        for (size_t i = 0; i < matches.size(); i++)
        {
            if ((int)matches[i].size() != 1)
                badCount++;
            else
            {
                cv::DMatch match = matches[i][0];
                if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i*countFactor) || (match.imgIdx != 0))
                    badCount++;
            }
        }

        ASSERT_EQ(0, badCount);
    }
}

wester committed
622 623
#ifndef OPENCV_TINY_GPU_MODULE
GPU_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
wester committed
624
{
wester committed
625
    cv::gpu::BFMatcher_GPU matcher(normCode);
wester committed
626 627 628 629

    const int n = 3;
    const float radius = 1.f / countFactor * n;

wester committed
630
    cv::gpu::GpuMat d_train(train);
wester committed
631 632

    // make add() twice to test such case
wester committed
633 634
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
    matcher.add(std::vector<cv::gpu::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
wester committed
635 636

    // prepare masks (make first nearest match illegal)
wester committed
637
    std::vector<cv::gpu::GpuMat> masks(2);
wester committed
638 639
    for (int mi = 0; mi < 2; mi++)
    {
wester committed
640
        masks[mi] = cv::gpu::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
wester committed
641 642 643 644
        for (int di = 0; di < queryDescCount / 2; di++)
            masks[mi].col(di * countFactor).setTo(cv::Scalar::all(0));
    }

wester committed
645
    if (!supportFeature(devInfo, cv::gpu::GLOBAL_ATOMICS))
wester committed
646 647 648 649
    {
        try
        {
            std::vector< std::vector<cv::DMatch> > matches;
wester committed
650
            matcher.radiusMatch(cv::gpu::GpuMat(query), matches, radius, masks);
wester committed
651 652 653
        }
        catch (const cv::Exception& e)
        {
wester committed
654
            ASSERT_EQ(CV_StsNotImplemented, e.code);
wester committed
655 656 657 658 659 660 661
        }
    }
    else
    {
        std::vector< std::vector<cv::DMatch> > matches;

        if (useMask)
wester committed
662
            matcher.radiusMatch(cv::gpu::GpuMat(query), matches, radius, masks);
wester committed
663
        else
wester committed
664
            matcher.radiusMatch(cv::gpu::GpuMat(query), matches, radius);
wester committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

        ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());

        int badCount = 0;
        int shift = useMask ? 1 : 0;
        int needMatchCount = useMask ? n-1 : n;
        for (size_t i = 0; i < matches.size(); i++)
        {
            if ((int)matches[i].size() != needMatchCount)
                badCount++;
            else
            {
                int localBadCount = 0;
                for (int k = 0; k < needMatchCount; k++)
                {
                    cv::DMatch match = matches[i][k];
                    {
                        if ((int)i < queryDescCount / 2)
                        {
                            if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k + shift) || (match.imgIdx != 0) )
                                localBadCount++;
                        }
                        else
                        {
                            if ((match.queryIdx != (int)i) || (match.trainIdx != ((int)i - queryDescCount / 2) * countFactor + k + shift) || (match.imgIdx != 1) )
                                localBadCount++;
                        }
                    }
                }
                badCount += localBadCount > 0 ? 1 : 0;
            }
        }

        ASSERT_EQ(0, badCount);
    }
}
wester committed
701
#endif
wester committed
702

wester committed
703 704 705 706 707 708 709 710
#ifdef OPENCV_TINY_GPU_MODULE
INSTANTIATE_TEST_CASE_P(GPU_Features2D, BruteForceMatcher, testing::Combine(
    ALL_DEVICES,
    testing::Values(NormCode(cv::NORM_L2)),
    testing::Values(DescriptorSize(57), DescriptorSize(64), DescriptorSize(83), DescriptorSize(128), DescriptorSize(179), DescriptorSize(256), DescriptorSize(304)),
    testing::Values(UseMask(false), UseMask(true))));
#else
INSTANTIATE_TEST_CASE_P(GPU_Features2D, BruteForceMatcher, testing::Combine(
wester committed
711 712 713 714
    ALL_DEVICES,
    testing::Values(NormCode(cv::NORM_L1), NormCode(cv::NORM_L2)),
    testing::Values(DescriptorSize(57), DescriptorSize(64), DescriptorSize(83), DescriptorSize(128), DescriptorSize(179), DescriptorSize(256), DescriptorSize(304)),
    testing::Values(UseMask(false), UseMask(true))));
wester committed
715
#endif
wester committed
716 717

#endif // HAVE_CUDA