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.
10 // Intel License Agreement
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 // Yao Wang, bitwangyaoyao@gmail.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 Intel Corporation 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"
47 #include "opencv2/core/core.hpp"
48 #include "opencv2/objdetect/objdetect.hpp"
51 using namespace testing;
54 extern string workdir;
56 ///////////////////// HOG /////////////////////////////
57 PARAM_TEST_CASE(HOG, Size, int)
64 winSize = GET_PARAM(0);
66 img_rgb = readImage("gpu/hog/road.png");
67 ASSERT_FALSE(img_rgb.empty());
71 TEST_P(HOG, GetDescriptors)
78 cvtColor(img_rgb, img, CV_BGR2GRAY);
82 cvtColor(img_rgb, img, CV_BGR2BGRA);
85 ocl::oclMat d_img(img);
88 ocl::HOGDescriptor ocl_hog;
89 ocl_hog.gamma_correction = true;
91 hog.gammaCorrection = true;
94 ocl::oclMat d_descriptors;
95 ocl_hog.getDescriptors(d_img, ocl_hog.win_size, d_descriptors, ocl_hog.DESCR_FORMAT_COL_BY_COL);
97 d_descriptors.download(down_descriptors);
98 down_descriptors = down_descriptors.reshape(0, down_descriptors.cols * down_descriptors.rows);
100 hog.setSVMDetector(hog.getDefaultPeopleDetector());
101 std::vector<float> descriptors;
105 hog.compute(img, descriptors, ocl_hog.win_size);
109 hog.compute(img_rgb, descriptors, ocl_hog.win_size);
112 Mat cpu_descriptors(descriptors);
114 EXPECT_MAT_SIMILAR(down_descriptors, cpu_descriptors, 1e-2);
124 cvtColor(img_rgb, img, CV_BGR2GRAY);
128 cvtColor(img_rgb, img, CV_BGR2BGRA);
131 ocl::oclMat d_img(img);
134 if ((winSize != Size(48, 96)) && (winSize != Size(64, 128)))
135 winSize = Size(64, 128);
136 ocl::HOGDescriptor ocl_hog(winSize);
137 ocl_hog.gamma_correction = true;
140 hog.winSize = winSize;
141 hog.gammaCorrection = true;
143 if (winSize.width == 48 && winSize.height == 96)
146 ocl_hog.setSVMDetector(hog.getDaimlerPeopleDetector());
147 hog.setSVMDetector(hog.getDaimlerPeopleDetector());
149 else if (winSize.width == 64 && winSize.height == 128)
151 ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
152 hog.setSVMDetector(hog.getDefaultPeopleDetector());
156 ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
157 hog.setSVMDetector(hog.getDefaultPeopleDetector());
161 std::vector<Rect> d_found;
162 ocl_hog.detectMultiScale(d_img, d_found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
165 std::vector<Rect> found;
169 hog.detectMultiScale(img, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
173 hog.detectMultiScale(img_rgb, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
177 EXPECT_LT(checkRectSimilarity(img.size(), found, d_found), 1.0);
181 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, HOG, testing::Combine(
182 testing::Values(Size(64, 128), Size(48, 96)),
183 testing::Values(MatType(CV_8UC1), MatType(CV_8UC4))));
185 ///////////////////////////// Haar //////////////////////////////
186 IMPLEMENT_PARAM_CLASS(CascadeName, std::string);
187 CascadeName cascade_frontalface_alt(std::string("haarcascade_frontalface_alt.xml"));
188 CascadeName cascade_frontalface_alt2(std::string("haarcascade_frontalface_alt2.xml"));
191 Rect operator ()(const CvAvgComp &e) const
197 PARAM_TEST_CASE(Haar, int, CascadeName)
199 ocl::OclCascadeClassifier cascade, nestedCascade;
200 CascadeClassifier cpucascade, cpunestedCascade;
203 std::string cascadeName;
204 vector<Rect> faces, oclfaces;
210 flags = GET_PARAM(0);
211 cascadeName = (string(cvtest::TS::ptr()->get_data_path()) + "cv/cascadeandhog/cascades/").append(GET_PARAM(1));
212 ASSERT_TRUE(cascade.load( cascadeName ));
213 ASSERT_TRUE(cpucascade.load(cascadeName));
214 img = readImage("cv/shared/lena.png", IMREAD_GRAYSCALE);
215 ASSERT_FALSE(img.empty());
216 equalizeHist(img, img);
221 TEST_P(Haar, FaceDetect)
223 MemStorage storage(cvCreateMemStorage(0));
225 _objects = cascade.oclHaarDetectObjects(d_img, storage, 1.1, 3,
226 flags, Size(30, 30), Size(0, 0));
227 vector<CvAvgComp> vecAvgComp;
228 Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
229 oclfaces.resize(vecAvgComp.size());
230 std::transform(vecAvgComp.begin(), vecAvgComp.end(), oclfaces.begin(), getRect());
232 cpucascade.detectMultiScale(img, faces, 1.1, 3,
234 Size(30, 30), Size(0, 0));
236 EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
239 TEST_P(Haar, FaceDetectUseBuf)
241 ocl::OclCascadeClassifierBuf cascadebuf;
242 if(!cascadebuf.load(cascadeName))
244 std::cout << "ERROR: Could not load classifier cascade for FaceDetectUseBuf!" << std::endl;
247 cascadebuf.detectMultiScale(d_img, oclfaces, 1.1, 3,
249 Size(30, 30), Size(0, 0));
250 cpucascade.detectMultiScale(img, faces, 1.1, 3,
252 Size(30, 30), Size(0, 0));
254 // intentionally run ocl facedetect again and check if it still works after the first run
255 cascadebuf.detectMultiScale(d_img, oclfaces, 1.1, 3,
258 cascadebuf.release();
260 EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
263 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, Haar,
264 Combine(Values(CV_HAAR_SCALE_IMAGE, 0),
265 Values(cascade_frontalface_alt/*, cascade_frontalface_alt2*/)));