cv.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #ifndef _CV_HPP_
  42. #define _CV_HPP_
  43. #ifdef __cplusplus
  44. /****************************************************************************************\
  45. * CvBaseImageFilter: Base class for filtering operations *
  46. \****************************************************************************************/
  47. #define CV_WHOLE 0
  48. #define CV_START 1
  49. #define CV_END 2
  50. #define CV_MIDDLE 4
  51. #define CV_ISOLATED_ROI 8
  52. typedef void (*CvRowFilterFunc)( const uchar* src, uchar* dst, void* params );
  53. typedef void (*CvColumnFilterFunc)( uchar** src, uchar* dst, int dst_step, int count, void* params );
  54. class CV_EXPORTS CvBaseImageFilter
  55. {
  56. public:
  57. CvBaseImageFilter();
  58. /* calls init() */
  59. CvBaseImageFilter( int _max_width, int _src_type, int _dst_type,
  60. bool _is_separable, CvSize _ksize,
  61. CvPoint _anchor=cvPoint(-1,-1),
  62. int _border_mode=IPL_BORDER_REPLICATE,
  63. CvScalar _border_value=cvScalarAll(0) );
  64. virtual ~CvBaseImageFilter();
  65. /* initializes the class for processing an image of maximal width _max_width,
  66. input image has data type _src_type, the output will have _dst_type.
  67. _is_separable != 0 if the filter is separable
  68. (specific behaviour is defined in a derived class), 0 otherwise.
  69. _ksize and _anchor specify the kernel size and the anchor point. _anchor=(-1,-1) means
  70. that the anchor is at the center.
  71. to get interpolate pixel values outside the image _border_mode=IPL_BORDER_*** is used,
  72. _border_value specify the pixel value in case of IPL_BORDER_CONSTANT border mode.
  73. before initialization clear() is called if necessary.
  74. */
  75. virtual void init( int _max_width, int _src_type, int _dst_type,
  76. bool _is_separable, CvSize _ksize,
  77. CvPoint _anchor=cvPoint(-1,-1),
  78. int _border_mode=IPL_BORDER_REPLICATE,
  79. CvScalar _border_value=cvScalarAll(0) );
  80. /* releases all the internal buffers.
  81. for the further use of the object, init() needs to be called. */
  82. virtual void clear();
  83. /* processes input image or a part of it.
  84. input is represented either as matrix (CvMat* src)
  85. or a list of row pointers (uchar** src2).
  86. in the later case width, _src_y1 and _src_y2 are used to specify the size.
  87. _dst is the output image/matrix.
  88. _src_roi specifies the roi inside the input image to process,
  89. (0,0,-1,-1) denotes the whole image.
  90. _dst_origin is the upper-left corner of the filtered roi within the output image.
  91. _phase is either CV_START, or CV_END, or CV_MIDDLE, or CV_START|CV_END, or CV_WHOLE,
  92. which is the same as CV_START|CV_END.
  93. CV_START means that the input is the first (top) stripe of the processed image [roi],
  94. CV_END - the input is the last (bottom) stripe of the processed image [roi],
  95. CV_MIDDLE - the input is neither first nor last stripe.
  96. CV_WHOLE - the input is the whole processed image [roi].
  97. */
  98. virtual int process( const CvMat* _src, CvMat* _dst,
  99. CvRect _src_roi=cvRect(0,0,-1,-1),
  100. CvPoint _dst_origin=cvPoint(0,0), int _flags=0 );
  101. /* retrieve various parameters of the filtering object */
  102. int get_src_type() const { return src_type; }
  103. int get_dst_type() const { return dst_type; }
  104. int get_work_type() const { return work_type; }
  105. CvSize get_kernel_size() const { return ksize; }
  106. CvPoint get_anchor() const { return anchor; }
  107. int get_width() const { return prev_x_range.end_index - prev_x_range.start_index; }
  108. CvRowFilterFunc get_x_filter_func() const { return x_func; }
  109. CvColumnFilterFunc get_y_filter_func() const { return y_func; }
  110. protected:
  111. /* initializes work_type, buf_size and max_rows */
  112. virtual void get_work_params();
  113. /* it is called (not always) from process when _phase=CV_START or CV_WHOLE.
  114. the method initializes ring buffer (buf_end, buf_head, buf_tail, buf_count, rows),
  115. prev_width, prev_x_range, const_row, border_tab, border_tab_sz* */
  116. virtual void start_process( CvSlice x_range, int width );
  117. /* forms pointers to "virtual rows" above or below the processed roi using the specified
  118. border mode */
  119. virtual void make_y_border( int row_count, int top_rows, int bottom_rows );
  120. virtual int fill_cyclic_buffer( const uchar* src, int src_step,
  121. int y, int y1, int y2 );
  122. enum { ALIGN=32 };
  123. int max_width;
  124. /* currently, work_type must be the same as src_type in case of non-separable filters */
  125. int min_depth, src_type, dst_type, work_type;
  126. /* pointers to convolution functions, initialized by init method.
  127. for non-separable filters only y_conv should be set */
  128. CvRowFilterFunc x_func;
  129. CvColumnFilterFunc y_func;
  130. uchar* buffer;
  131. uchar** rows;
  132. int top_rows, bottom_rows, max_rows;
  133. uchar *buf_start, *buf_end, *buf_head, *buf_tail;
  134. int buf_size, buf_step, buf_count, buf_max_count;
  135. bool is_separable;
  136. CvSize ksize;
  137. CvPoint anchor;
  138. int max_ky, border_mode;
  139. CvScalar border_value;
  140. uchar* const_row;
  141. int* border_tab;
  142. int border_tab_sz1, border_tab_sz;
  143. CvSlice prev_x_range;
  144. int prev_width;
  145. };
  146. /* Derived class, for linear separable filtering. */
  147. class CV_EXPORTS CvSepFilter : public CvBaseImageFilter
  148. {
  149. public:
  150. CvSepFilter();
  151. CvSepFilter( int _max_width, int _src_type, int _dst_type,
  152. const CvMat* _kx, const CvMat* _ky,
  153. CvPoint _anchor=cvPoint(-1,-1),
  154. int _border_mode=IPL_BORDER_REPLICATE,
  155. CvScalar _border_value=cvScalarAll(0) );
  156. virtual ~CvSepFilter();
  157. virtual void init( int _max_width, int _src_type, int _dst_type,
  158. const CvMat* _kx, const CvMat* _ky,
  159. CvPoint _anchor=cvPoint(-1,-1),
  160. int _border_mode=IPL_BORDER_REPLICATE,
  161. CvScalar _border_value=cvScalarAll(0) );
  162. virtual void init_deriv( int _max_width, int _src_type, int _dst_type,
  163. int dx, int dy, int aperture_size, int flags=0 );
  164. virtual void init_gaussian( int _max_width, int _src_type, int _dst_type,
  165. int gaussian_size, double sigma );
  166. /* dummy method to avoid compiler warnings */
  167. virtual void init( int _max_width, int _src_type, int _dst_type,
  168. bool _is_separable, CvSize _ksize,
  169. CvPoint _anchor=cvPoint(-1,-1),
  170. int _border_mode=IPL_BORDER_REPLICATE,
  171. CvScalar _border_value=cvScalarAll(0) );
  172. virtual void clear();
  173. const CvMat* get_x_kernel() const { return kx; }
  174. const CvMat* get_y_kernel() const { return ky; }
  175. int get_x_kernel_flags() const { return kx_flags; }
  176. int get_y_kernel_flags() const { return ky_flags; }
  177. enum { GENERIC=0, ASYMMETRICAL=1, SYMMETRICAL=2, POSITIVE=4, SUM_TO_1=8, INTEGER=16 };
  178. enum { NORMALIZE_KERNEL=1, FLIP_KERNEL=2 };
  179. static void init_gaussian_kernel( CvMat* kernel, double sigma=-1 );
  180. static void init_sobel_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
  181. static void init_scharr_kernel( CvMat* _kx, CvMat* _ky, int dx, int dy, int flags=0 );
  182. protected:
  183. CvMat *kx, *ky;
  184. int kx_flags, ky_flags;
  185. };
  186. /* Derived class, for linear non-separable filtering. */
  187. class CV_EXPORTS CvLinearFilter : public CvBaseImageFilter
  188. {
  189. public:
  190. CvLinearFilter();
  191. CvLinearFilter( int _max_width, int _src_type, int _dst_type,
  192. const CvMat* _kernel,
  193. CvPoint _anchor=cvPoint(-1,-1),
  194. int _border_mode=IPL_BORDER_REPLICATE,
  195. CvScalar _border_value=cvScalarAll(0) );
  196. virtual ~CvLinearFilter();
  197. virtual void init( int _max_width, int _src_type, int _dst_type,
  198. const CvMat* _kernel,
  199. CvPoint _anchor=cvPoint(-1,-1),
  200. int _border_mode=IPL_BORDER_REPLICATE,
  201. CvScalar _border_value=cvScalarAll(0) );
  202. /* dummy method to avoid compiler warnings */
  203. virtual void init( int _max_width, int _src_type, int _dst_type,
  204. bool _is_separable, CvSize _ksize,
  205. CvPoint _anchor=cvPoint(-1,-1),
  206. int _border_mode=IPL_BORDER_REPLICATE,
  207. CvScalar _border_value=cvScalarAll(0) );
  208. virtual void clear();
  209. const CvMat* get_kernel() const { return kernel; }
  210. uchar* get_kernel_sparse_buf() { return k_sparse; }
  211. int get_kernel_sparse_count() const { return k_sparse_count; }
  212. protected:
  213. CvMat *kernel;
  214. uchar* k_sparse;
  215. int k_sparse_count;
  216. };
  217. /* Box filter ("all 1's", optionally normalized) filter. */
  218. class CV_EXPORTS CvBoxFilter : public CvBaseImageFilter
  219. {
  220. public:
  221. CvBoxFilter();
  222. CvBoxFilter( int _max_width, int _src_type, int _dst_type,
  223. bool _normalized, CvSize _ksize,
  224. CvPoint _anchor=cvPoint(-1,-1),
  225. int _border_mode=IPL_BORDER_REPLICATE,
  226. CvScalar _border_value=cvScalarAll(0) );
  227. virtual void init( int _max_width, int _src_type, int _dst_type,
  228. bool _normalized, CvSize _ksize,
  229. CvPoint _anchor=cvPoint(-1,-1),
  230. int _border_mode=IPL_BORDER_REPLICATE,
  231. CvScalar _border_value=cvScalarAll(0) );
  232. virtual ~CvBoxFilter();
  233. bool is_normalized() const { return normalized; }
  234. double get_scale() const { return scale; }
  235. uchar* get_sum_buf() { return sum; }
  236. int* get_sum_count_ptr() { return &sum_count; }
  237. protected:
  238. virtual void start_process( CvSlice x_range, int width );
  239. uchar* sum;
  240. int sum_count;
  241. bool normalized;
  242. double scale;
  243. };
  244. /* Laplacian operator: (d2/dx + d2/dy)I. */
  245. class CV_EXPORTS CvLaplaceFilter : public CvSepFilter
  246. {
  247. public:
  248. CvLaplaceFilter();
  249. CvLaplaceFilter( int _max_width, int _src_type, int _dst_type,
  250. bool _normalized, int _ksize,
  251. int _border_mode=IPL_BORDER_REPLICATE,
  252. CvScalar _border_value=cvScalarAll(0) );
  253. virtual ~CvLaplaceFilter();
  254. virtual void init( int _max_width, int _src_type, int _dst_type,
  255. bool _normalized, int _ksize,
  256. int _border_mode=IPL_BORDER_REPLICATE,
  257. CvScalar _border_value=cvScalarAll(0) );
  258. /* dummy methods to avoid compiler warnings */
  259. virtual void init( int _max_width, int _src_type, int _dst_type,
  260. bool _is_separable, CvSize _ksize,
  261. CvPoint _anchor=cvPoint(-1,-1),
  262. int _border_mode=IPL_BORDER_REPLICATE,
  263. CvScalar _border_value=cvScalarAll(0) );
  264. virtual void init( int _max_width, int _src_type, int _dst_type,
  265. const CvMat* _kx, const CvMat* _ky,
  266. CvPoint _anchor=cvPoint(-1,-1),
  267. int _border_mode=IPL_BORDER_REPLICATE,
  268. CvScalar _border_value=cvScalarAll(0) );
  269. bool is_normalized() const { return normalized; }
  270. bool is_basic_laplacian() const { return basic_laplacian; }
  271. protected:
  272. void get_work_params();
  273. bool basic_laplacian;
  274. bool normalized;
  275. };
  276. /* basic morphological operations: erosion & dilation */
  277. class CV_EXPORTS CvMorphology : public CvBaseImageFilter
  278. {
  279. public:
  280. CvMorphology();
  281. CvMorphology( int _operation, int _max_width, int _src_dst_type,
  282. int _element_shape, CvMat* _element,
  283. CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
  284. int _border_mode=IPL_BORDER_REPLICATE,
  285. CvScalar _border_value=cvScalarAll(0) );
  286. virtual ~CvMorphology();
  287. virtual void init( int _operation, int _max_width, int _src_dst_type,
  288. int _element_shape, CvMat* _element,
  289. CvSize _ksize=cvSize(0,0), CvPoint _anchor=cvPoint(-1,-1),
  290. int _border_mode=IPL_BORDER_REPLICATE,
  291. CvScalar _border_value=cvScalarAll(0) );
  292. /* dummy method to avoid compiler warnings */
  293. virtual void init( int _max_width, int _src_type, int _dst_type,
  294. bool _is_separable, CvSize _ksize,
  295. CvPoint _anchor=cvPoint(-1,-1),
  296. int _border_mode=IPL_BORDER_REPLICATE,
  297. CvScalar _border_value=cvScalarAll(0) );
  298. virtual void clear();
  299. const CvMat* get_element() const { return element; }
  300. int get_element_shape() const { return el_shape; }
  301. int get_operation() const { return operation; }
  302. uchar* get_element_sparse_buf() { return el_sparse; }
  303. int get_element_sparse_count() const { return el_sparse_count; }
  304. enum { RECT=0, CROSS=1, ELLIPSE=2, CUSTOM=100, BINARY = 0, GRAYSCALE=256 };
  305. enum { ERODE=0, DILATE=1 };
  306. static void init_binary_element( CvMat* _element, int _element_shape,
  307. CvPoint _anchor=cvPoint(-1,-1) );
  308. protected:
  309. void start_process( CvSlice x_range, int width );
  310. int fill_cyclic_buffer( const uchar* src, int src_step,
  311. int y0, int y1, int y2 );
  312. uchar* el_sparse;
  313. int el_sparse_count;
  314. CvMat *element;
  315. int el_shape;
  316. int operation;
  317. };
  318. #endif /* __cplusplus */
  319. #endif /* _CV_HPP_ */
  320. /* End of file. */