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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 
42 #ifndef _CV_HPP_
43 #define _CV_HPP_
44 
45 #ifdef __cplusplus
46 
47 /****************************************************************************************\
48 *                    CvBaseImageFilter: Base class for filtering operations              *
49 \****************************************************************************************/
50 
51 #define CV_WHOLE   0
52 #define CV_START   1
53 #define CV_END     2
54 #define CV_MIDDLE  4
55 #define CV_ISOLATED_ROI 8
56 
57 typedef void (*CvRowFilterFunc)( const uchar* src, uchar* dst, void* params );
58 typedef void (*CvColumnFilterFunc)( uchar** src, uchar* dst, int dst_step, int count, void* params );
59 
60 class CV_EXPORTS CvBaseImageFilter
61 {
62 public:
63     CvBaseImageFilter();
64     /* calls init() */
65     CvBaseImageFilter( int _max_width, int _src_type, int _dst_type,
66                        bool _is_separable, CvSize _ksize,
67                        CvPoint _anchor=cvPoint(-1,-1),
68                        int _border_mode=IPL_BORDER_REPLICATE,
69                        CvScalar _border_value=cvScalarAll(0) );
70     virtual ~CvBaseImageFilter();
71 
72     /* initializes the class for processing an image of maximal width _max_width,
73        input image has data type _src_type, the output will have _dst_type.
74        _is_separable != 0 if the filter is separable
75        (specific behaviour is defined in a derived class), 0 otherwise.
76        _ksize and _anchor specify the kernel size and the anchor point. _anchor=(-1,-1) means
77        that the anchor is at the center.
78        to get interpolate pixel values outside the image _border_mode=IPL_BORDER_*** is used,
79        _border_value specify the pixel value in case of IPL_BORDER_CONSTANT border mode.
80        before initialization clear() is called if necessary.
81     */
82     virtual void init( int _max_width, int _src_type, int _dst_type,
83                        bool _is_separable, CvSize _ksize,
84                        CvPoint _anchor=cvPoint(-1,-1),
85                        int _border_mode=IPL_BORDER_REPLICATE,
86                        CvScalar _border_value=cvScalarAll(0) );
87     /* releases all the internal buffers.
88        for the further use of the object, init() needs to be called. */
89     virtual void clear();
90     /* processes input image or a part of it.
91        input is represented either as matrix (CvMat* src)
92        or a list of row pointers (uchar** src2).
93        in the later case width, _src_y1 and _src_y2 are used to specify the size.
94        _dst is the output image/matrix.
95        _src_roi specifies the roi inside the input image to process,
96           (0,0,-1,-1) denotes the whole image.
97        _dst_origin is the upper-left corner of the filtered roi within the output image.
98        _phase is either CV_START, or CV_END, or CV_MIDDLE, or CV_START|CV_END, or CV_WHOLE,
99           which is the same as CV_START|CV_END.
100           CV_START means that the input is the first (top) stripe of the processed image [roi],
101           CV_END - the input is the last (bottom) stripe of the processed image [roi],
102           CV_MIDDLE - the input is neither first nor last stripe.
103           CV_WHOLE - the input is the whole processed image [roi].
104     */
105     virtual int process( const CvMat* _src, CvMat* _dst,
106                          CvRect _src_roi=cvRect(0,0,-1,-1),
107                          CvPoint _dst_origin=cvPoint(0,0), int _flags=0 );
108     /* retrieve various parameters of the filtering object */
get_src_type() const109     int get_src_type() const { return src_type; }
get_dst_type() const110     int get_dst_type() const { return dst_type; }
get_work_type() const111     int get_work_type() const { return work_type; }
get_kernel_size() const112     CvSize get_kernel_size() const { return ksize; }
get_anchor() const113     CvPoint get_anchor() const { return anchor; }
get_width() const114     int get_width() const { return prev_x_range.end_index - prev_x_range.start_index; }
get_x_filter_func() const115     CvRowFilterFunc get_x_filter_func() const { return x_func; }
get_y_filter_func() const116     CvColumnFilterFunc get_y_filter_func() const { return y_func; }
117 
118 protected:
119     /* initializes work_type, buf_size and max_rows */
120     virtual void get_work_params();
121     /* it is called (not always) from process when _phase=CV_START or CV_WHOLE.
122        the method initializes ring buffer (buf_end, buf_head, buf_tail, buf_count, rows),
123        prev_width, prev_x_range, const_row, border_tab, border_tab_sz* */
124     virtual void start_process( CvSlice x_range, int width );
125     /* forms pointers to "virtual rows" above or below the processed roi using the specified
126        border mode */
127     virtual void make_y_border( int row_count, int top_rows, int bottom_rows );
128 
129     virtual int fill_cyclic_buffer( const uchar* src, int src_step,
130                                     int y, int y1, int y2 );
131 
132     enum { ALIGN=32 };
133 
134     int max_width;
135     /* currently, work_type must be the same as src_type in case of non-separable filters */
136     int min_depth, src_type, dst_type, work_type;
137 
138     /* pointers to convolution functions, initialized by init method.
139        for non-separable filters only y_conv should be set */
140     CvRowFilterFunc x_func;
141     CvColumnFilterFunc y_func;
142 
143     uchar* buffer;
144     uchar** rows;
145     int top_rows, bottom_rows, max_rows;
146     uchar *buf_start, *buf_end, *buf_head, *buf_tail;
147     int buf_size, buf_step, buf_count, buf_max_count;
148 
149     bool is_separable;
150     CvSize ksize;
151     CvPoint anchor;
152     int max_ky, border_mode;
153     CvScalar border_value;
154     uchar* const_row;
155     int* border_tab;
156     int border_tab_sz1, border_tab_sz;
157 
158     CvSlice prev_x_range;
159     int prev_width;
160 };
161 
162 
163 /* Derived class, for linear separable filtering. */
164 class CV_EXPORTS CvSepFilter : public CvBaseImageFilter
165 {
166 public:
167     CvSepFilter();
168     CvSepFilter( int _max_width, int _src_type, int _dst_type,
169                  const CvMat* _kx, const CvMat* _ky,
170                  CvPoint _anchor=cvPoint(-1,-1),
171                  int _border_mode=IPL_BORDER_REPLICATE,
172                  CvScalar _border_value=cvScalarAll(0) );
173     virtual ~CvSepFilter();
174 
175     virtual void init( int _max_width, int _src_type, int _dst_type,
176                        const CvMat* _kx, const CvMat* _ky,
177                        CvPoint _anchor=cvPoint(-1,-1),
178                        int _border_mode=IPL_BORDER_REPLICATE,
179                        CvScalar _border_value=cvScalarAll(0) );
180     virtual void init_deriv( int _max_width, int _src_type, int _dst_type,
181                              int dx, int dy, int aperture_size, int flags=0 );
182     virtual void init_gaussian( int _max_width, int _src_type, int _dst_type,
183                                 int gaussian_size, double sigma );
184 
185     /* dummy method to avoid compiler warnings */
186     virtual void init( int _max_width, int _src_type, int _dst_type,
187                        bool _is_separable, CvSize _ksize,
188                        CvPoint _anchor=cvPoint(-1,-1),
189                        int _border_mode=IPL_BORDER_REPLICATE,
190                        CvScalar _border_value=cvScalarAll(0) );
191 
192     virtual void clear();
get_x_kernel() const193     const CvMat* get_x_kernel() const { return kx; }
get_y_kernel() const194     const CvMat* get_y_kernel() const { return ky; }
get_x_kernel_flags() const195     int get_x_kernel_flags() const { return kx_flags; }
get_y_kernel_flags() const196     int get_y_kernel_flags() const { return ky_flags; }
197 
198     enum { GENERIC=0, ASYMMETRICAL=1, SYMMETRICAL=2, POSITIVE=4, SUM_TO_1=8, INTEGER=16 };
199     enum { NORMALIZE_KERNEL=1, FLIP_KERNEL=2 };
200 
201     static void init_gaussian_kernel( CvMat* kernel, double sigma=-1 );
202     static void init_sobel_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
203     static void init_scharr_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
204 
205 protected:
206     CvMat *kx, *ky;
207     int kx_flags, ky_flags;
208 };
209 
210 
211 /* Derived class, for linear non-separable filtering. */
212 class CV_EXPORTS CvLinearFilter : public CvBaseImageFilter
213 {
214 public:
215     CvLinearFilter();
216     CvLinearFilter( int _max_width, int _src_type, int _dst_type,
217                     const CvMat* _kernel,
218                     CvPoint _anchor=cvPoint(-1,-1),
219                     int _border_mode=IPL_BORDER_REPLICATE,
220                     CvScalar _border_value=cvScalarAll(0) );
221     virtual ~CvLinearFilter();
222 
223     virtual void init( int _max_width, int _src_type, int _dst_type,
224                        const CvMat* _kernel,
225                        CvPoint _anchor=cvPoint(-1,-1),
226                        int _border_mode=IPL_BORDER_REPLICATE,
227                        CvScalar _border_value=cvScalarAll(0) );
228 
229     /* dummy method to avoid compiler warnings */
230     virtual void init( int _max_width, int _src_type, int _dst_type,
231                        bool _is_separable, CvSize _ksize,
232                        CvPoint _anchor=cvPoint(-1,-1),
233                        int _border_mode=IPL_BORDER_REPLICATE,
234                        CvScalar _border_value=cvScalarAll(0) );
235 
236     virtual void clear();
get_kernel() const237     const CvMat* get_kernel() const { return kernel; }
get_kernel_sparse_buf()238     uchar* get_kernel_sparse_buf() { return k_sparse; }
get_kernel_sparse_count() const239     int get_kernel_sparse_count() const { return k_sparse_count; }
240 
241 protected:
242     CvMat *kernel;
243     uchar* k_sparse;
244     int k_sparse_count;
245 };
246 
247 
248 /* Box filter ("all 1's", optionally normalized) filter. */
249 class CV_EXPORTS CvBoxFilter : public CvBaseImageFilter
250 {
251 public:
252     CvBoxFilter();
253     CvBoxFilter( int _max_width, int _src_type, int _dst_type,
254                  bool _normalized, CvSize _ksize,
255                  CvPoint _anchor=cvPoint(-1,-1),
256                  int _border_mode=IPL_BORDER_REPLICATE,
257                  CvScalar _border_value=cvScalarAll(0) );
258     virtual void init( int _max_width, int _src_type, int _dst_type,
259                        bool _normalized, CvSize _ksize,
260                        CvPoint _anchor=cvPoint(-1,-1),
261                        int _border_mode=IPL_BORDER_REPLICATE,
262                        CvScalar _border_value=cvScalarAll(0) );
263 
264     virtual ~CvBoxFilter();
is_normalized() const265     bool is_normalized() const { return normalized; }
get_scale() const266     double get_scale() const { return scale; }
get_sum_buf()267     uchar* get_sum_buf() { return sum; }
get_sum_count_ptr()268     int* get_sum_count_ptr() { return &sum_count; }
269 
270 protected:
271     virtual void start_process( CvSlice x_range, int width );
272 
273     uchar* sum;
274     int sum_count;
275     bool normalized;
276     double scale;
277 };
278 
279 
280 /* Laplacian operator: (d2/dx + d2/dy)I. */
281 class CV_EXPORTS CvLaplaceFilter : public CvSepFilter
282 {
283 public:
284     CvLaplaceFilter();
285     CvLaplaceFilter( int _max_width, int _src_type, int _dst_type,
286                      bool _normalized, int _ksize,
287                      int _border_mode=IPL_BORDER_REPLICATE,
288                      CvScalar _border_value=cvScalarAll(0) );
289     virtual ~CvLaplaceFilter();
290     virtual void init( int _max_width, int _src_type, int _dst_type,
291                        bool _normalized, int _ksize,
292                        int _border_mode=IPL_BORDER_REPLICATE,
293                        CvScalar _border_value=cvScalarAll(0) );
294 
295     /* dummy methods to avoid compiler warnings */
296     virtual void init( int _max_width, int _src_type, int _dst_type,
297                        bool _is_separable, CvSize _ksize,
298                        CvPoint _anchor=cvPoint(-1,-1),
299                        int _border_mode=IPL_BORDER_REPLICATE,
300                        CvScalar _border_value=cvScalarAll(0) );
301 
302     virtual void init( int _max_width, int _src_type, int _dst_type,
303                        const CvMat* _kx, const CvMat* _ky,
304                        CvPoint _anchor=cvPoint(-1,-1),
305                        int _border_mode=IPL_BORDER_REPLICATE,
306                        CvScalar _border_value=cvScalarAll(0) );
307 
is_normalized() const308     bool is_normalized() const { return normalized; }
is_basic_laplacian() const309     bool is_basic_laplacian() const { return basic_laplacian; }
310 protected:
311     void get_work_params();
312 
313     bool basic_laplacian;
314     bool normalized;
315 };
316 
317 
318 /* basic morphological operations: erosion & dilation */
319 class CV_EXPORTS CvMorphology : public CvBaseImageFilter
320 {
321 public:
322     CvMorphology();
323     CvMorphology( int _operation, int _max_width, int _src_dst_type,
324                   int _element_shape, CvMat* _element,
325                   CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
326                   int _border_mode=IPL_BORDER_REPLICATE,
327                   CvScalar _border_value=cvScalarAll(0) );
328     virtual ~CvMorphology();
329     virtual void init( int _operation, int _max_width, int _src_dst_type,
330                        int _element_shape, CvMat* _element,
331                        CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
332                        int _border_mode=IPL_BORDER_REPLICATE,
333                        CvScalar _border_value=cvScalarAll(0) );
334 
335     /* dummy method to avoid compiler warnings */
336     virtual void init( int _max_width, int _src_type, int _dst_type,
337                        bool _is_separable, CvSize _ksize,
338                        CvPoint _anchor=cvPoint(-1,-1),
339                        int _border_mode=IPL_BORDER_REPLICATE,
340                        CvScalar _border_value=cvScalarAll(0) );
341 
342     virtual void clear();
get_element() const343     const CvMat* get_element() const { return element; }
get_element_shape() const344     int get_element_shape() const { return el_shape; }
get_operation() const345     int get_operation() const { return operation; }
get_element_sparse_buf()346     uchar* get_element_sparse_buf() { return el_sparse; }
get_element_sparse_count() const347     int get_element_sparse_count() const { return el_sparse_count; }
348 
349     enum { RECT=0, CROSS=1, ELLIPSE=2, CUSTOM=100, BINARY = 0, GRAYSCALE=256 };
350     enum { ERODE=0, DILATE=1 };
351 
352     static void init_binary_element( CvMat* _element, int _element_shape,
353                                      CvPoint _anchor=cvPoint(-1,-1) );
354 protected:
355 
356     void start_process( CvSlice x_range, int width );
357     int fill_cyclic_buffer( const uchar* src, int src_step,
358                             int y0, int y1, int y2 );
359     uchar* el_sparse;
360     int el_sparse_count;
361 
362     CvMat *element;
363     int el_shape;
364     int operation;
365 };
366 
367 
368 //////////////////////////////////////////////////////////////////////////////////////////
369 
370 struct CV_EXPORTS CvLevMarq
371 {
372     CvLevMarq();
373     CvLevMarq( int nparams, int nerrs, CvTermCriteria criteria=
374         cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON),
375         bool completeSymmFlag=false );
376     ~CvLevMarq();
377     void init( int nparams, int nerrs, CvTermCriteria criteria=
378         cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,DBL_EPSILON),
379         bool completeSymmFlag=false );
380     bool update( const CvMat*& param, CvMat*& J, CvMat*& err );
381     bool updateAlt( const CvMat*& param, CvMat*& JtJ, CvMat*& JtErr, double*& errNorm );
382 
383     void clear();
384     void step();
385     enum { DONE=0, STARTED=1, CALC_J=2, CHECK_ERR=3 };
386 
387     CvMat* mask;
388     CvMat* prevParam;
389     CvMat* param;
390     CvMat* J;
391     CvMat* err;
392     CvMat* JtJ;
393     CvMat* JtJN;
394     CvMat* JtErr;
395     CvMat* JtJV;
396     CvMat* JtJW;
397     double prevErrNorm, errNorm;
398     int lambdaLg10;
399     CvTermCriteria criteria;
400     int state;
401     int iters;
402     bool completeSymmFlag;
403 };
404 
405 #endif /* __cplusplus */
406 
407 #endif /* _CV_HPP_ */
408 
409 /* End of file. */
410