dilate 函数的实现(源码)

          文章从自己的QQ(632846506)7年前的日志中移过来的。

       数学形态学可以作为图像的一种滤波算法--形态学滤波。滤波中用到的滤波器(kernal)即为结构元素。结构元素往往是由一个特殊的形状构成,如线条、矩形、圆等。

OpenCV中的dilate函数支持多通道,各个通道膨胀处理过程独立。膨胀针对白色部分(高亮部分)。膨胀即是求局部最大值的操作,图像A与核B作卷积运算,计算核B覆盖区域的像素点的最大值,并把这个值赋值给锚点(anchor point)指定的像素。

Dilation:

(1)、This operations consists of convoluting an image A with some kernel(B), which can have any shape or size, usually a square or circle.

(2)、The kernel B has a defined anchor point, usually being the center of the kernel.

(3)、As the kernel B is scanned over the image, we compute the maximal pixel value overlapped by B and replace the image pixel in the anchor point position with that maximal value. As you can deduce, this maximizing operation causes bright regions within an image to “grow” (therefore the name dilation).


目前fbc_cv库中支持uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一致。

实现代码dilate.cpp:

[cpp]  view plain   copy
  1. // fbc_cv is free software and uses the same licence as OpenCV  
  2. // Email: fengbingchun@163.com  
  3.   
  4. #ifndef FBC_CV_DILATE_HPP_  
  5. #define FBC_CV_DILATE_HPP_  
  6.   
  7. /* reference: include/opencv2/imgproc.hpp 
  8.               modules/imgproc/src/morph.cpp 
  9. */  
  10.   
  11. #include <typeinfo>  
  12. #include "core/mat.hpp"  
  13. #include "imgproc.hpp"  
  14. #include "filterengine.hpp"  
  15. #include "core/core.hpp"  
  16. #include "morph.hpp"  
  17.   
  18. namespace fbc {  
  19.   
  20. // Dilates an image by using a specific structuring element  
  21. // \f[\texttt{dst} (x,y) =  \max _{(x',y'):  \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f]  
  22. // In case of multi - channel images, each channel is processed independently.  
  23. // support type: uchar/float, multi-channels  
  24. template<typename _Tp, int chs>  
  25. int dilate(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, Mat_<uchar, 1>& kernel,  
  26.     Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar& borderValue = Scalar::all(DBL_MAX))  
  27. {  
  28.     FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() || typeid(float).name() == typeid(_Tp).name()); // uchar || float  
  29.     if (dst.empty()) {  
  30.         dst = Mat_<_Tp, chs>(src.rows, src.cols);  
  31.     } else {  
  32.         FBC_Assert(src.rows == dst.rows && src.cols == dst.cols);  
  33.     }  
  34.   
  35.     Size ksize = !kernel.empty() ? kernel.size() : Size(3, 3);  
  36.     anchor = normalizeAnchor(anchor, ksize);  
  37.   
  38.     if (iterations == 0 || kernel.rows * kernel.cols == 1) {  
  39.         src.copyTo(dst);  
  40.         return 0;  
  41.     }  
  42.   
  43.     if (kernel.empty()) {  
  44.         kernel = Mat_<uchar, 1>(1 + iterations * 2, 1 + iterations * 2);  
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值