在二值图的结果基础上,可针对性处理。
这些处理有些是概念上的,有些是原理上的,也有形态上的,那就看用途与目的了。
本质上还是对二值图的黑白点进行处理,以用于图像增强、边缘检测、图像分割等多个领域。比如膨胀与腐蚀。
膨胀:输入图像与结构元素进行卷积计算,取局部极大值来替代中心值,可以使相互分离的物体连接起来。
腐蚀:取局部极小值替换中心值,可以断开两个连接在一起的物体。
这个概念对于没太多理论支持的程序员来说,术语太多,可以简单理解就是,膨胀与腐蚀是相对于白色区域而言的。如果膨胀,白色区域就象面团发酵一样会胀大,但空间就那么大,可能两个面团就会挨上合并成一个面团了。如果腐蚀,就象拉面,原来一根面条,可能就断成两根,当然断的地方肯定是从细处断。
原理很简单,代码也很简单,膨胀与腐蚀分别对应两个函数:dilate与erode,这两个函数的参数都一样。
/** @brief Erodes an image by using a specific structuring element.
The function erodes the source image using the specified structuring element that determines the
shape of a pixel neighborhood over which the minimum is taken:
\f[\texttt{dst} (x,y) = \min _{(x',y'): \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f]
The function supports the in-place mode. Erosion can be applied several ( iterations ) times. In
case of multi-channel images, each channel is processed independently.
@param src input image; the number of channels can be arbitrary, but the depth should be one of
CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param kernel structuring element used for erosion; if `element=Mat()`, a `3 x 3` rectangular
structuring element is used. Kernel can be created using #getStructuringElement.
@param anchor position of the anchor within the element; default value (-1, -1) means that the
anchor is at the element center.
@param iterations number of times erosion is applied.
@param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not supported.
@param borderValue border value in case of a constant border
@sa dilate, morphologyEx, getStructuringElement
*/
CV_EXPORTS_W void erode( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
/** @brief Dilates an image by using a specific structuring element.
The function dilates the source image using the specified structuring element that determines the
shape of a pixel neighborhood over which the maximum is taken:
\f[\texttt{dst} (x,y) = \max _{(x',y'): \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')\f]
The function supports the in-place mode. Dilation can be applied several ( iterations ) times. In
case of multi-channel images, each channel is processed independently.
@param src input image; the number of channels can be arbitrary, but the depth should be one of
CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param kernel structuring element used for dilation; if elemenat=Mat(), a 3 x 3 rectangular
structuring element is used. Kernel can be created using #getStructuringElement
@param anchor position of the anchor within the element; default value (-1, -1) means that the
anchor is at the element center.
@param iterations number of times dilation is applied.
@param borderType pixel extrapolation method, see #BorderTypes. #BORDER_WRAP is not suported.
@param borderValue border value in case of a constant border
@sa erode, morphologyEx, getStructuringElement
*/
CV_EXPORTS_W void dilate( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
其中,各参数含义如下:
src:输入图像。
kernel:用于腐蚀运算的核结构。
dst:输出图像(返回值)。
anchor:瞄点位置,默认是kernel对应区域的中心位置。
iterations:应用腐蚀操作迭代的次数。
borderType:边界模式,由BorderTypes定义。
borderValue:当边界模式为BORDER_CONSTANT时的边界像素值。
调试过程中,突然发现,膨胀腐蚀的输入也支持多通道图像,即不局限于灰度图
其中好坏利弊,那就看程序员自己体会了。