Merge pull request #112 from taka-no-me/warnings
[profile/ivi/opencv.git] / modules / ocl / perf / perf_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 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Jia Haipeng, jiahaipeng95@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 oclMaterials provided with the distribution.
29 //
30 //   * The name of the copyright holders 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 "opencv2/objdetect/objdetect.hpp"
47 #include "precomp.hpp"
48
49 #ifdef HAVE_OPENCL
50
51 using namespace cvtest;
52 using namespace testing;
53 using namespace std;
54 using namespace cv;
55 extern std::string workdir;
56 struct getRect
57 {
58     Rect operator ()(const CvAvgComp &e) const
59     {
60         return e.rect;
61     }
62 };
63
64 PARAM_TEST_CASE(HaarTestBase, int, int)
65 {
66     //std::vector<cv::ocl::Info> oclinfo;
67     cv::ocl::OclCascadeClassifier cascade, nestedCascade;
68     cv::CascadeClassifier cpucascade, cpunestedCascade;
69     //    Mat img;
70
71     double scale;
72     int index;
73
74     virtual void SetUp()
75     {
76         scale = 1.0;
77         index = 0;
78         string cascadeName = "../../../data/haarcascades/haarcascade_frontalface_alt.xml";
79
80         if( (!cascade.load( cascadeName )) || (!cpucascade.load(cascadeName)))
81         {
82             cout << "ERROR: Could not load classifier cascade" << endl;
83             return;
84         }
85         //int devnums = getDevice(oclinfo);
86         //CV_Assert(devnums>0);
87         ////if you want to use undefault device, set it here
88         ////setDevice(oclinfo[0]);
89         //cv::ocl::setBinpath("E:\\");
90     }
91 };
92
93 ////////////////////////////////faceDetect/////////////////////////////////////////////////
94
95 struct Haar : HaarTestBase {};
96
97 TEST_F(Haar, FaceDetect)
98 {
99     string imgName = workdir + "lena.jpg";
100     Mat img = imread( imgName, 1 );
101
102     if(img.empty())
103     {
104         std::cout << imgName << std::endl;
105         return ;
106     }
107
108     //int i = 0;
109     double t = 0;
110     vector<Rect> faces, oclfaces;
111
112     const static Scalar colors[] =  { CV_RGB(0, 0, 255),
113                                       CV_RGB(0, 128, 255),
114                                       CV_RGB(0, 255, 255),
115                                       CV_RGB(0, 255, 0),
116                                       CV_RGB(255, 128, 0),
117                                       CV_RGB(255, 255, 0),
118                                       CV_RGB(255, 0, 0),
119                                       CV_RGB(255, 0, 255)
120                                     } ;
121
122     Mat gray, smallImg(cvRound (img.rows / scale), cvRound(img.cols / scale), CV_8UC1 );
123     MemStorage storage(cvCreateMemStorage(0));
124     cvtColor( img, gray, CV_BGR2GRAY );
125     resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
126     equalizeHist( smallImg, smallImg );
127
128     t = (double)cvGetTickCount();
129     for(int k = 0; k < LOOP_TIMES; k++)
130     {
131         cpucascade.detectMultiScale( smallImg, faces,  1.1,
132                                      3, 0
133                                      | CV_HAAR_SCALE_IMAGE
134                                      , Size(30, 30), Size(0, 0) );
135     }
136     t = (double)cvGetTickCount() - t ;
137     printf( "cpudetection time = %g ms\n", t / (LOOP_TIMES * (double)cvGetTickFrequency() * 1000.) );
138
139     cv::ocl::oclMat image;
140     CvSeq *_objects=NULL;
141     t = (double)cvGetTickCount();
142     for(int k = 0; k < LOOP_TIMES; k++)
143     {
144         image.upload(smallImg);
145         _objects = cascade.oclHaarDetectObjects( image, storage, 1.1,
146                    3, 0
147                    | CV_HAAR_SCALE_IMAGE
148                    , Size(30, 30), Size(0, 0) );
149     }
150     t = (double)cvGetTickCount() - t ;
151     printf( "ocldetection time = %g ms\n", t / (LOOP_TIMES * (double)cvGetTickFrequency() * 1000.) );
152     vector<CvAvgComp> vecAvgComp;
153     Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
154     oclfaces.resize(vecAvgComp.size());
155     std::transform(vecAvgComp.begin(), vecAvgComp.end(), oclfaces.begin(), getRect());
156
157     //for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
158     //{
159     //  Mat smallImgROI;
160     //  Point center;
161     //  Scalar color = colors[i%8];
162     //  int radius;
163     //  center.x = cvRound((r->x + r->width*0.5)*scale);
164     //  center.y = cvRound((r->y + r->height*0.5)*scale);
165     //  radius = cvRound((r->width + r->height)*0.25*scale);
166     //  circle( img, center, radius, color, 3, 8, 0 );
167     //}
168     //namedWindow("result");
169     //imshow("result",img);
170     //waitKey(0);
171     //destroyAllWindows();
172
173 }
174 #endif // HAVE_OPENCL