604a96a43157c7b25ae8052090ecdd9e8679f286
[profile/ivi/opencv.git] / modules / ocl / test / test_objdetect.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 //                        Intel 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 //              Yao Wang, bitwangyaoyao@gmail.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 Intel Corporation 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 #include "opencv2/core/core.hpp"
48 #include "opencv2/objdetect/objdetect.hpp"
49
50 using namespace cv;
51 using namespace testing;
52 #ifdef HAVE_OPENCL
53
54 ///////////////////// HOG /////////////////////////////
55 PARAM_TEST_CASE(HOG, Size, int)
56 {
57     Size winSize;
58     int type;
59     Mat img_rgb;
60     virtual void SetUp()
61     {
62         winSize = GET_PARAM(0);
63         type = GET_PARAM(1);
64         img_rgb = readImage("gpu/hog/road.png");
65         ASSERT_FALSE(img_rgb.empty());
66     }
67 };
68
69 OCL_TEST_P(HOG, GetDescriptors)
70 {
71     // Convert image
72     Mat img;
73     switch (type)
74     {
75     case CV_8UC1:
76         cvtColor(img_rgb, img, CV_BGR2GRAY);
77         break;
78     case CV_8UC4:
79     default:
80         cvtColor(img_rgb, img, CV_BGR2BGRA);
81         break;
82     }
83     ocl::oclMat d_img(img);
84
85     // HOGs
86     ocl::HOGDescriptor ocl_hog;
87     ocl_hog.gamma_correction = true;
88     HOGDescriptor hog;
89     hog.gammaCorrection = true;
90
91     // Compute descriptor
92     ocl::oclMat d_descriptors;
93     ocl_hog.getDescriptors(d_img, ocl_hog.win_size, d_descriptors, ocl_hog.DESCR_FORMAT_COL_BY_COL);
94     Mat down_descriptors;
95     d_descriptors.download(down_descriptors);
96     down_descriptors = down_descriptors.reshape(0, down_descriptors.cols * down_descriptors.rows);
97
98     hog.setSVMDetector(hog.getDefaultPeopleDetector());
99     std::vector<float> descriptors;
100     switch (type)
101     {
102     case CV_8UC1:
103         hog.compute(img, descriptors, ocl_hog.win_size);
104         break;
105     case CV_8UC4:
106     default:
107         hog.compute(img_rgb, descriptors, ocl_hog.win_size);
108         break;
109     }
110     Mat cpu_descriptors(descriptors);
111
112     EXPECT_MAT_SIMILAR(down_descriptors, cpu_descriptors, 1e-2);
113 }
114
115 OCL_TEST_P(HOG, Detect)
116 {
117     // Convert image
118     Mat img;
119     switch (type)
120     {
121     case CV_8UC1:
122         cvtColor(img_rgb, img, CV_BGR2GRAY);
123         break;
124     case CV_8UC4:
125     default:
126         cvtColor(img_rgb, img, CV_BGR2BGRA);
127         break;
128     }
129     ocl::oclMat d_img(img);
130
131     // HOGs
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;
136
137     HOGDescriptor hog;
138     hog.winSize = winSize;
139     hog.gammaCorrection = true;
140
141     if (winSize.width == 48 && winSize.height == 96)
142     {
143         // daimler's base
144         ocl_hog.setSVMDetector(hog.getDaimlerPeopleDetector());
145         hog.setSVMDetector(hog.getDaimlerPeopleDetector());
146     }
147     else if (winSize.width == 64 && winSize.height == 128)
148     {
149         ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
150         hog.setSVMDetector(hog.getDefaultPeopleDetector());
151     }
152     else
153     {
154         ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
155         hog.setSVMDetector(hog.getDefaultPeopleDetector());
156     }
157
158     // OpenCL detection
159     std::vector<Rect> d_found;
160     ocl_hog.detectMultiScale(d_img, d_found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
161
162     // CPU detection
163     std::vector<Rect> found;
164     switch (type)
165     {
166     case CV_8UC1:
167         hog.detectMultiScale(img, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
168         break;
169     case CV_8UC4:
170     default:
171         hog.detectMultiScale(img_rgb, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
172         break;
173     }
174
175     EXPECT_LT(checkRectSimilarity(img.size(), found, d_found), 1.0);
176 }
177
178
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))));
182
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"));
187 struct getRect
188 {
189     Rect operator ()(const CvAvgComp &e) const
190     {
191         return e.rect;
192     }
193 };
194
195 PARAM_TEST_CASE(Haar, int, CascadeName)
196 {
197     ocl::OclCascadeClassifier cascade, nestedCascade;
198     CascadeClassifier cpucascade, cpunestedCascade;
199
200     int flags;
201     std::string cascadeName;
202     vector<Rect> faces, oclfaces;
203     Mat img;
204     ocl::oclMat d_img;
205
206     virtual void SetUp()
207     {
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);
215         d_img.upload(img);
216     }
217 };
218
219 OCL_TEST_P(Haar, FaceDetect)
220 {
221     cascade.detectMultiScale(d_img, oclfaces,  1.1, 3,
222                                 flags,
223                                 Size(30, 30), Size(0, 0));
224
225     cpucascade.detectMultiScale(img, faces,  1.1, 3,
226                                 flags,
227                                 Size(30, 30), Size(0, 0));
228
229     EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
230 }
231
232 OCL_TEST_P(Haar, FaceDetectUseBuf)
233 {
234     ocl::OclCascadeClassifierBuf cascadebuf;
235     ASSERT_TRUE(cascadebuf.load(cascadeName)) << "could not load classifier cascade for FaceDetectUseBuf!";
236
237     cascadebuf.detectMultiScale(d_img, oclfaces,  1.1, 3,
238                                 flags,
239                                 Size(30, 30), Size(0, 0));
240     cpucascade.detectMultiScale(img, faces,  1.1, 3,
241                                 flags,
242                                 Size(30, 30), Size(0, 0));
243
244     // intentionally run ocl facedetect again and check if it still works after the first run
245     cascadebuf.detectMultiScale(d_img, oclfaces,  1.1, 3,
246                                 flags,
247                                 Size(30, 30));
248     cascadebuf.release();
249
250     EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
251 }
252
253 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, Haar,
254     Combine(Values(CV_HAAR_SCALE_IMAGE, 0),
255             Values(cascade_frontalface_alt/*, cascade_frontalface_alt2*/)));
256
257 #endif //HAVE_OPENCL