opencv/modules/cudafeatures2d/test/test_features2d.cpp

704 lines
23 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
2012-10-17 07:18:30 +08:00
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
2012-10-17 07:18:30 +08:00
// 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
2012-10-17 07:18:30 +08:00
// 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;
2012-10-17 07:18:30 +08:00
/////////////////////////////////////////////////////////////////////////////////////////////////
// FAST
namespace
{
IMPLEMENT_PARAM_CLASS(FAST_Threshold, int)
2014-03-03 23:37:47 +08:00
IMPLEMENT_PARAM_CLASS(FAST_NonmaxSuppression, bool)
}
2012-10-17 07:18:30 +08:00
Merge remote-tracking branch 'origin/2.4' into merge-2.4 Conflicts: modules/calib3d/perf/perf_pnp.cpp modules/contrib/src/imagelogpolprojection.cpp modules/contrib/src/templatebuffer.hpp modules/core/perf/opencl/perf_gemm.cpp modules/cudafeatures2d/doc/feature_detection_and_description.rst modules/cudafeatures2d/perf/perf_features2d.cpp modules/cudafeatures2d/src/fast.cpp modules/cudafeatures2d/test/test_features2d.cpp modules/features2d/doc/feature_detection_and_description.rst modules/features2d/include/opencv2/features2d/features2d.hpp modules/features2d/perf/opencl/perf_brute_force_matcher.cpp modules/gpu/include/opencv2/gpu/gpu.hpp modules/gpu/perf/perf_imgproc.cpp modules/gpu/perf4au/main.cpp modules/imgproc/perf/opencl/perf_blend.cpp modules/imgproc/perf/opencl/perf_color.cpp modules/imgproc/perf/opencl/perf_moments.cpp modules/imgproc/perf/opencl/perf_pyramid.cpp modules/objdetect/perf/opencl/perf_hogdetect.cpp modules/ocl/perf/perf_arithm.cpp modules/ocl/perf/perf_bgfg.cpp modules/ocl/perf/perf_blend.cpp modules/ocl/perf/perf_brute_force_matcher.cpp modules/ocl/perf/perf_canny.cpp modules/ocl/perf/perf_filters.cpp modules/ocl/perf/perf_gftt.cpp modules/ocl/perf/perf_haar.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_imgwarp.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/perf_matrix_operation.cpp modules/ocl/perf/perf_ml.cpp modules/ocl/perf/perf_moments.cpp modules/ocl/perf/perf_opticalflow.cpp modules/ocl/perf/perf_precomp.hpp modules/ocl/src/cl_context.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/video/src/lkpyramid.cpp modules/video/src/precomp.hpp samples/gpu/morphology.cpp
2014-03-11 21:20:01 +08:00
PARAM_TEST_CASE(FAST, cv::cuda::DeviceInfo, FAST_Threshold, FAST_NonmaxSuppression)
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
int threshold;
2014-03-03 23:37:47 +08:00
bool nonmaxSuppression;
2012-10-17 07:18:30 +08:00
virtual void SetUp()
{
devInfo = GET_PARAM(0);
threshold = GET_PARAM(1);
2014-03-03 23:37:47 +08:00
nonmaxSuppression = GET_PARAM(2);
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
}
};
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(FAST, Accuracy)
2012-10-17 07:18:30 +08:00
{
cv::Mat image = readImage("features2d/aloe.png", cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(image.empty());
2013-07-24 17:55:18 +08:00
cv::cuda::FAST_CUDA fast(threshold);
2014-03-03 23:37:47 +08:00
fast.nonmaxSuppression = nonmaxSuppression;
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
if (!supportFeature(devInfo, cv::cuda::GLOBAL_ATOMICS))
2012-10-17 07:18:30 +08:00
{
try
{
std::vector<cv::KeyPoint> keypoints;
2013-08-28 19:45:13 +08:00
fast(loadMat(image), cv::cuda::GpuMat(), keypoints);
2012-10-17 07:18:30 +08:00
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsNotImplemented, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
std::vector<cv::KeyPoint> keypoints;
2013-08-28 19:45:13 +08:00
fast(loadMat(image), cv::cuda::GpuMat(), keypoints);
2012-10-17 07:18:30 +08:00
std::vector<cv::KeyPoint> keypoints_gold;
2014-03-03 23:37:47 +08:00
cv::FAST(image, keypoints_gold, threshold, nonmaxSuppression);
2012-10-17 07:18:30 +08:00
ASSERT_KEYPOINTS_EQ(keypoints_gold, keypoints);
}
}
2013-07-24 17:55:18 +08:00
INSTANTIATE_TEST_CASE_P(CUDA_Features2D, FAST, testing::Combine(
2012-10-17 07:18:30 +08:00
ALL_DEVICES,
testing::Values(FAST_Threshold(25), FAST_Threshold(50)),
2014-03-03 23:37:47 +08:00
testing::Values(FAST_NonmaxSuppression(false), FAST_NonmaxSuppression(true))));
2012-10-17 07:18:30 +08:00
/////////////////////////////////////////////////////////////////////////////////////////////////
// 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)
}
2012-10-17 07:18:30 +08:00
CV_ENUM(ORB_ScoreType, ORB::HARRIS_SCORE, ORB::FAST_SCORE)
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
PARAM_TEST_CASE(ORB, cv::cuda::DeviceInfo, ORB_FeaturesCount, ORB_ScaleFactor, ORB_LevelsCount, ORB_EdgeThreshold, ORB_firstLevel, ORB_WTA_K, ORB_ScoreType, ORB_PatchSize, ORB_BlurForDescriptor)
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
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);
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
}
};
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(ORB, Accuracy)
2012-10-17 07:18:30 +08:00
{
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));
2013-07-24 17:55:18 +08:00
cv::cuda::ORB_CUDA orb(nFeatures, scaleFactor, nLevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize);
2012-10-17 07:18:30 +08:00
orb.blurForDescriptor = blurForDescriptor;
2013-08-28 19:45:13 +08:00
if (!supportFeature(devInfo, cv::cuda::GLOBAL_ATOMICS))
2012-10-17 07:18:30 +08:00
{
try
{
std::vector<cv::KeyPoint> keypoints;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat descriptors;
2012-10-17 07:18:30 +08:00
orb(loadMat(image), loadMat(mask), keypoints, descriptors);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsNotImplemented, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
std::vector<cv::KeyPoint> keypoints;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat descriptors;
2012-10-17 07:18:30 +08:00
orb(loadMat(image), loadMat(mask), keypoints, descriptors);
cv::Ptr<cv::ORB> orb_gold = cv::ORB::create(nFeatures, scaleFactor, nLevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize);
2012-10-17 07:18:30 +08:00
std::vector<cv::KeyPoint> keypoints_gold;
cv::Mat descriptors_gold;
orb_gold->detectAndCompute(image, mask, keypoints_gold, descriptors_gold);
2012-10-17 07:18:30 +08:00
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);
}
}
2013-07-24 17:55:18 +08:00
INSTANTIATE_TEST_CASE_P(CUDA_Features2D, ORB, testing::Combine(
2012-10-17 07:18:30 +08:00
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)),
testing::Values(ORB_firstLevel(0), ORB_firstLevel(2)),
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)
}
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
PARAM_TEST_CASE(BruteForceMatcher, cv::cuda::DeviceInfo, NormCode, DescriptorSize, UseMask)
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
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);
2013-08-28 19:45:13 +08:00
cv::cuda::setDevice(devInfo.deviceID());
2012-10-17 07:18:30 +08:00
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);
}
};
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, Match_Single)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mask;
2012-10-17 07:18:30 +08:00
if (useMask)
{
mask.create(query.rows, train.rows, CV_8UC1);
mask.setTo(cv::Scalar::all(1));
}
std::vector<cv::DMatch> matches;
matcher.match(loadMat(query), loadMat(train), matches, mask);
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, Match_Collection)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_train(train);
2012-10-17 07:18:30 +08:00
// make add() twice to test such case
2013-08-28 19:45:13 +08:00
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
2012-10-17 07:18:30 +08:00
// prepare masks (make first nearest match illegal)
2013-08-28 19:45:13 +08:00
std::vector<cv::cuda::GpuMat> masks(2);
2012-10-17 07:18:30 +08:00
for (int mi = 0; mi < 2; mi++)
{
2013-08-28 19:45:13 +08:00
masks[mi] = cv::cuda::GpuMat(query.rows, train.rows/2, CV_8UC1, cv::Scalar::all(1));
2012-10-17 07:18:30 +08:00
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)
2013-08-28 19:45:13 +08:00
matcher.match(cv::cuda::GpuMat(query), matches, masks);
2012-10-17 07:18:30 +08:00
else
2013-08-28 19:45:13 +08:00
matcher.match(cv::cuda::GpuMat(query), matches);
2012-10-17 07:18:30 +08:00
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const int knn = 2;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mask;
2012-10-17 07:18:30 +08:00
if (useMask)
{
mask.create(query.rows, train.rows, CV_8UC1);
mask.setTo(cv::Scalar::all(1));
}
std::vector< std::vector<cv::DMatch> > matches;
matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Single)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const int knn = 3;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mask;
2012-10-17 07:18:30 +08:00
if (useMask)
{
mask.create(query.rows, train.rows, CV_8UC1);
mask.setTo(cv::Scalar::all(1));
}
std::vector< std::vector<cv::DMatch> > matches;
matcher.knnMatch(loadMat(query), loadMat(train), matches, knn, mask);
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, KnnMatch_2_Collection)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const int knn = 2;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_train(train);
2012-10-17 07:18:30 +08:00
// make add() twice to test such case
2013-08-28 19:45:13 +08:00
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
2012-10-17 07:18:30 +08:00
// prepare masks (make first nearest match illegal)
2013-08-28 19:45:13 +08:00
std::vector<cv::cuda::GpuMat> masks(2);
2012-10-17 07:18:30 +08:00
for (int mi = 0; mi < 2; mi++ )
{
2013-08-28 19:45:13 +08:00
masks[mi] = cv::cuda::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
2012-10-17 07:18:30 +08:00
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)
2013-08-28 19:45:13 +08:00
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn, masks);
2012-10-17 07:18:30 +08:00
else
2013-08-28 19:45:13 +08:00
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn);
2012-10-17 07:18:30 +08:00
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, KnnMatch_3_Collection)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const int knn = 3;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_train(train);
2012-10-17 07:18:30 +08:00
// make add() twice to test such case
2013-08-28 19:45:13 +08:00
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
2012-10-17 07:18:30 +08:00
// prepare masks (make first nearest match illegal)
2013-08-28 19:45:13 +08:00
std::vector<cv::cuda::GpuMat> masks(2);
2012-10-17 07:18:30 +08:00
for (int mi = 0; mi < 2; mi++ )
{
2013-08-28 19:45:13 +08:00
masks[mi] = cv::cuda::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
2012-10-17 07:18:30 +08:00
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)
2013-08-28 19:45:13 +08:00
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn, masks);
2012-10-17 07:18:30 +08:00
else
2013-08-28 19:45:13 +08:00
matcher.knnMatch(cv::cuda::GpuMat(query), matches, knn);
2012-10-17 07:18:30 +08:00
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);
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Single)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const float radius = 1.f / countFactor;
2013-08-28 19:45:13 +08:00
if (!supportFeature(devInfo, cv::cuda::GLOBAL_ATOMICS))
2012-10-17 07:18:30 +08:00
{
try
{
std::vector< std::vector<cv::DMatch> > matches;
matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius);
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsNotImplemented, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat mask;
2012-10-17 07:18:30 +08:00
if (useMask)
{
mask.create(query.rows, train.rows, CV_8UC1);
mask.setTo(cv::Scalar::all(1));
}
std::vector< std::vector<cv::DMatch> > matches;
matcher.radiusMatch(loadMat(query), loadMat(train), matches, radius, mask);
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);
}
}
2013-07-24 17:55:18 +08:00
CUDA_TEST_P(BruteForceMatcher, RadiusMatch_Collection)
2012-10-17 07:18:30 +08:00
{
2013-07-24 17:55:18 +08:00
cv::cuda::BFMatcher_CUDA matcher(normCode);
2012-10-17 07:18:30 +08:00
const int n = 3;
const float radius = 1.f / countFactor * n;
2013-08-28 19:45:13 +08:00
cv::cuda::GpuMat d_train(train);
2012-10-17 07:18:30 +08:00
// make add() twice to test such case
2013-08-28 19:45:13 +08:00
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(0, train.rows / 2)));
matcher.add(std::vector<cv::cuda::GpuMat>(1, d_train.rowRange(train.rows / 2, train.rows)));
2012-10-17 07:18:30 +08:00
// prepare masks (make first nearest match illegal)
2013-08-28 19:45:13 +08:00
std::vector<cv::cuda::GpuMat> masks(2);
2012-10-17 07:18:30 +08:00
for (int mi = 0; mi < 2; mi++)
{
2013-08-28 19:45:13 +08:00
masks[mi] = cv::cuda::GpuMat(query.rows, train.rows / 2, CV_8UC1, cv::Scalar::all(1));
2012-10-17 07:18:30 +08:00
for (int di = 0; di < queryDescCount / 2; di++)
masks[mi].col(di * countFactor).setTo(cv::Scalar::all(0));
}
2013-08-28 19:45:13 +08:00
if (!supportFeature(devInfo, cv::cuda::GLOBAL_ATOMICS))
2012-10-17 07:18:30 +08:00
{
try
{
std::vector< std::vector<cv::DMatch> > matches;
2013-08-28 19:45:13 +08:00
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks);
2012-10-17 07:18:30 +08:00
}
catch (const cv::Exception& e)
{
2013-04-17 19:24:53 +08:00
ASSERT_EQ(cv::Error::StsNotImplemented, e.code);
2012-10-17 07:18:30 +08:00
}
}
else
{
std::vector< std::vector<cv::DMatch> > matches;
if (useMask)
2013-08-28 19:45:13 +08:00
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius, masks);
2012-10-17 07:18:30 +08:00
else
2013-08-28 19:45:13 +08:00
matcher.radiusMatch(cv::cuda::GpuMat(query), matches, radius);
2012-10-17 07:18:30 +08:00
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);
}
}
2013-07-24 17:55:18 +08:00
INSTANTIATE_TEST_CASE_P(CUDA_Features2D, BruteForceMatcher, testing::Combine(
2012-10-17 07:18:30 +08:00
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))));
#endif // HAVE_CUDA