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.
11 // For Open Source Computer Vision Library
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.
18 // Jia Haipeng, jiahaipeng95@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 oclMaterials provided with the distribution.
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.
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 "opencv2/objdetect/objdetect.hpp"
47 #include "precomp.hpp"
51 using namespace cvtest;
52 using namespace testing;
55 extern std::string workdir;
58 Rect operator ()(const CvAvgComp &e) const
64 PARAM_TEST_CASE(HaarTestBase, int, int)
66 //std::vector<cv::ocl::Info> oclinfo;
67 cv::ocl::OclCascadeClassifier cascade, nestedCascade;
68 cv::CascadeClassifier cpucascade, cpunestedCascade;
78 string cascadeName = "../../../data/haarcascades/haarcascade_frontalface_alt.xml";
80 if( (!cascade.load( cascadeName )) || (!cpucascade.load(cascadeName)))
82 cout << "ERROR: Could not load classifier cascade" << endl;
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:\\");
93 ////////////////////////////////faceDetect/////////////////////////////////////////////////
95 struct Haar : HaarTestBase {};
97 TEST_F(Haar, FaceDetect)
99 string imgName = workdir + "lena.jpg";
100 Mat img = imread( imgName, 1 );
104 std::cout << imgName << std::endl;
110 vector<Rect> faces, oclfaces;
112 const static Scalar colors[] = { CV_RGB(0, 0, 255),
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 );
128 t = (double)cvGetTickCount();
129 for(int k = 0; k < LOOP_TIMES; k++)
131 cpucascade.detectMultiScale( smallImg, faces, 1.1,
133 | CV_HAAR_SCALE_IMAGE
134 , Size(30, 30), Size(0, 0) );
136 t = (double)cvGetTickCount() - t ;
137 printf( "cpudetection time = %g ms\n", t / (LOOP_TIMES * (double)cvGetTickFrequency() * 1000.) );
139 cv::ocl::oclMat image;
140 CvSeq *_objects=NULL;
141 t = (double)cvGetTickCount();
142 for(int k = 0; k < LOOP_TIMES; k++)
144 image.upload(smallImg);
145 _objects = cascade.oclHaarDetectObjects( image, storage, 1.1,
147 | CV_HAAR_SCALE_IMAGE
148 , Size(30, 30), Size(0, 0) );
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());
157 //for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
161 // Scalar color = colors[i%8];
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 );
168 //namedWindow("result");
169 //imshow("result",img);
171 //destroyAllWindows();
174 #endif // HAVE_OPENCL