形态学处理

 本文的主角是OpenCV中的morphologyEx函数,它利用基本的膨胀和腐蚀技术,来执行更加高级的形态学变换,如开闭运算、形态学梯度、“顶帽”、“黑帽”等等

void cv::morphologyEx( InputArray _src,OutputArray _dst, int op,
             InputArray kernel, Pointanchor, int iterations,
             int borderType, constScalar& borderValue )
{
//拷贝Mat数据到临时变量
   Mat src = _src.getMat(), temp;
   _dst.create(src.size(), src.type());
   Mat dst = _dst.getMat();
 
//一个大switch,根据不同的标识符取不同的操作
   switch( op )
  {
   case MORPH_ERODE: //腐蚀
     erode( src, dst, kernel, anchor, iterations, borderType, borderValue );
     break;
   case MORPH_DILATE: //膨胀
     dilate( src, dst, kernel, anchor, iterations, borderType, borderValue );
     break;
   case MORPH_OPEN:  //开环
     erode( src, dst, kernel, anchor, iterations, borderType, borderValue );
     dilate( dst, dst, kernel, anchor, iterations, borderType, borderValue );
     break;
   case CV_MOP_CLOSE: //闭环
     dilate( src, dst, kernel, anchor, iterations, borderType, borderValue );
     erode( dst, dst, kernel, anchor, iterations, borderType, borderValue );
     break;
   case CV_MOP_GRADIENT://梯度
     erode( src, temp, kernel, anchor, iterations, borderType, borderValue );
     dilate( src, dst, kernel, anchor, iterations, borderType, borderValue );
     dst -= temp;
     break;
   case CV_MOP_TOPHAT: //顶帽  即是原图像与开运算后的图像相减
     if( src.data != dst.data )
       temp = dst;
     erode( src, temp, kernel, anchor, iterations, borderType, borderValue );
    dilate( temp, temp, kernel, anchor,iterations, borderType, borderValue );
     dst = src - temp;
     break;
   case CV_MOP_BLACKHAT: //黑帽  即是闭运算后的图像减去原图像
     if( src.data != dst.data )
       temp = dst;
     dilate( src, temp, kernel, anchor, iterations, borderType, borderValue);
     erode( temp, temp, kernel, anchor, iterations, borderType, borderValue);
     dst = temp - src;
     break;
   default:
     CV_Error( CV_StsBadArg, "unknown morphological operation" );
  }
}

  • MORPH_OPEN – 开运算(Opening operation)
  • MORPH_CLOSE – 闭运算(Closing operation)
  • MORPH_GRADIENT -形态学梯度(Morphological gradient)
  • MORPH_TOPHAT - “顶帽”(“Top hat”)
  • MORPH_BLACKHAT - “黑帽”(“Black hat“)
 膨胀与腐蚀的介绍可以参考这篇文章http://www.cnblogs.com/slysky/archive/2011/10/16/2214015.html
原文地址:https://www.cnblogs.com/haoxing990/p/4577225.html