CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / perf / perf_hog.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, 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 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 #include "precomp.hpp"
47
48 ///////////// HOG////////////////////////
49 bool match_rect(cv::Rect r1, cv::Rect r2, int threshold)
50 {
51     return ((abs(r1.x - r2.x) < threshold) && (abs(r1.y - r2.y) < threshold) &&
52         (abs(r1.width - r2.width) < threshold) && (abs(r1.height - r2.height) < threshold));
53 }
54
55 PERFTEST(HOG)
56 {
57     Mat src = imread(abspath("road.png"), cv::IMREAD_GRAYSCALE);
58
59     if (src.empty())
60     {
61         throw runtime_error("can't open road.png");
62     }
63
64
65     cv::HOGDescriptor hog;
66     hog.setSVMDetector(hog.getDefaultPeopleDetector());
67     std::vector<cv::Rect> found_locations;
68     std::vector<cv::Rect> d_found_locations;
69
70     SUBTEST << 768 << 'x' << 576 << "; road.png";
71
72     hog.detectMultiScale(src, found_locations);
73
74     CPU_ON;
75     hog.detectMultiScale(src, found_locations);
76     CPU_OFF;
77
78     cv::ocl::HOGDescriptor ocl_hog;
79     ocl_hog.setSVMDetector(ocl_hog.getDefaultPeopleDetector());
80     ocl::oclMat d_src;
81     d_src.upload(src);
82
83     WARMUP_ON;
84     ocl_hog.detectMultiScale(d_src, d_found_locations);
85     WARMUP_OFF;
86     
87     // Ground-truth rectangular people window
88     cv::Rect win1_64x128(231, 190, 72, 144);
89     cv::Rect win2_64x128(621, 156, 97, 194);
90     cv::Rect win1_48x96(238, 198, 63, 126);
91     cv::Rect win2_48x96(619, 161, 92, 185);
92     cv::Rect win3_48x96(488, 136, 56, 112);
93
94     // Compare whether ground-truth windows are detected and compare the number of windows detected.
95     std::vector<int> d_comp(4);
96     std::vector<int> comp(4);
97     for(int i = 0; i < (int)d_comp.size(); i++)
98     {
99         d_comp[i] = 0;
100         comp[i] = 0;
101     }
102
103     int threshold = 10;
104     int val = 32;
105     d_comp[0] = (int)d_found_locations.size();
106     comp[0] = (int)found_locations.size();
107
108     cv::Size winSize = hog.winSize;
109
110     if (winSize == cv::Size(48, 96))
111     {
112         for(int i = 0; i < (int)d_found_locations.size(); i++)
113         {
114             if (match_rect(d_found_locations[i], win1_48x96, threshold))
115                 d_comp[1] = val;
116             if (match_rect(d_found_locations[i], win2_48x96, threshold))
117                 d_comp[2] = val;
118             if (match_rect(d_found_locations[i], win3_48x96, threshold))
119                 d_comp[3] = val;
120         }
121         for(int i = 0; i < (int)found_locations.size(); i++)
122         {
123             if (match_rect(found_locations[i], win1_48x96, threshold))
124                 comp[1] = val;
125             if (match_rect(found_locations[i], win2_48x96, threshold))
126                 comp[2] = val;
127             if (match_rect(found_locations[i], win3_48x96, threshold))
128                 comp[3] = val;
129         }
130     }
131     else if (winSize == cv::Size(64, 128))
132     {
133         for(int i = 0; i < (int)d_found_locations.size(); i++)
134         {
135             if (match_rect(d_found_locations[i], win1_64x128, threshold))
136                 d_comp[1] = val;
137             if (match_rect(d_found_locations[i], win2_64x128, threshold))
138                 d_comp[2] = val;
139         }
140         for(int i = 0; i < (int)found_locations.size(); i++)
141         {
142             if (match_rect(found_locations[i], win1_64x128, threshold))
143                 comp[1] = val;
144             if (match_rect(found_locations[i], win2_64x128, threshold))
145                 comp[2] = val;
146         }
147     }
148
149     cv::Mat gpu_rst(d_comp), cpu_rst(comp);
150     TestSystem::instance().ExpectedMatNear(gpu_rst, cpu_rst, 3);
151
152     GPU_ON;
153     ocl_hog.detectMultiScale(d_src, found_locations);
154     GPU_OFF;
155
156     GPU_FULL_ON;
157     d_src.upload(src);
158     ocl_hog.detectMultiScale(d_src, found_locations);
159     GPU_FULL_OFF;
160 }