理论基础
数学形态学的运算以腐蚀和膨胀这两种基本运算为基础,引出了其他几个常用的数学形态运算,最常见的基本运算有七种,分别是:膨胀,腐蚀,开运算,闭运算,击中,细化和粗化,它们是全部形态学的基础,运用这些运算及其组合可以进行图像形状和结构的分析及处理,常见于图像预处理工作中,为后面图像分割,特征提取,边界检测,图像滤波等打下基础。
腐蚀是数学形态学的两种最为基本的运算之一,腐蚀在数学形态学的作用是消除物体边界点,使边界向内部收缩,可以把小于结构元素的物体去除。这样选取不同大小的结构元素,就可以去除不同大小的物体。如两个区域之间有细小的通道,通过腐蚀可以将两个区域分开。腐蚀的数学表达式是
S
=
X
⨂
B
=
{
x
,
y
∣
B
x
y
⊆
X
}
S=X\bigotimes B=\left \{ x,y\mid B_{xy}\subseteq X\right \}
S=X⨂B={x,y∣Bxy⊆X}
膨胀是数学形态学中除腐蚀之外的另一种基本运算。膨胀在数学形态学中的作用与腐蚀的作用正好相反,它是对二值化物体边界点进行扩充,将与物体接触的所有背景点合并到该物体当中。如果两个物体之间的距离比较近,则膨胀运算可能将两个物体连接在一起,膨胀对于填补空洞狠有用。膨胀的数学表达式为:
S
=
X
⨁
B
=
{
x
,
y
∣
B
x
y
∩
X
≠
φ
}
S=X\bigoplus B=\left \{ x,y\mid B_{xy}\cap X\neq \varphi \right \}
S=X⨁B={x,y∣Bxy∩X=φ}
图像腐蚀操作
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像的腐蚀操作
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
image = imread("F:\\fabrik.png");
image_matrix=image(:,:,1);
image_matrix=double(image_matrix);
[height,width,channels]=size(image);
image_out = zeros(height,width);
%输入的卷积核大小
maskHeight=11;
maskWidth=11;
%当前像素值
currentPixel=0;
for i=(maskHeight+1)/2:height-(maskHeight+1)/2
for j=(maskWidth+1)/2:width-(maskWidth+1)/2
minValue = 255;
%更新最大值
for k=1:maskHeight
for p=1:maskWidth
currentPixel = image_matrix(i-(maskHeight+1)/2+k,j-(maskWidth+1)/2+p);
if(currentPixel<minValue)
minValue = currentPixel;
end
end
end
%更新卷积核中心的数值
image_out(i,j)=minValue;
end
end
%图像边缘像素修改
for i=1:height
for j=1:width
if(i>(maskHeight+1)/2 && i<height-(maskHeight+1)/2 && j>(maskWidth+1)/2 && j<width-(maskWidth+1)/2)
image_out(i,j)= image_out(i,j);
else
image_out(i,j)= image_matrix(i,j);
end
end
end
%显示图像
image_out = uint8(image_out);
subplot(1,2,1);
imshow(image);
subplot(1,2,2);
imshow(image_out);
图像膨胀操作
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像的膨胀操作
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
image = imread("F:\\fabrik.png");
image_matrix=image(:,:,1);
image_matrix=double(image_matrix);
[height,width,channels]=size(image);
image_out = zeros(height,width);
%输入的卷积核大小
maskHeight=11;
maskWidth=11;
%当前像素值
currentPixel=0;
for i=(maskHeight+1)/2:height-(maskHeight+1)/2
for j=(maskWidth+1)/2:width-(maskWidth+1)/2
maxValue = 0;
%更新最大值
for k=1:maskHeight
for p=1:maskWidth
currentPixel = image_matrix(i-(maskHeight+1)/2+k,j-(maskWidth+1)/2+p);
if(currentPixel>maxValue)
maxValue = currentPixel;
end
end
end
%更新卷积核中心的数值
image_out(i,j)=maxValue;
end
end
%图像边缘像素修改
for i=1:height
for j=1:width
if(i>(maskHeight+1)/2 && i<height-(maskHeight+1)/2 && j>(maskWidth+1)/2 && j<width-(maskWidth+1)/2)
image_out(i,j)= image_out(i,j);
else
image_out(i,j)= image_matrix(i,j);
end
end
end
%显示图像
image_out = uint8(image_out);
subplot(1,2,1);
imshow(image);
subplot(1,2,2);
imshow(image_out);
Halcon算子
*腐蚀图像
gray_erosion_rect (Image, ImageMin, 11, 11)
*膨胀图像
gray_dilation_rect (Image, ImageMax, 11, 11)
Halcon与源码图像效果比对
Halcon腐蚀效果
Matlab源码腐蚀效果
Halcon膨胀效果
Matlab源码膨胀效果