1d6fd2bcecfad1c529bc3955fbb979628bf2b299
[platform/upstream/opencv.git] / modules / objdetect / include / opencv2 / objdetect / objdetect_c.h
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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #ifndef __OPENCV_OBJDETECT_C_H__
45 #define __OPENCV_OBJDETECT_C_H__
46
47 #include "opencv2/core/core_c.h"
48
49 #ifdef __cplusplus
50 #include <deque>
51 #include <vector>
52
53 extern "C" {
54 #endif
55
56 /****************************************************************************************\
57 *                         Haar-like Object Detection functions                           *
58 \****************************************************************************************/
59
60 #define CV_HAAR_MAGIC_VAL    0x42500000
61 #define CV_TYPE_NAME_HAAR    "opencv-haar-classifier"
62
63 #define CV_IS_HAAR_CLASSIFIER( haar )                                                    \
64     ((haar) != NULL &&                                                                   \
65     (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
66
67 #define CV_HAAR_FEATURE_MAX  3
68
69 typedef struct CvHaarFeature
70 {
71     int tilted;
72     struct
73     {
74         CvRect r;
75         float weight;
76     } rect[CV_HAAR_FEATURE_MAX];
77 } CvHaarFeature;
78
79 typedef struct CvHaarClassifier
80 {
81     int count;
82     CvHaarFeature* haar_feature;
83     float* threshold;
84     int* left;
85     int* right;
86     float* alpha;
87 } CvHaarClassifier;
88
89 typedef struct CvHaarStageClassifier
90 {
91     int  count;
92     float threshold;
93     CvHaarClassifier* classifier;
94
95     int next;
96     int child;
97     int parent;
98 } CvHaarStageClassifier;
99
100 typedef struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade;
101
102 typedef struct CvHaarClassifierCascade
103 {
104     int  flags;
105     int  count;
106     CvSize orig_window_size;
107     CvSize real_window_size;
108     double scale;
109     CvHaarStageClassifier* stage_classifier;
110     CvHidHaarClassifierCascade* hid_cascade;
111 } CvHaarClassifierCascade;
112
113 typedef struct CvAvgComp
114 {
115     CvRect rect;
116     int neighbors;
117 } CvAvgComp;
118
119 /* Loads haar classifier cascade from a directory.
120    It is obsolete: convert your cascade to xml and use cvLoad instead */
121 CVAPI(CvHaarClassifierCascade*) cvLoadHaarClassifierCascade(
122                     const char* directory, CvSize orig_window_size);
123
124 CVAPI(void) cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade );
125
126 #define CV_HAAR_DO_CANNY_PRUNING    1
127 #define CV_HAAR_SCALE_IMAGE         2
128 #define CV_HAAR_FIND_BIGGEST_OBJECT 4
129 #define CV_HAAR_DO_ROUGH_SEARCH     8
130
131 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
132                      CvHaarClassifierCascade* cascade, CvMemStorage* storage,
133                      double scale_factor CV_DEFAULT(1.1),
134                      int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
135                      CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
136
137 /* sets images for haar classifier cascade */
138 CVAPI(void) cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* cascade,
139                                                 const CvArr* sum, const CvArr* sqsum,
140                                                 const CvArr* tilted_sum, double scale );
141
142 /* runs the cascade on the specified window */
143 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
144                                        CvPoint pt, int start_stage CV_DEFAULT(0));
145
146
147 /****************************************************************************************\
148 *                         Latent SVM Object Detection functions                          *
149 \****************************************************************************************/
150
151 // DataType: STRUCT position
152 // Structure describes the position of the filter in the feature pyramid
153 // l - level in the feature pyramid
154 // (x, y) - coordinate in level l
155 typedef struct CvLSVMFilterPosition
156 {
157     int x;
158     int y;
159     int l;
160 } CvLSVMFilterPosition;
161
162 // DataType: STRUCT filterObject
163 // Description of the filter, which corresponds to the part of the object
164 // V               - ideal (penalty = 0) position of the partial filter
165 //                   from the root filter position (V_i in the paper)
166 // penaltyFunction - vector describes penalty function (d_i in the paper)
167 //                   pf[0] * x + pf[1] * y + pf[2] * x^2 + pf[3] * y^2
168 // FILTER DESCRIPTION
169 //   Rectangular map (sizeX x sizeY),
170 //   every cell stores feature vector (dimension = p)
171 // H               - matrix of feature vectors
172 //                   to set and get feature vectors (i,j)
173 //                   used formula H[(j * sizeX + i) * p + k], where
174 //                   k - component of feature vector in cell (i, j)
175 // END OF FILTER DESCRIPTION
176 typedef struct CvLSVMFilterObject{
177     CvLSVMFilterPosition V;
178     float fineFunction[4];
179     int sizeX;
180     int sizeY;
181     int numFeatures;
182     float *H;
183 } CvLSVMFilterObject;
184
185 // data type: STRUCT CvLatentSvmDetector
186 // structure contains internal representation of trained Latent SVM detector
187 // num_filters          - total number of filters (root plus part) in model
188 // num_components       - number of components in model
189 // num_part_filters     - array containing number of part filters for each component
190 // filters              - root and part filters for all model components
191 // b                    - biases for all model components
192 // score_threshold      - confidence level threshold
193 typedef struct CvLatentSvmDetector
194 {
195     int num_filters;
196     int num_components;
197     int* num_part_filters;
198     CvLSVMFilterObject** filters;
199     float* b;
200     float score_threshold;
201 } CvLatentSvmDetector;
202
203 // data type: STRUCT CvObjectDetection
204 // structure contains the bounding box and confidence level for detected object
205 // rect                 - bounding box for a detected object
206 // score                - confidence level
207 typedef struct CvObjectDetection
208 {
209     CvRect rect;
210     float score;
211 } CvObjectDetection;
212
213 //////////////// Object Detection using Latent SVM //////////////
214
215
216 /*
217 // load trained detector from a file
218 //
219 // API
220 // CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename);
221 // INPUT
222 // filename             - path to the file containing the parameters of
223                         - trained Latent SVM detector
224 // OUTPUT
225 // trained Latent SVM detector in internal representation
226 */
227 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
228
229 /*
230 // release memory allocated for CvLatentSvmDetector structure
231 //
232 // API
233 // void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
234 // INPUT
235 // detector             - CvLatentSvmDetector structure to be released
236 // OUTPUT
237 */
238 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
239
240 /*
241 // find rectangular regions in the given image that are likely
242 // to contain objects and corresponding confidence levels
243 //
244 // API
245 // CvSeq* cvLatentSvmDetectObjects(const IplImage* image,
246 //                                  CvLatentSvmDetector* detector,
247 //                                  CvMemStorage* storage,
248 //                                  float overlap_threshold = 0.5f,
249 //                                  int numThreads = -1);
250 // INPUT
251 // image                - image to detect objects in
252 // detector             - Latent SVM detector in internal representation
253 // storage              - memory storage to store the resultant sequence
254 //                          of the object candidate rectangles
255 // overlap_threshold    - threshold for the non-maximum suppression algorithm
256                            = 0.5f [here will be the reference to original paper]
257 // OUTPUT
258 // sequence of detected objects (bounding boxes and confidence levels stored in CvObjectDetection structures)
259 */
260 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
261                                 CvLatentSvmDetector* detector,
262                                 CvMemStorage* storage,
263                                 float overlap_threshold CV_DEFAULT(0.5f),
264                                 int numThreads CV_DEFAULT(-1));
265
266 #ifdef __cplusplus
267 }
268
269 CV_EXPORTS CvSeq* cvHaarDetectObjectsForROC( const CvArr* image,
270                      CvHaarClassifierCascade* cascade, CvMemStorage* storage,
271                      std::vector<int>& rejectLevels, std::vector<double>& levelWeightds,
272                      double scale_factor = 1.1,
273                      int min_neighbors = 3, int flags = 0,
274                      CvSize min_size = cvSize(0, 0), CvSize max_size = cvSize(0, 0),
275                      bool outputRejectLevels = false );
276
277 struct CvDataMatrixCode
278 {
279   char msg[4];
280   CvMat* original;
281   CvMat* corners;
282 };
283
284 CV_EXPORTS std::deque<CvDataMatrixCode> cvFindDataMatrix(CvMat *im);
285
286 #endif
287
288
289 #endif /* __OPENCV_OBJDETECT_C_H__ */