aihot  2017-05-18 19:26:00  OpenCV |   查看评论   

实现代码形态

  1. // fbc_cv是免费软件,并且使用与OpenCV相同的许可证  
  2.   
  3. #ifndef FBC_CV_MORPHOLOGYEX_HPP_  
  4. #define FBC_CV_MORPHOLOGYEX_HPP_  
  5.   
  6. /* reference: include/opencv2/imgproc.hpp 
  7.               modules/imgproc/src/morph.cpp 
  8. */  
  9.   
  10. #include <typeinfo>  
  11. #include "erode.hpp"  
  12. #include "dilate.hpp"  
  13.   
  14. namespace fbc {  
  15.   
  16. // 使用侵蚀和扩张作为基本操作进行先进的形态学变换  
  17. // 在多通道图像的情况下,每个通道被独立处理。 
  18. // morphologyEx可以应用几次(迭代)次。  
  19. // op ==> enum MorphTypes  
  20. // 支持类型:uchar/float,多通道  
  21. template<typename _Tp, int chs>  
  22. int morphologyEx(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, int op, const Mat_<uchar, 1>& kernel,  
  23.     Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar& borderValue = Scalar::all(DBL_MAX))  
  24. {  
  25.     FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() || typeid(float).name() == typeid(_Tp).name()); // uchar || float  
  26.     if (dst.empty()) {  
  27.         dst = Mat_<_Tp, chs>(src.rows, src.cols);  
  28.     } else {  
  29.         FBC_Assert(src.rows == dst.rows && src.cols == dst.cols);  
  30.     }  
  31.   
  32.     Mat_<uchar, 1> kernel_ = kernel;  
  33.     if (kernel_.empty()) {  
  34.         kernel_ = Mat_<uchar, 1>(3, 3);  
  35.         getStructuringElement(kernel_, MORPH_RECT, Size(3, 3), Point(1, 1));  
  36.     }  
  37.   
  38.     switch (op) {  
  39.         case MORPH_ERODE: {  
  40.             erode(src, dst, kernel_, anchor, iterations, borderType, borderValue);  
  41.             break;  
  42.         }  
  43.         case MORPH_DILATE: {  
  44.             dilate(src, dst, kernel_, anchor, iterations, borderType, borderValue);  
  45.             break;  
  46.         }  
  47.         case MORPH_OPEN: {  
  48.             erode(src, dst, kernel_, anchor, iterations, borderType, borderValue);  
  49.             dilate(dst, dst, kernel_, anchor, iterations, borderType, borderValue);  
  50.             break;  
  51.         }  
  52.         case CV_MOP_CLOSE: {  
  53.             dilate(src, dst, kernel_, anchor, iterations, borderType, borderValue);  
  54.             erode(dst, dst, kernel_, anchor, iterations, borderType, borderValue);  
  55.             break;  
  56.         }  
  57.         case CV_MOP_GRADIENT: {  
  58.             Mat_<_Tp, chs> temp(src.rows, src.cols);  
  59.             erode(src, temp, kernel_, anchor, iterations, borderType, borderValue);  
  60.             dilate(src, dst, kernel_, anchor, iterations, borderType, borderValue);  
  61.             dst -= temp;  
  62.             break;  
  63.         }  
  64.         case CV_MOP_TOPHAT: {  
  65.             Mat_<_Tp, chs> temp(src.rows, src.cols);  
  66.             if (src.data != dst.data)  
  67.                 temp = dst;  
  68.             erode(src, temp, kernel_, anchor, iterations, borderType, borderValue);  
  69.             dilate(temp, temp, kernel_, anchor, iterations, borderType, borderValue);  
  70.             dst = src - temp;  
  71.             break;  
  72.         }  
  73.         case CV_MOP_BLACKHAT: {  
  74.             Mat_<_Tp, chs> temp(src.rows, src.cols);  
  75.             if (src.data != dst.data)  
  76.                 temp = dst;  
  77.             dilate(src, temp, kernel_, anchor, iterations, borderType, borderValue);  
  78.             erode(temp, temp, kernel_, anchor, iterations, borderType, borderValue);  
  79.             dst = temp - src;  
  80.             break;  
  81.         }  
  82.         case MORPH_HITMISS: {  
  83.             FBC_Assert(typeid(uchar).name() == typeid(_Tp).name() && chs == 1);  
  84.             Mat_<uchar, 1> k1 = (kernel_ == Mat_<uchar, 1>(kernel_.rows, kernel_.cols, Scalar::all(1)));  
  85.             Mat_<uchar, 1> k2 = (kernel_ == Mat_<int, 1>(kernel_.rows, kernel_.cols, Scalar::all(-1)));  
  86.             Mat_<_Tp, chs> e1, e2;  
  87.   
  88.             if (countNonZero(k1) <= 0)  
  89.                 e1 = src;  
  90.             else  
  91.                 erode(src, e1, k1, anchor, iterations, borderType, borderValue);  
  92.             if (countNonZero(k2) <= 0) {  
  93.                 e2 = src;  
  94.             } else {  
  95.                 Mat_<_Tp, chs> src_complement;  
  96.                 bitwise_not(src, src_complement);  
  97.                 erode(src_complement, e2, k2, anchor, iterations, borderType, borderValue);  
  98.             }  
  99.             bitwise_and(e1, e2, dst);  
  100.             break;  
  101.         }  
  102.         default:  
  103.             FBC_Assert("unknown morphological operation");  
  104.     }  
  105.   
  106.     return 0;  
  107. }  
  108.   
  109. // namespace fbc  
  110.   
  111. #endif // FBC_CV_MORPHOLOGYEX_HPP_  

 

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

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