(未实用 需多个函数配套使用)opencv3.0 函数学习 10——morphologyEx 形态学图像处理:开运算、闭运算、形态学梯度、顶帽、黑帽合辑

来源:互联网 发布:机械模块化设计软件 编辑:程序博客网 时间:2024/05/26 19:17

morphologyEx 形态学图像处理  (多个函数配套使用)

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


原理学习网址  http://www.tuicool.com/articles/j6namy   本文中大量资料都转载于此文章


函数参数

void cv::morphologyEx(InputArray src,  OutputArray dst,  int op,  InputArray kernel,  Point anchor = Point(-1,-1),  int iterations = 1,  int borderType = BORDER_CONSTANT,  const Scalar & borderValue = morphologyDefaultBorderValue()  )  

Performs advanced morphological transformations.

The function morphologyEx can perform advanced morphological transformations using an erosion and dilation as basic operations.

Any of the operations can be done in-place. In case of multi-channel images, each channel is processed independently.

Parameters
srcSource image. The number of channels can be arbitrary. The depth should be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.dstDestination image of the same size and type as source image.opType of a morphological operation, see cv::MorphTypeskernelStructuring element. It can be created using cv::getStructuringElement.anchorAnchor position with the kernel. Negative values mean that the anchor is at the kernel center.iterationsNumber of times erosion and dilation are applied.borderTypePixel extrapolation method, see cv::BorderTypesborderValueBorder value in case of a constant border. The default value has a special meaning.
See also
dilate,erode,getStructuringElement
Examples:
morphology2.cpp.


第一个参数,InputArray类型的src,输入图像,即源图像,填Mat类的对象即可。图像位深应该为以下五种之一:CV_8U, CV_16U,CV_16S, CV_32F 或CV_64F。

第二个参数,OutputArray类型的dst,即目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型。

第三个参数,int类型的op,表示形态学运算的类型,可以是如下之一的标识符:

    • MORPH_OPEN – 开运算(Opening operation)
    • MORPH_CLOSE – 闭运算(Closing operation)
    • MORPH_GRADIENT -形态学梯度(Morphological gradient)
    • MORPH_TOPHAT - “顶帽”(“Top hat”)
    • MORPH_BLACKHAT - “黑帽”(“Black hat“)

另有CV版本的标识符也可选择,如CV_MOP_CLOSE,CV_MOP_GRADIENT,CV_MOP_TOPHAT,CV_MOP_BLACKHAT,这应该是OpenCV1.0系列版本遗留下来的标识符,和上面的“MORPH_OPEN”一样的效果。

第四个参数,InputArray类型的kernel,形态学运算的内核。若为NULL时,表示的是使用参考点位于中心3x3的核。我们一般使用函数 getStructuringElement配合这个参数的使用。getStructuringElement函数会返回指定形状和尺寸的结构元素(内核矩阵)。

其中,getStructuringElement函数的第一个参数表示内核的形状,我们可以选择如下三种形状之一:

    • 矩形: MORPH_RECT
    • 交叉形: MORPH_CROSS
    • 椭圆形: MORPH_ELLIPSE

而getStructuringElement函数的第二和第三个参数分别是内核的尺寸以及锚点的位置。

我们一般在调用erode以及dilate函数之前,先定义一个Mat类型的变量来获得getStructuringElement函数的返回值。对于锚点的位置,有默认值Point(-1,-1),表示锚点位于中心。且需要注意,十字形的element形状唯一依赖于锚点的位置。而在其他情况下,锚点只是影响了形态学运算结果的偏移。


getStructuringElement函数相关的调用示例代码如下:

int g_nStructElementSize = 3; //结构元素(内核矩阵)的尺寸 //获取自定义核Mat element =getStructuringElement(MORPH_RECT,       Size(2*g_nStructElementSize+1,2*g_nStructElementSize+1),       Point(g_nStructElementSize, g_nStructElementSize ));

调用这样之后,我们便可以在接下来调用erode、dilate或morphologyEx 函数时,kernel参数填保存getStructuringElement返回值的Mat类型变量。对应于我们上面的示例,就是填element变量。

  • 第五个参数,Point类型的anchor,锚的位置,其有默认值(-1,-1),表示锚位于中心。
  • 第六个参数,int类型的iterations,迭代使用函数的次数,默认值为1。
  • 第七个参数,int类型的borderType,用于推断图像外部像素的某种边界模式。注意它有默认值BORDER_ CONSTANT。
  • 第八个参数,const Scalar&类型的borderValue,当边界为常数时的边界值,有默认值morphologyDefaultBorderValue(),一般我们不用去管他。需要用到它时,可以看官方文档中的createMorphologyFilter()函数得到更详细的解释。


其中的这些操作都可以进行就地(in-place)操作。且对于多通道图像,每一个通道都是单独进行操作。



0 0
原创粉丝点击