1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
47 /****************************************************************************************\
48 * CvBaseImageFilter: Base class for filtering operations *
49 \****************************************************************************************/
55 #define CV_ISOLATED_ROI 8
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 );
60 class CV_EXPORTS CvBaseImageFilter
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();
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.
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. */
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].
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; }
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
127 virtual void make_y_border( int row_count, int top_rows, int bottom_rows );
129 virtual int fill_cyclic_buffer( const uchar* src, int src_step,
130 int y, int y1, int y2 );
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;
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;
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;
152 int max_ky, border_mode;
153 CvScalar border_value;
156 int border_tab_sz1, border_tab_sz;
158 CvSlice prev_x_range;
163 /* Derived class, for linear separable filtering. */
164 class CV_EXPORTS CvSepFilter : public CvBaseImageFilter
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();
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 );
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) );
192 virtual void clear();
193 const CvMat* get_x_kernel() const { return kx; }
194 const CvMat* get_y_kernel() const { return ky; }
195 int get_x_kernel_flags() const { return kx_flags; }
196 int get_y_kernel_flags() const { return ky_flags; }
198 enum { GENERIC=0, ASYMMETRICAL=1, SYMMETRICAL=2, POSITIVE=4, SUM_TO_1=8, INTEGER=16 };
199 enum { NORMALIZE_KERNEL=1, FLIP_KERNEL=2 };
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 );
207 int kx_flags, ky_flags;
211 /* Derived class, for linear non-separable filtering. */
212 class CV_EXPORTS CvLinearFilter : public CvBaseImageFilter
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();
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) );
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) );
236 virtual void clear();
237 const CvMat* get_kernel() const { return kernel; }
238 uchar* get_kernel_sparse_buf() { return k_sparse; }
239 int get_kernel_sparse_count() const { return k_sparse_count; }
248 /* Box filter ("all 1's", optionally normalized) filter. */
249 class CV_EXPORTS CvBoxFilter : public CvBaseImageFilter
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) );
264 virtual ~CvBoxFilter();
265 bool is_normalized() const { return normalized; }
266 double get_scale() const { return scale; }
267 uchar* get_sum_buf() { return sum; }
268 int* get_sum_count_ptr() { return &sum_count; }
271 virtual void start_process( CvSlice x_range, int width );
280 /* Laplacian operator: (d2/dx + d2/dy)I. */
281 class CV_EXPORTS CvLaplaceFilter : public CvSepFilter
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) );
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) );
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) );
308 bool is_normalized() const { return normalized; }
309 bool is_basic_laplacian() const { return basic_laplacian; }
311 void get_work_params();
313 bool basic_laplacian;
318 /* basic morphological operations: erosion & dilation */
319 class CV_EXPORTS CvMorphology : public CvBaseImageFilter
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) );
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) );
342 virtual void clear();
343 const CvMat* get_element() const { return element; }
344 int get_element_shape() const { return el_shape; }
345 int get_operation() const { return operation; }
346 uchar* get_element_sparse_buf() { return el_sparse; }
347 int get_element_sparse_count() const { return el_sparse_count; }
349 enum { RECT=0, CROSS=1, ELLIPSE=2, CUSTOM=100, BINARY = 0, GRAYSCALE=256 };
350 enum { ERODE=0, DILATE=1 };
352 static void init_binary_element( CvMat* _element, int _element_shape,
353 CvPoint _anchor=cvPoint(-1,-1) );
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 );
368 #endif /* __cplusplus */
370 #endif /* _CV_HPP_ */