CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / test / test_haar.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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Jia Haipeng, jiahaipeng95@gmail.com
20 //    Sen Liu, swjutls1987@126.com
21 //
22 // Redistribution and use in source and binary forms, with or without modification,
23 // are permitted provided that the following conditions are met:
24 //
25 //   * Redistribution's of source code must retain the above copyright notice,
26 //     this list of conditions and the following disclaimer.
27 //
28 //   * Redistribution's in binary form must reproduce the above copyright notice,
29 //     this list of conditions and the following disclaimer in the documentation
30 //     and/or other oclMaterials provided with the distribution.
31 //
32 //   * The name of the copyright holders may not be used to endorse or promote products
33 //     derived from this software without specific prior written permission.
34 //
35 // This software is provided by the copyright holders and contributors "as is" and
36 // any express or implied warranties, including, but not limited to, the implied
37 // warranties of merchantability and fitness for a particular purpose are disclaimed.
38 // In no event shall the Intel Corporation or contributors be liable for any direct,
39 // indirect, incidental, special, exemplary, or consequential damages
40 // (including, but not limited to, procurement of substitute goods or services;
41 // loss of use, data, or profits; or business interruption) however caused
42 // and on any theory of liability, whether in contract, strict liability,
43 // or tort (including negligence or otherwise) arising in any way out of
44 // the use of this software, even if advised of the possibility of such damage.
45 //
46 //M*/
47
48 #include "opencv2/objdetect/objdetect.hpp"
49 #include "precomp.hpp"
50
51 #ifdef HAVE_OPENCL
52
53 using namespace cvtest;
54 using namespace testing;
55 using namespace std;
56 using namespace cv;
57 extern string workdir;
58
59 namespace
60 {
61 IMPLEMENT_PARAM_CLASS(CascadeName, std::string);
62 CascadeName cascade_frontalface_alt(std::string("haarcascade_frontalface_alt.xml"));
63 CascadeName cascade_frontalface_alt2(std::string("haarcascade_frontalface_alt2.xml"));
64 struct getRect
65 {
66     Rect operator ()(const CvAvgComp &e) const
67     {
68         return e.rect;
69     }
70 };
71 }
72
73 PARAM_TEST_CASE(Haar, double, int, CascadeName)
74 {
75     cv::ocl::OclCascadeClassifier cascade, nestedCascade;
76     cv::CascadeClassifier cpucascade, cpunestedCascade;
77
78     double scale;
79     int flags;
80     std::string cascadeName;
81
82     virtual void SetUp()
83     {
84         scale = GET_PARAM(0);
85         flags = GET_PARAM(1);
86         cascadeName = (workdir + "../../data/haarcascades/").append(GET_PARAM(2));
87
88         if( (!cascade.load( cascadeName )) || (!cpucascade.load(cascadeName)) )
89         {
90             cout << "ERROR: Could not load classifier cascade" << endl;
91             return;
92         }
93     }
94 };
95
96 ////////////////////////////////faceDetect/////////////////////////////////////////////////
97 TEST_P(Haar, FaceDetect)
98 {
99     string imgName = workdir + "lena.jpg";
100     Mat img = imread( imgName, 1 );
101
102     if(img.empty())
103     {
104         std::cout << "Couldn't read " << imgName << std::endl;
105         return ;
106     }
107
108     vector<Rect> faces, oclfaces;
109
110     Mat gray, smallImg(cvRound (img.rows / scale), cvRound(img.cols / scale), CV_8UC1 );
111     MemStorage storage(cvCreateMemStorage(0));
112     cvtColor( img, gray, CV_BGR2GRAY );
113     resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
114     equalizeHist( smallImg, smallImg );
115
116     cv::ocl::oclMat image;
117     CvSeq *_objects;
118     image.upload(smallImg);
119     _objects = cascade.oclHaarDetectObjects( image, storage, 1.1,
120                    3, flags, Size(30, 30), Size(0, 0) );
121     vector<CvAvgComp> vecAvgComp;
122     Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
123     oclfaces.resize(vecAvgComp.size());
124     std::transform(vecAvgComp.begin(), vecAvgComp.end(), oclfaces.begin(), getRect());
125     
126     cpucascade.detectMultiScale( smallImg, faces,  1.1, 3,
127                                  flags,
128                                  Size(30, 30), Size(0, 0) );
129     EXPECT_EQ(faces.size(), oclfaces.size());
130 }
131
132 TEST_P(Haar, FaceDetectUseBuf)
133 {
134     string imgName = workdir + "lena.jpg";
135     Mat img = imread( imgName, 1 );
136
137     if(img.empty())
138     {
139         std::cout << "Couldn't read " << imgName << std::endl;
140         return ;
141     }
142
143     vector<Rect> faces, oclfaces;
144
145     Mat gray, smallImg(cvRound (img.rows / scale), cvRound(img.cols / scale), CV_8UC1 );
146     cvtColor( img, gray, CV_BGR2GRAY );
147     resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
148     equalizeHist( smallImg, smallImg );
149
150     cv::ocl::oclMat image;
151     image.upload(smallImg);
152
153     cv::ocl::OclCascadeClassifierBuf cascadebuf;
154     if( !cascadebuf.load( cascadeName ) )
155     {
156         cout << "ERROR: Could not load classifier cascade for FaceDetectUseBuf!" << endl;
157         return;
158     }
159     cascadebuf.detectMultiScale( image, oclfaces,  1.1, 3,
160                                  flags,
161                                  Size(30, 30), Size(0, 0) );
162
163     cpucascade.detectMultiScale( smallImg, faces,  1.1, 3,
164                                  flags,
165                                  Size(30, 30), Size(0, 0) );
166     EXPECT_EQ(faces.size(), oclfaces.size());
167
168     // intentionally run ocl facedetect again and check if it still works after the first run
169     cascadebuf.detectMultiScale( image, oclfaces,  1.1, 3,
170         flags,
171         Size(30, 30));
172     cascadebuf.release();
173     EXPECT_EQ(faces.size(), oclfaces.size());
174 }
175
176 INSTANTIATE_TEST_CASE_P(FaceDetect, Haar,
177     Combine(Values(1.0),
178             Values(CV_HAAR_SCALE_IMAGE, 0), Values(cascade_frontalface_alt, cascade_frontalface_alt2)));
179
180 #endif // HAVE_OPENCL