图像形态学操作 – 基于形状的一系列图像处理操作的合集,主要是基于集合论基础上的形态学数学
形态学有四个基本操作:腐蚀、膨胀、开、闭
膨胀与腐蚀是图像处理中最常用的形态学操作手段
膨胀和腐蚀的主要用途:
消除噪声;
分割出独立的图像元素,在图像中连接相邻的元素;
寻找图像中明显的极大值或极小值区;
求出图像的梯度;
膨胀
将图像或者图像的一部分区域称为A,与核称为B进行卷积,B可以是任何形状和大小,它拥有单独定义出来的
锚点(anchor point),核也可以视为模板或者是掩码,膨胀是求局部的最大值,A与B完成卷积后,计算B区域
的像素最大值,并且幅值给指定的参考点像素,这样使视图中的高亮区域逐渐增长。
A区域被扩大了
腐蚀
腐蚀跟膨胀操作的过程类似,唯一不同的是以最小值替换锚点重叠下图像的像素值
A区域变小了
腐蚀和膨胀是对像素值大的部分而言的,即高亮白部分而不是黑色部分;
膨胀是图像中的高亮部分进行膨胀,领域扩张,效果图拥有比原图更大的高亮区域;
腐蚀是图像中的高亮部分被腐蚀掉,领域缩减,效果图拥有比原图更小的高亮区域;
getStructuringElement(int shape, Size ksize, Point anchor) - 形状 (MORPH_RECT \MORPH_CROSS \MORPH_ELLIPSE) - 大小 - 锚点 默认是Point(-1, -1)意思就是中心像素
@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用getStructuringElement来创建一个kernel
@param anchor position of the anchor within the element; default value (-1, -1) means that the默认自动,-1,-1
anchor is at the element center.
@param iterations number of times dilation is applied.默认,膨胀迭代次数
@param borderType pixel extrapolation method, see cv::BorderTypes默认
@param borderValue border value in case of a constant border默认,在恒定的边界里,好像是这么个意思
膨胀
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() );
腐蚀
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() );
src1 = imread("G:/kobe.jpeg");
namedWindow("0");
imshow("0", src1);
src = Mat::zeros(src1.size(), src1.type());
cvtColor(src1, src, CV_BGR2GRAY);
namedWindow("1");
imshow("1", src);
Mat kernel = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));
Mat src2 = Mat::zeros(src.size(), src.type());
dilate(src1, src2, kernel);
namedWindow("2");
imshow("2", src2);
Mat src3 = Mat::zeros(src.size(), src.type());
erode(src1, src3, kernel);
namedWindow("3");
imshow("3", src3);
开闭运算
- 开运算为: 先腐蚀再膨胀
-
闭运算为: 先膨胀再腐蚀
-
开闭运算具有幂等性。即做n次开运算和做一次开运算的结果相同,闭运算同样如此。
-
这里有一篇博客讲到另外一个点感觉挺有用的:https://blog.csdn.net/qq_25847123/article/details/73744575