217460a79da33fa6b7f986c44bd7a5d6a0ecaa8b
[profile/ivi/opencv.git] / modules / nonfree / test / test_surf.ocl.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 //    Peng Xiao, pengxiao@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 materials 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
48 #ifdef HAVE_OPENCV_OCL
49
50 using namespace std;
51 using std::tr1::get;
52
53 static bool keyPointsEquals(const cv::KeyPoint& p1, const cv::KeyPoint& p2)
54 {
55     const double maxPtDif = 0.1;
56     const double maxSizeDif = 0.1;
57     const double maxAngleDif = 0.1;
58     const double maxResponseDif = 0.01;
59
60     double dist = cv::norm(p1.pt - p2.pt);
61
62     if (dist < maxPtDif &&
63         fabs(p1.size - p2.size) < maxSizeDif &&
64         abs(p1.angle - p2.angle) < maxAngleDif &&
65         abs(p1.response - p2.response) < maxResponseDif &&
66         p1.octave == p2.octave &&
67         p1.class_id == p2.class_id)
68     {
69         return true;
70     }
71
72     return false;
73 }
74
75 static int getMatchedPointsCount(std::vector<cv::KeyPoint>& gold, std::vector<cv::KeyPoint>& actual)
76 {
77     std::sort(actual.begin(), actual.end(), perf::comparators::KeypointGreater());
78     std::sort(gold.begin(), gold.end(), perf::comparators::KeypointGreater());
79
80     int validCount = 0;
81
82     for (size_t i = 0; i < gold.size(); ++i)
83     {
84         const cv::KeyPoint& p1 = gold[i];
85         const cv::KeyPoint& p2 = actual[i];
86
87         if (keyPointsEquals(p1, p2))
88             ++validCount;
89     }
90
91     return validCount;
92 }
93
94 static int getMatchedPointsCount(const std::vector<cv::KeyPoint>& keypoints1, const std::vector<cv::KeyPoint>& keypoints2, const std::vector<cv::DMatch>& matches)
95 {
96     int validCount = 0;
97
98     for (size_t i = 0; i < matches.size(); ++i)
99     {
100         const cv::DMatch& m = matches[i];
101
102         const cv::KeyPoint& p1 = keypoints1[m.queryIdx];
103         const cv::KeyPoint& p2 = keypoints2[m.trainIdx];
104
105         if (keyPointsEquals(p1, p2))
106             ++validCount;
107     }
108
109     return validCount;
110 }
111
112 IMPLEMENT_PARAM_CLASS(HessianThreshold, double)
113 IMPLEMENT_PARAM_CLASS(Octaves, int)
114 IMPLEMENT_PARAM_CLASS(OctaveLayers, int)
115 IMPLEMENT_PARAM_CLASS(Extended, bool)
116 IMPLEMENT_PARAM_CLASS(Upright, bool)
117
118 PARAM_TEST_CASE(SURF, HessianThreshold, Octaves, OctaveLayers, Extended, Upright)
119 {
120     double hessianThreshold;
121     int nOctaves;
122     int nOctaveLayers;
123     bool extended;
124     bool upright;
125
126     virtual void SetUp()
127     {
128         hessianThreshold = get<0>(GetParam());
129         nOctaves = get<1>(GetParam());
130         nOctaveLayers = get<2>(GetParam());
131         extended = get<3>(GetParam());
132         upright = get<4>(GetParam());
133     }
134 };
135
136 TEST_P(SURF, DISABLED_Detector)
137 {
138     cv::Mat image  = cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE);
139     ASSERT_FALSE(image.empty());
140
141     cv::ocl::SURF_OCL surf;
142     surf.hessianThreshold = static_cast<float>(hessianThreshold);
143     surf.nOctaves = nOctaves;
144     surf.nOctaveLayers = nOctaveLayers;
145     surf.extended = extended;
146     surf.upright = upright;
147     surf.keypointsRatio = 0.05f;
148
149     std::vector<cv::KeyPoint> keypoints;
150     surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints);
151
152     cv::SURF surf_gold;
153     surf_gold.hessianThreshold = hessianThreshold;
154     surf_gold.nOctaves = nOctaves;
155     surf_gold.nOctaveLayers = nOctaveLayers;
156     surf_gold.extended = extended;
157     surf_gold.upright = upright;
158
159     std::vector<cv::KeyPoint> keypoints_gold;
160     surf_gold(image, cv::noArray(), keypoints_gold);
161
162     ASSERT_EQ(keypoints_gold.size(), keypoints.size());
163     int matchedCount = getMatchedPointsCount(keypoints_gold, keypoints);
164     double matchedRatio = static_cast<double>(matchedCount) / keypoints_gold.size();
165
166     EXPECT_GT(matchedRatio, 0.99);
167 }
168
169 TEST_P(SURF, DISABLED_Descriptor)
170 {
171     cv::Mat image  = cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE);
172     ASSERT_FALSE(image.empty());
173
174     cv::ocl::SURF_OCL surf;
175     surf.hessianThreshold = static_cast<float>(hessianThreshold);
176     surf.nOctaves = nOctaves;
177     surf.nOctaveLayers = nOctaveLayers;
178     surf.extended = extended;
179     surf.upright = upright;
180     surf.keypointsRatio = 0.05f;
181
182     cv::SURF surf_gold;
183     surf_gold.hessianThreshold = hessianThreshold;
184     surf_gold.nOctaves = nOctaves;
185     surf_gold.nOctaveLayers = nOctaveLayers;
186     surf_gold.extended = extended;
187     surf_gold.upright = upright;
188
189     std::vector<cv::KeyPoint> keypoints;
190     surf_gold(image, cv::noArray(), keypoints);
191
192     cv::ocl::oclMat descriptors;
193     surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints, descriptors, true);
194
195     cv::Mat descriptors_gold;
196     surf_gold(image, cv::noArray(), keypoints, descriptors_gold, true);
197
198     cv::BFMatcher matcher(surf.defaultNorm());
199     std::vector<cv::DMatch> matches;
200     matcher.match(descriptors_gold, cv::Mat(descriptors), matches);
201
202     int matchedCount = getMatchedPointsCount(keypoints, keypoints, matches);
203     double matchedRatio = static_cast<double>(matchedCount) / keypoints.size();
204
205     EXPECT_GT(matchedRatio, 0.35);
206 }
207
208 INSTANTIATE_TEST_CASE_P(OCL_Features2D, SURF, testing::Combine(
209     testing::Values(HessianThreshold(500.0), HessianThreshold(1000.0)),
210     testing::Values(Octaves(3), Octaves(4)),
211     testing::Values(OctaveLayers(2), OctaveLayers(3)),
212     testing::Values(Extended(false), Extended(true)),
213     testing::Values(Upright(false), Upright(true))));
214
215 #endif // HAVE_OPENCV_OCL