Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / ocl / test / test_brute_force_matcher.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // @Authors
18 //    Nathan, liujun@multicorewareinc.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
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.
29 //
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.
32 //
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.
43 //
44 //M*/
45
46 #include "test_precomp.hpp"
47 #ifdef HAVE_OPENCL
48 namespace
49 {
50     /////////////////////////////////////////////////////////////////////////////////////////////////
51     // BruteForceMatcher
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)
57     {
58         cv::ocl::BruteForceMatcher_OCL_base::DistType distType;
59         int normCode;
60         int dim;
61
62         int queryDescCount;
63         int countFactor;
64
65         cv::Mat query, train;
66
67         virtual void SetUp()
68         {
69             distType = (cv::ocl::BruteForceMatcher_OCL_base::DistType)(int)GET_PARAM(0);
70             dim = GET_PARAM(1);
71
72             queryDescCount = 300; // must be even number because we split train data in some cases in two
73             countFactor = 4; // do not change it
74
75             cv::RNG &rng = cvtest::TS::ptr()->get_rng();
76
77             cv::Mat queryBuf, trainBuf;
78
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);
84
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
89             // are (0.f, 1.f).
90             trainBuf.create(queryDescCount * countFactor, dim, CV_32FC1);
91             float step = 1.f / countFactor;
92             for (int qIdx = 0; qIdx < queryDescCount; qIdx++)
93             {
94                 cv::Mat queryDescriptor = queryBuf.row(qIdx);
95                 for (int c = 0; c < countFactor; c++)
96                 {
97                     int tIdx = qIdx * countFactor + c;
98                     cv::Mat trainDescriptor = trainBuf.row(tIdx);
99                     queryDescriptor.copyTo(trainDescriptor);
100                     int elem = rng(dim);
101                     float diff = rng.uniform(step * c, step * (c + 1));
102                     trainDescriptor.at<float>(0, elem) += diff;
103                 }
104             }
105
106             queryBuf.convertTo(query, CV_32F);
107             trainBuf.convertTo(train, CV_32F);
108         }
109     };
110
111     TEST_P(BruteForceMatcher, Match_Single)
112     {
113         cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
114
115         std::vector<cv::DMatch> matches;
116         matcher.match(cv::ocl::oclMat(query),  cv::ocl::oclMat(train),  matches);
117
118         ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
119
120         int badCount = 0;
121         for (size_t i = 0; i < matches.size(); i++)
122         {
123             cv::DMatch match = matches[i];
124             if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
125                 badCount++;
126         }
127
128         ASSERT_EQ(0, badCount);
129     }
130
131     TEST_P(BruteForceMatcher, KnnMatch_2_Single)
132     {
133         const int knn = 2;
134
135         cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
136
137         std::vector< std::vector<cv::DMatch> > matches;
138         matcher.knnMatch(cv::ocl::oclMat(query), cv::ocl::oclMat(train), matches, knn);
139
140         ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
141
142         int badCount = 0;
143         for (size_t i = 0; i < matches.size(); i++)
144         {
145             if ((int)matches[i].size() != knn)
146                 badCount++;
147             else
148             {
149                 int localBadCount = 0;
150                 for (int k = 0; k < knn; k++)
151                 {
152                     cv::DMatch match = matches[i][k];
153                     if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))
154                         localBadCount++;
155                 }
156                 badCount += localBadCount > 0 ? 1 : 0;
157             }
158         }
159
160         ASSERT_EQ(0, badCount);
161     }
162
163     TEST_P(BruteForceMatcher, RadiusMatch_Single)
164     {
165         float radius = 1.f / countFactor;
166
167         cv::ocl::BruteForceMatcher_OCL_base matcher(distType);
168
169         std::vector< std::vector<cv::DMatch> > matches;
170         matcher.radiusMatch(cv::ocl::oclMat(query), cv::ocl::oclMat(train), matches, radius);
171
172         ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
173
174         int badCount = 0;
175         for (size_t i = 0; i < matches.size(); i++)
176         {
177             if ((int)matches[i].size() != 1)
178             {
179                 badCount++;
180             }
181             else
182             {
183                 cv::DMatch match = matches[i][0];
184                 if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
185                     badCount++;
186             }
187         }
188
189         ASSERT_EQ(0, badCount);
190     }
191
192     INSTANTIATE_TEST_CASE_P(OCL_Features2D, BruteForceMatcher, 
193         testing::Combine(
194         testing::Values(
195             DistType(cv::ocl::BruteForceMatcher_OCL_base::L1Dist),
196             DistType(cv::ocl::BruteForceMatcher_OCL_base::L2Dist)/*, 
197             DistType(cv::ocl::BruteForceMatcher_OCL_base::HammingDist)*/
198         ),
199         testing::Values(
200             DescriptorSize(57), 
201             DescriptorSize(64), 
202             DescriptorSize(83), 
203             DescriptorSize(128), 
204             DescriptorSize(179), 
205             DescriptorSize(256), 
206             DescriptorSize(304))
207         )
208     );
209 } // namespace
210 #endif