Merge pull request #1263 from abidrahmank:pyCLAHE_24
[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 extern string workdir;
55
56 ///////////////////// HOG /////////////////////////////
57 PARAM_TEST_CASE(HOG, Size, int)
58 {
59     Size winSize;
60     int type;
61     Mat img_rgb;
62     virtual void SetUp()
63     {
64         winSize = GET_PARAM(0);
65         type = GET_PARAM(1);
66         img_rgb = readImage("gpu/hog/road.png");
67         ASSERT_FALSE(img_rgb.empty());
68     }
69 };
70
71 TEST_P(HOG, GetDescriptors)
72 {
73     // Convert image
74     Mat img;
75     switch (type)
76     {
77     case CV_8UC1:
78         cvtColor(img_rgb, img, CV_BGR2GRAY);
79         break;
80     case CV_8UC4:
81     default:
82         cvtColor(img_rgb, img, CV_BGR2BGRA);
83         break;
84     }
85     ocl::oclMat d_img(img);
86
87     // HOGs
88     ocl::HOGDescriptor ocl_hog;
89     ocl_hog.gamma_correction = true;
90     HOGDescriptor hog;
91     hog.gammaCorrection = true;
92
93     // Compute descriptor
94     ocl::oclMat d_descriptors;
95     ocl_hog.getDescriptors(d_img, ocl_hog.win_size, d_descriptors, ocl_hog.DESCR_FORMAT_COL_BY_COL);
96     Mat down_descriptors;
97     d_descriptors.download(down_descriptors);
98     down_descriptors = down_descriptors.reshape(0, down_descriptors.cols * down_descriptors.rows);
99
100     hog.setSVMDetector(hog.getDefaultPeopleDetector());
101     std::vector<float> descriptors;
102     switch (type)
103     {
104     case CV_8UC1:
105         hog.compute(img, descriptors, ocl_hog.win_size);
106         break;
107     case CV_8UC4:
108     default:
109         hog.compute(img_rgb, descriptors, ocl_hog.win_size);
110         break;
111     }
112     Mat cpu_descriptors(descriptors);
113
114     EXPECT_MAT_SIMILAR(down_descriptors, cpu_descriptors, 1e-2);
115 }
116
117 TEST_P(HOG, Detect)
118 {
119     // Convert image
120     Mat img;
121     switch (type)
122     {
123     case CV_8UC1:
124         cvtColor(img_rgb, img, CV_BGR2GRAY);
125         break;
126     case CV_8UC4:
127     default:
128         cvtColor(img_rgb, img, CV_BGR2BGRA);
129         break;
130     }
131     ocl::oclMat d_img(img);
132
133     // HOGs
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;
138
139     HOGDescriptor hog;
140     hog.winSize = winSize;
141     hog.gammaCorrection = true;
142
143     if (winSize.width == 48 && winSize.height == 96)
144     {
145         // daimler's base
146         ocl_hog.setSVMDetector(hog.getDaimlerPeopleDetector());
147         hog.setSVMDetector(hog.getDaimlerPeopleDetector());
148     }
149     else if (winSize.width == 64 && winSize.height == 128)
150     {
151         ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
152         hog.setSVMDetector(hog.getDefaultPeopleDetector());
153     }
154     else
155     {
156         ocl_hog.setSVMDetector(hog.getDefaultPeopleDetector());
157         hog.setSVMDetector(hog.getDefaultPeopleDetector());
158     }
159
160     // OpenCL detection
161     std::vector<Rect> d_found;
162     ocl_hog.detectMultiScale(d_img, d_found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
163
164     // CPU detection
165     std::vector<Rect> found;
166     switch (type)
167     {
168     case CV_8UC1:
169         hog.detectMultiScale(img, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
170         break;
171     case CV_8UC4:
172     default:
173         hog.detectMultiScale(img_rgb, found, 0, Size(8, 8), Size(0, 0), 1.05, 6);
174         break;
175     }
176
177     EXPECT_LT(checkRectSimilarity(img.size(), found, d_found), 1.0);
178 }
179
180
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))));
184
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"));
189 struct getRect
190 {
191     Rect operator ()(const CvAvgComp &e) const
192     {
193         return e.rect;
194     }
195 };
196
197 PARAM_TEST_CASE(Haar, int, CascadeName)
198 {
199     ocl::OclCascadeClassifier cascade, nestedCascade;
200     CascadeClassifier cpucascade, cpunestedCascade;
201
202     int flags;
203     std::string cascadeName;
204     vector<Rect> faces, oclfaces;
205     Mat img;
206     ocl::oclMat d_img;
207
208     virtual void SetUp()
209     {
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);
217         d_img.upload(img);
218     }
219 };
220
221 TEST_P(Haar, FaceDetect)
222 {
223     MemStorage storage(cvCreateMemStorage(0));
224     CvSeq *_objects;
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());
231     
232     cpucascade.detectMultiScale(img, faces,  1.1, 3,
233                                 flags,
234                                 Size(30, 30), Size(0, 0));
235
236     EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
237 }
238
239 TEST_P(Haar, FaceDetectUseBuf)
240 {
241     ocl::OclCascadeClassifierBuf cascadebuf;
242     if(!cascadebuf.load(cascadeName))
243     {
244         std::cout << "ERROR: Could not load classifier cascade for FaceDetectUseBuf!" << std::endl;
245         return;
246     }
247     cascadebuf.detectMultiScale(d_img, oclfaces,  1.1, 3,
248                                 flags,
249                                 Size(30, 30), Size(0, 0));
250     cpucascade.detectMultiScale(img, faces,  1.1, 3,
251                                 flags,
252                                 Size(30, 30), Size(0, 0));
253
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,
256                                 flags,
257                                 Size(30, 30));
258     cascadebuf.release();
259
260     EXPECT_LT(checkRectSimilarity(img.size(), faces, oclfaces), 1.0);
261 }
262
263 INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, Haar,
264     Combine(Values(CV_HAAR_SCALE_IMAGE, 0), 
265             Values(cascade_frontalface_alt/*, cascade_frontalface_alt2*/)));
266
267 #endif //HAVE_OPENCL