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 ///////////////////// HOG /////////////////////////////
55 PARAM_TEST_CASE(HOG, Size, int)
62 winSize = GET_PARAM(0);
64 img_rgb = readImage("gpu/hog/road.png");
65 ASSERT_FALSE(img_rgb.empty());
69 OCL_TEST_P(HOG, GetDescriptors)
76 cvtColor(img_rgb, img, CV_BGR2GRAY);
80 cvtColor(img_rgb, img, CV_BGR2BGRA);
83 ocl::oclMat d_img(img);
86 ocl::HOGDescriptor ocl_hog;
87 ocl_hog.gamma_correction = true;
89 hog.gammaCorrection = true;
92 ocl::oclMat d_descriptors;
93 ocl_hog.getDescriptors(d_img, ocl_hog.win_size, d_descriptors, ocl_hog.DESCR_FORMAT_COL_BY_COL);
95 d_descriptors.download(down_descriptors);
96 down_descriptors = down_descriptors.reshape(0, down_descriptors.cols * down_descriptors.rows);
98 hog.setSVMDetector(hog.getDefaultPeopleDetector());
99 std::vector<float> descriptors;
103 hog.compute(img, descriptors, ocl_hog.win_size);
107 hog.compute(img_rgb, descriptors, ocl_hog.win_size);
110 Mat cpu_descriptors(descriptors);
112 EXPECT_MAT_SIMILAR(down_descriptors, cpu_descriptors, 1e-2);
115 OCL_TEST_P(HOG, Detect)
122 cvtColor(img_rgb, img, CV_BGR2GRAY);
126 cvtColor(img_rgb, img, CV_BGR2BGRA);
129 ocl::oclMat d_img(img);
132 if ((winSize != Size(48, 96)) && (winSize != Size(64, 128)))
133 winSize = Size(64, 128);
134 ocl::HOGDescriptor ocl_hog(winSize);
135 ocl_hog.gamma_correction = true;
138 hog.winSize = winSize;
139 hog.gammaCorrection = true;
141 if (winSize.width == 48 && winSize.height == 96)
144 ocl_hog.setSVMDetector(hog.getDaimlerPeopleDetector());
145 hog.setSVMDetector(hog.getDaimlerPeopleDetector());
147 else if (winSize.width == 64 && winSize.height == 128)
149 ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
150 hog.setSVMDetector(hog.getDefaultPeopleDetector());
154 ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
155 hog.setSVMDetector(hog.getDefaultPeopleDetector());
159 std::vector<Rect> d_found;
160 ocl_hog.detectMultiScale(d_img, d_found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
163 std::vector<Rect> found;
167 hog.detectMultiScale(img, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
171 hog.detectMultiScale(img_rgb, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
175 EXPECT_LT(checkRectSimilarity(img.size(), found, d_found), 1.0);
179 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, HOG, testing::Combine(
180 testing::Values(Size(64, 128), Size(48, 96)),
181 testing::Values(MatType(CV_8UC1), MatType(CV_8UC4))));
183 ///////////////////////////// Haar //////////////////////////////
184 IMPLEMENT_PARAM_CLASS(CascadeName, std::string);
185 CascadeName cascade_frontalface_alt(std::string("haarcascade_frontalface_alt.xml"));
186 CascadeName cascade_frontalface_alt2(std::string("haarcascade_frontalface_alt2.xml"));
189 Rect operator ()(const CvAvgComp &e) const
195 PARAM_TEST_CASE(Haar, int, CascadeName)
197 ocl::OclCascadeClassifier cascade, nestedCascade;
198 CascadeClassifier cpucascade, cpunestedCascade;
201 std::string cascadeName;
202 vector<Rect> faces, oclfaces;
208 flags = GET_PARAM(0);
209 cascadeName = (string(cvtest::TS::ptr()->get_data_path()) + "cv/cascadeandhog/cascades/").append(GET_PARAM(1));
210 ASSERT_TRUE(cascade.load( cascadeName ));
211 ASSERT_TRUE(cpucascade.load(cascadeName));
212 img = readImage("cv/shared/lena.png", IMREAD_GRAYSCALE);
213 ASSERT_FALSE(img.empty());
214 equalizeHist(img, img);
219 OCL_TEST_P(Haar, FaceDetect)
221 MemStorage storage(cvCreateMemStorage(0));
223 _objects = cascade.oclHaarDetectObjects(d_img, storage, 1.1, 3,
224 flags, Size(30, 30), Size(0, 0));
225 vector<CvAvgComp> vecAvgComp;
226 Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
227 oclfaces.resize(vecAvgComp.size());
228 std::transform(vecAvgComp.begin(), vecAvgComp.end(), oclfaces.begin(), getRect());
230 cpucascade.detectMultiScale(img, faces, 1.1, 3,
232 Size(30, 30), Size(0, 0));
234 EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
237 OCL_TEST_P(Haar, FaceDetectUseBuf)
239 ocl::OclCascadeClassifierBuf cascadebuf;
240 ASSERT_TRUE(cascadebuf.load(cascadeName)) << "could not load classifier cascade for FaceDetectUseBuf!";
242 cascadebuf.detectMultiScale(d_img, oclfaces, 1.1, 3,
244 Size(30, 30), Size(0, 0));
245 cpucascade.detectMultiScale(img, faces, 1.1, 3,
247 Size(30, 30), Size(0, 0));
249 // intentionally run ocl facedetect again and check if it still works after the first run
250 cascadebuf.detectMultiScale(d_img, oclfaces, 1.1, 3,
253 cascadebuf.release();
255 EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
258 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, Haar,
259 Combine(Values(CV_HAAR_SCALE_IMAGE, 0),
260 Values(cascade_frontalface_alt/*, cascade_frontalface_alt2*/)));