]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/include/opencv/cv.hpp
add virtual destructors to every C++ filter class for proper memory deallocation...
[opencv.git] / opencv / include / opencv / cv.hpp
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 */
109     int get_src_type() const { return src_type; }
110     int get_dst_type() const { return dst_type; }
111     int get_work_type() const { return work_type; }
112     CvSize get_kernel_size() const { return ksize; }
113     CvPoint get_anchor() const { return anchor; }
114     int get_width() const { return prev_x_range.end_index - prev_x_range.start_index; }
115     CvRowFilterFunc get_x_filter_func() const { return x_func; }
116     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     virtual void clear();
185     const CvMat* get_x_kernel() const { return kx; }
186     const CvMat* get_y_kernel() const { return ky; }
187     int get_x_kernel_flags() const { return kx_flags; }
188     int get_y_kernel_flags() const { return ky_flags; }
189
190     enum { GENERIC=0, ASYMMETRICAL=1, SYMMETRICAL=2, POSITIVE=4, SUM_TO_1=8, INTEGER=16 };
191     enum { NORMALIZE_KERNEL=1, FLIP_KERNEL=2 };
192
193     static void init_gaussian_kernel( CvMat* kernel, double sigma=-1 );
194     static void init_sobel_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
195     static void init_scharr_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
196
197 protected:
198     CvMat *kx, *ky;
199     int kx_flags, ky_flags;
200 };
201
202
203 /* Derived class, for linear non-separable filtering. */
204 class CV_EXPORTS CvLinearFilter : public CvBaseImageFilter
205 {
206 public:
207     CvLinearFilter();
208     CvLinearFilter( int _max_width, int _src_type, int _dst_type,
209                     const CvMat* _kernel,
210                     CvPoint _anchor=cvPoint(-1,-1),
211                     int _border_mode=IPL_BORDER_REPLICATE,
212                     CvScalar _border_value=cvScalarAll(0) );
213     virtual ~CvLinearFilter();
214
215     virtual void init( int _max_width, int _src_type, int _dst_type,
216                        const CvMat* _kernel,
217                        CvPoint _anchor=cvPoint(-1,-1),
218                        int _border_mode=IPL_BORDER_REPLICATE,
219                        CvScalar _border_value=cvScalarAll(0) );
220     virtual void clear();
221     const CvMat* get_kernel() const { return kernel; }
222     uchar* get_kernel_sparse_buf() { return k_sparse; }
223     int get_kernel_sparse_count() const { return k_sparse_count; }
224
225 protected:
226     CvMat *kernel;
227     uchar* k_sparse;
228     int k_sparse_count;
229 };
230
231
232 /* Box filter ("all 1's", optionally normalized) filter. */
233 class CV_EXPORTS CvBoxFilter : public CvBaseImageFilter
234 {
235 public:
236     CvBoxFilter();
237     CvBoxFilter( int _max_width, int _src_type, int _dst_type,
238                  bool _normalized, CvSize _ksize,
239                  CvPoint _anchor=cvPoint(-1,-1),
240                  int _border_mode=IPL_BORDER_REPLICATE,
241                  CvScalar _border_value=cvScalarAll(0) );
242     virtual void init( int _max_width, int _src_type, int _dst_type,
243                        bool _normalized, CvSize _ksize,
244                        CvPoint _anchor=cvPoint(-1,-1),
245                        int _border_mode=IPL_BORDER_REPLICATE,
246                        CvScalar _border_value=cvScalarAll(0) );
247     virtual ~CvBoxFilter();
248     bool is_normalized() const { return normalized; }
249     double get_scale() const { return scale; }
250     uchar* get_sum_buf() { return sum; }
251     int* get_sum_count_ptr() { return &sum_count; }
252
253 protected:
254     virtual void start_process( CvSlice x_range, int width );
255
256     uchar* sum;
257     int sum_count;
258     bool normalized;
259     double scale;
260 };
261
262
263 /* Laplacian operator: (d2/dx + d2/dy)I. */
264 class CV_EXPORTS CvLaplaceFilter : public CvSepFilter
265 {
266 public:
267     CvLaplaceFilter();
268     CvLaplaceFilter( int _max_width, int _src_type, int _dst_type,
269                      bool _normalized, int _ksize,
270                      int _border_mode=IPL_BORDER_REPLICATE,
271                      CvScalar _border_value=cvScalarAll(0) );
272     virtual ~CvLaplaceFilter();
273     virtual void init( int _max_width, int _src_type, int _dst_type,
274                        bool _normalized, int _ksize,
275                        int _border_mode=IPL_BORDER_REPLICATE,
276                        CvScalar _border_value=cvScalarAll(0) );
277     bool is_normalized() const { return normalized; }
278     bool is_basic_laplacian() const { return basic_laplacian; }
279 protected:
280     void get_work_params();
281
282     bool basic_laplacian;
283     bool normalized;
284 };
285
286
287 /* basic morphological operations: erosion & dilation */
288 class CV_EXPORTS CvMorphology : public CvBaseImageFilter
289 {
290 public:
291     CvMorphology();
292     CvMorphology( int _operation, int _max_width, int _src_dst_type,
293                   int _element_shape, CvMat* _element,
294                   CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
295                   int _border_mode=IPL_BORDER_REPLICATE,
296                   CvScalar _border_value=cvScalarAll(0) );
297     virtual ~CvMorphology();
298     virtual void init( int _operation, int _max_width, int _src_dst_type,
299                        int _element_shape, CvMat* _element,
300                        CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
301                        int _border_mode=IPL_BORDER_REPLICATE,
302                        CvScalar _border_value=cvScalarAll(0) );
303     virtual void clear();
304     const CvMat* get_element() const { return element; }
305     int get_element_shape() const { return el_shape; }
306     int get_operation() const { return operation; }
307     uchar* get_element_sparse_buf() { return el_sparse; }
308     int get_element_sparse_count() const { return el_sparse_count; }
309
310     enum { RECT=0, CROSS=1, ELLIPSE=2, CUSTOM=100, BINARY = 0, GRAYSCALE=256 };
311     enum { ERODE=0, DILATE=1 };
312
313     static void init_binary_element( CvMat* _element, int _element_shape,
314                                      CvPoint _anchor=cvPoint(-1,-1) );
315 protected:
316
317     void start_process( CvSlice x_range, int width );
318     int fill_cyclic_buffer( const uchar* src, int src_step,
319                             int y0, int y1, int y2 );
320     uchar* el_sparse;
321     int el_sparse_count;
322
323     CvMat *element;
324     int el_shape;
325     int operation;
326 };
327
328
329 #endif /* __cplusplus */
330
331 #endif /* _CV_HPP_ */
332
333 /* End of file. */