Path: blob/master/modules/features2d/test/ocl/test_brute_force_matcher.cpp
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.15// Third party copyrights are property of their respective owners.16//17// @Authors18// Niko Li, [email protected]19// Jia Haipeng, [email protected]20// Zero Lin, [email protected]21// Zhang Ying, [email protected]22// Yao Wang, [email protected]23//24// Redistribution and use in source and binary forms, with or without modification,25// are permitted provided that the following conditions are met:26//27// * Redistribution's of source code must retain the above copyright notice,28// this list of conditions and the following disclaimer.29//30// * Redistribution's in binary form must reproduce the above copyright notice,31// this list of conditions and the following disclaimer in the documentation32// and/or other materials provided with the distribution.33//34// * The name of the copyright holders may not be used to endorse or promote products35// derived from this software without specific prior written permission.36//37// This software is provided by the copyright holders and contributors "as is" and38// any express or implied warranties, including, but not limited to, the implied39// warranties of merchantability and fitness for a particular purpose are disclaimed.40// In no event shall the Intel Corporation or contributors be liable for any direct,41// indirect, incidental, special, exemplary, or consequential damages42// (including, but not limited to, procurement of substitute goods or services;43// loss of use, data, or profits; or business interruption) however caused44// and on any theory of liability, whether in contract, strict liability,45// or tort (including negligence or otherwise) arising in any way out of46// the use of this software, even if advised of the possibility of such damage.47//48//M*/4950#include "../test_precomp.hpp"51#include "cvconfig.h"52#include "opencv2/ts/ocl_test.hpp"5354#ifdef HAVE_OPENCL5556namespace opencv_test {57namespace ocl {58PARAM_TEST_CASE(BruteForceMatcher, int, int)59{60int distType;61int dim;6263int queryDescCount;64int countFactor;6566Mat query, train;67UMat uquery, utrain;6869virtual void SetUp()70{71distType = GET_PARAM(0);72dim = GET_PARAM(1);7374queryDescCount = 300; // must be even number because we split train data in some cases in two75countFactor = 4; // do not change it7677cv::Mat queryBuf, trainBuf;7879// Generate query descriptors randomly.80// Descriptor vector elements are integer values.81queryBuf.create(queryDescCount, dim, CV_32SC1);82rng.fill(queryBuf, cv::RNG::UNIFORM, cv::Scalar::all(0), cv::Scalar::all(3));83queryBuf.convertTo(queryBuf, CV_32FC1);8485// Generate train descriptors as follows:86// copy each query descriptor to train set countFactor times87// and perturb some one element of the copied descriptors in88// in ascending order. General boundaries of the perturbation89// are (0.f, 1.f).90trainBuf.create(queryDescCount * countFactor, dim, CV_32FC1);91float step = 1.f / countFactor;92for (int qIdx = 0; qIdx < queryDescCount; qIdx++)93{94cv::Mat queryDescriptor = queryBuf.row(qIdx);95for (int c = 0; c < countFactor; c++)96{97int tIdx = qIdx * countFactor + c;98cv::Mat trainDescriptor = trainBuf.row(tIdx);99queryDescriptor.copyTo(trainDescriptor);100int elem = rng(dim);101float diff = rng.uniform(step * c, step * (c + 1));102trainDescriptor.at<float>(0, elem) += diff;103}104}105106queryBuf.convertTo(query, CV_32F);107trainBuf.convertTo(train, CV_32F);108query.copyTo(uquery);109train.copyTo(utrain);110}111};112113#ifdef __ANDROID__114OCL_TEST_P(BruteForceMatcher, DISABLED_Match_Single)115#else116OCL_TEST_P(BruteForceMatcher, Match_Single)117#endif118{119BFMatcher matcher(distType);120121std::vector<cv::DMatch> matches;122matcher.match(uquery, utrain, matches);123124ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());125126int badCount = 0;127for (size_t i = 0; i < matches.size(); i++)128{129cv::DMatch match = matches[i];130if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))131badCount++;132}133134ASSERT_EQ(0, badCount);135}136137#ifdef __ANDROID__138OCL_TEST_P(BruteForceMatcher, DISABLED_KnnMatch_2_Single)139#else140OCL_TEST_P(BruteForceMatcher, KnnMatch_2_Single)141#endif142{143const int knn = 2;144145BFMatcher matcher(distType);146147std::vector< std::vector<cv::DMatch> > matches;148matcher.knnMatch(uquery, utrain, matches, knn);149150ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());151152int badCount = 0;153for (size_t i = 0; i < matches.size(); i++)154{155if ((int)matches[i].size() != knn)156badCount++;157else158{159int localBadCount = 0;160for (int k = 0; k < knn; k++)161{162cv::DMatch match = matches[i][k];163if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))164localBadCount++;165}166badCount += localBadCount > 0 ? 1 : 0;167}168}169170ASSERT_EQ(0, badCount);171}172173#ifdef __ANDROID__174OCL_TEST_P(BruteForceMatcher, DISABLED_RadiusMatch_Single)175#else176OCL_TEST_P(BruteForceMatcher, RadiusMatch_Single)177#endif178{179float radius = 1.f / countFactor;180181BFMatcher matcher(distType);182183std::vector< std::vector<cv::DMatch> > matches;184matcher.radiusMatch(uquery, utrain, matches, radius);185186ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());187188int badCount = 0;189for (size_t i = 0; i < matches.size(); i++)190{191if ((int)matches[i].size() != 1)192{193badCount++;194}195else196{197cv::DMatch match = matches[i][0];198if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))199badCount++;200}201}202203ASSERT_EQ(0, badCount);204}205206OCL_INSTANTIATE_TEST_CASE_P(Matcher, BruteForceMatcher, Combine( Values((int)NORM_L1, (int)NORM_L2),207Values(57, 64, 83, 128, 179, 256, 304) ) );208209}//ocl210}//cvtest211212#endif //HAVE_OPENCL213214215