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 // Peng Xiao, pengxiao@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 materials 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 "test_precomp.hpp"
48 #ifdef HAVE_OPENCV_OCL
53 static bool keyPointsEquals(const cv::KeyPoint& p1, const cv::KeyPoint& p2)
55 const double maxPtDif = 0.1;
56 const double maxSizeDif = 0.1;
57 const double maxAngleDif = 0.1;
58 const double maxResponseDif = 0.01;
60 double dist = cv::norm(p1.pt - p2.pt);
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)
75 static int getMatchedPointsCount(std::vector<cv::KeyPoint>& gold, std::vector<cv::KeyPoint>& actual)
77 std::sort(actual.begin(), actual.end(), perf::comparators::KeypointGreater());
78 std::sort(gold.begin(), gold.end(), perf::comparators::KeypointGreater());
82 for (size_t i = 0; i < gold.size(); ++i)
84 const cv::KeyPoint& p1 = gold[i];
85 const cv::KeyPoint& p2 = actual[i];
87 if (keyPointsEquals(p1, p2))
94 static int getMatchedPointsCount(const std::vector<cv::KeyPoint>& keypoints1, const std::vector<cv::KeyPoint>& keypoints2, const std::vector<cv::DMatch>& matches)
98 for (size_t i = 0; i < matches.size(); ++i)
100 const cv::DMatch& m = matches[i];
102 const cv::KeyPoint& p1 = keypoints1[m.queryIdx];
103 const cv::KeyPoint& p2 = keypoints2[m.trainIdx];
105 if (keyPointsEquals(p1, p2))
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)
118 PARAM_TEST_CASE(SURF, HessianThreshold, Octaves, OctaveLayers, Extended, Upright)
120 double hessianThreshold;
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());
136 TEST_P(SURF, DISABLED_Detector)
138 cv::Mat image = cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE);
139 ASSERT_FALSE(image.empty());
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;
149 std::vector<cv::KeyPoint> keypoints;
150 surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints);
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;
159 std::vector<cv::KeyPoint> keypoints_gold;
160 surf_gold(image, cv::noArray(), keypoints_gold);
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();
166 EXPECT_GT(matchedRatio, 0.99);
169 TEST_P(SURF, DISABLED_Descriptor)
171 cv::Mat image = cv::imread(string(cvtest::TS::ptr()->get_data_path()) + "shared/fruits.png", cv::IMREAD_GRAYSCALE);
172 ASSERT_FALSE(image.empty());
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;
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;
189 std::vector<cv::KeyPoint> keypoints;
190 surf_gold(image, cv::noArray(), keypoints);
192 cv::ocl::oclMat descriptors;
193 surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints, descriptors, true);
195 cv::Mat descriptors_gold;
196 surf_gold(image, cv::noArray(), keypoints, descriptors_gold, true);
198 cv::BFMatcher matcher(surf.defaultNorm());
199 std::vector<cv::DMatch> matches;
200 matcher.match(descriptors_gold, cv::Mat(descriptors), matches);
202 int matchedCount = getMatchedPointsCount(keypoints, keypoints, matches);
203 double matchedRatio = static_cast<double>(matchedCount) / keypoints.size();
205 EXPECT_GT(matchedRatio, 0.35);
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))));
215 #endif // HAVE_OPENCV_OCL