1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Nathan, liujun@multicorewareinc.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
26 // * Redistribution's in binary form must reproduce the above copyright notice,
27 // this list of conditions and the following disclaimer in the documentation
28 // and/or other oclMaterials provided with the distribution.
30 // * The name of the copyright holders may not be used to endorse or promote products
31 // derived from this software without specific prior written permission.
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
46 #include "precomp.hpp"
50 /////////////////////////////////////////////////////////////////////////////////////////////////
52 CV_ENUM(DistType, BruteForceMatcher_OCL_base::L1Dist,
53 BruteForceMatcher_OCL_base::L2Dist,
54 BruteForceMatcher_OCL_base::HammingDist)
55 IMPLEMENT_PARAM_CLASS(DescriptorSize, int)
56 PARAM_TEST_CASE(BruteForceMatcher, DistType, DescriptorSize)
58 cv::ocl::BruteForceMatcher_OCL_base::DistType distType;
69 distType = (cv::ocl::BruteForceMatcher_OCL_base::DistType)(int)GET_PARAM(0);
72 queryDescCount = 300; // must be even number because we split train data in some cases in two
73 countFactor = 4; // do not change it
75 cv::RNG &rng = cvtest::TS::ptr()->get_rng();
77 cv::Mat queryBuf, trainBuf;
79 // Generate query descriptors randomly.
80 // Descriptor vector elements are integer values.
81 queryBuf.create(queryDescCount, dim, CV_32SC1);
82 rng.fill(queryBuf, cv::RNG::UNIFORM, cv::Scalar::all(0), cv::Scalar::all(3));
83 queryBuf.convertTo(queryBuf, CV_32FC1);
85 // Generate train decriptors as follows:
86 // copy each query descriptor to train set countFactor times
87 // and perturb some one element of the copied descriptors in
88 // in ascending order. General boundaries of the perturbation
90 trainBuf.create(queryDescCount * countFactor, dim, CV_32FC1);
91 float step = 1.f / countFactor;
92 for (int qIdx = 0; qIdx < queryDescCount; qIdx++)
94 cv::Mat queryDescriptor = queryBuf.row(qIdx);
95 for (int c = 0; c < countFactor; c++)
97 int tIdx = qIdx * countFactor + c;
98 cv::Mat trainDescriptor = trainBuf.row(tIdx);
99 queryDescriptor.copyTo(trainDescriptor);
101 float diff = rng.uniform(step * c, step * (c + 1));
102 trainDescriptor.at<float>(0, elem) += diff;
106 queryBuf.convertTo(query, CV_32F);
107 trainBuf.convertTo(train, CV_32F);
111 TEST_P(BruteForceMatcher, Match_Single)
113 cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
115 std::vector<cv::DMatch> matches;
116 matcher.match(cv::ocl::oclMat(query), cv::ocl::oclMat(train), matches);
118 ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
121 for (size_t i = 0; i < matches.size(); i++)
123 cv::DMatch match = matches[i];
124 if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
128 ASSERT_EQ(0, badCount);
131 TEST_P(BruteForceMatcher, KnnMatch_2_Single)
135 cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
137 std::vector< std::vector<cv::DMatch> > matches;
138 matcher.knnMatch(cv::ocl::oclMat(query), cv::ocl::oclMat(train), matches, knn);
140 ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
143 for (size_t i = 0; i < matches.size(); i++)
145 if ((int)matches[i].size() != knn)
149 int localBadCount = 0;
150 for (int k = 0; k < knn; k++)
152 cv::DMatch match = matches[i][k];
153 if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))
156 badCount += localBadCount > 0 ? 1 : 0;
160 ASSERT_EQ(0, badCount);
163 TEST_P(BruteForceMatcher, RadiusMatch_Single)
165 float radius = 1.f / countFactor;
167 cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
169 std::vector< std::vector<cv::DMatch> > matches;
170 matcher.radiusMatch(cv::ocl::oclMat(query), cv::ocl::oclMat(train), matches, radius);
172 ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
175 for (size_t i = 0; i < matches.size(); i++)
177 if ((int)matches[i].size() != 1)
183 cv::DMatch match = matches[i][0];
184 if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
189 ASSERT_EQ(0, badCount);
192 INSTANTIATE_TEST_CASE_P(OCL_Features2D, BruteForceMatcher,
195 DistType(cv::ocl::BruteForceMatcher_OCL_base::L1Dist),
196 DistType(cv::ocl::BruteForceMatcher_OCL_base::L2Dist)/*,
197 DistType(cv::ocl::BruteForceMatcher_OCL_base::HammingDist)*/