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