aihot  2017-05-18 19:50:39  OpenCV |   查看评论   

实现代码dilate.cpp:

  1. // fbc_cv是免费软件,并且使用与OpenCV相同的许可证
  2.   
  3. #ifndef FBC_CV_DILATE_HPP_  
  4. #define FBC_CV_DILATE_HPP_  
  5.   
  6. /* reference: include/opencv2/imgproc.hpp 
  7.               modules/imgproc/src/morph.cpp 
  8. */  
  9.   
  10. #include <typeinfo>  
  11. #include "core/mat.hpp"  
  12. #include "imgproc.hpp"  
  13. #include "filterengine.hpp"  
  14. #include "core/core.hpp"  
  15. #include "morph.hpp"  
  16.   
  17. namespace fbc {  
  18.   
  19. // 通过使用特定的结构元素来扩展图像  
  20. // \f[\texttt{dst} (x,y) =  \max _{(x',y'):  \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f]  
  21. // 在多通道图像的情况下,每个通道被独立处理.  
  22. // 支持类型:uchar / float,多通道  
  23. template<typename _Tp, int chs>  
  24. int dilate(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, Mat_<uchar, 1>& kernel,  
  25.     Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar& borderValue = Scalar::all(DBL_MAX))  
  26. {  
  27.     FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() || typeid(float).name() == typeid(_Tp).name()); // uchar || float  
  28.     if (dst.empty()) {  
  29.         dst = Mat_<_Tp, chs>(src.rows, src.cols);  
  30.     } else {  
  31.         FBC_Assert(src.rows == dst.rows && src.cols == dst.cols);  
  32.     }  
  33.   
  34.     Size ksize = !kernel.empty() ? kernel.size() : Size(3, 3);  
  35.     anchor = normalizeAnchor(anchor, ksize);  
  36.   
  37.     if (iterations == 0 || kernel.rows * kernel.cols == 1) {  
  38.         src.copyTo(dst);  
  39.         return 0;  
  40.     }  
  41.   
  42.     if (kernel.empty()) {  
  43.         kernel = Mat_<uchar, 1>(1 + iterations * 2, 1 + iterations * 2);  
  44.         getStructuringElement(kernel, MORPH_RECT, Size(1 + iterations * 2, 1 + iterations * 2));  
  45.         anchor = Point(iterations, iterations);  
  46.         iterations = 1;  
  47.     } else if (iterations > 1 && countNonZero(kernel) == kernel.rows * kernel.cols) {  
  48.         anchor = Point(anchor.x*iterations, anchor.y*iterations);  
  49.         kernel = Mat_<uchar, 1>(ksize.height + (iterations - 1)*(ksize.height - 1), ksize.width + (iterations - 1)*(ksize.width - 1));  
  50.         getStructuringElement(kernel, MORPH_RECT,  
  51.             Size(ksize.width + (iterations - 1)*(ksize.width - 1), ksize.height + (iterations - 1)*(ksize.height - 1)), anchor);  
  52.         iterations = 1;  
  53.     }  
  54.   
  55.     anchor = normalizeAnchor(anchor, kernel.size());  
  56.   
  57.     Ptr<BaseRowFilter> rowFilter;  
  58.     Ptr<BaseColumnFilter> columnFilter;  
  59.     Ptr<BaseFilter> filter2D;  
  60.   
  61.     if (countNonZero(kernel) == kernel.rows*kernel.cols) {  
  62.         // 矩形结构元素
  63.         rowFilter = getMorphologyRowFilter<_Tp, chs>(1, kernel.cols, anchor.x);  
  64.         columnFilter = getMorphologyColumnFilter<_Tp, chs>(1, kernel.rows, anchor.y);  
  65.     } else {  
  66.         filter2D = getMorphologyFilter<_Tp, chs>(1, kernel, anchor);  
  67.     }  
  68.   
  69.     Scalar borderValue_ = borderValue;  
  70.     if (borderType == BORDER_CONSTANT && borderValue_ == Scalar::all(DBL_MAX)) {  
  71.         if (sizeof(_Tp) == 1) // CV_8U  
  72.             borderValue_ = Scalar::all(0.);  
  73.         else // CV_32F  
  74.             borderValue_ = Scalar::all(-FLT_MAX);  
  75.     }  
  76.   
  77.     Ptr<FilterEngine<_Tp, _Tp, _Tp, chs, chs, chs>> f = makePtr<FilterEngine<_Tp, _Tp, _Tp, chs, chs, chs>>(filter2D, rowFilter, columnFilter, borderType, borderType, borderValue_);  
  78.     f->apply(src, dst);  
  79.     for (int i = 1; i < iterations; i++)  
  80.         f->apply(dst, dst);  
  81.   
  82.     return 0;  
  83. }  
  84.   
  85. // 命名空间fbc 
  86.   
  87. #endif // FBC_CV_DILATE_HPP_  

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV代码提取:扩张(dilate)函数的实现

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]