c#数字图像处理(十二)图像的腐蚀与膨胀
背景知识
腐蚀与膨胀基本原理:就是用一个特定的结构元素来与待处理图像按像素做逻辑操作;可以理解成拿一个带孔的网格板(结构元素矩阵中元素为1的为孔)盖住图像的某一部分,然后按照各种不同的观察方式来确定操作类型。
比如:腐蚀操作就是拿这个结构元素的中心位置(假设参与逻辑计算的元素对应与二维矩阵中元素为1的点,即网格板上的孔),在图像上移动时,如果透过所有的孔都能看到底下的图像,那么这个中心点处的图像就保留,否则去除。
腐蚀
图像腐蚀运算定义
二值图像腐蚀运算的数学表达式为
g(x,y)=erode[f(x, y ), B]=AND[Bf(x,y)]
其中,g(x,y)为腐蚀后的二值图像,f(x,y)为原二值图像,B为结构元素。B(x,y)定义为:
Bf(x,y)={f(x-bx, y-by) ,(bx, by)∈B}
算子AND(x1,…,xn)定义为:当且仅当x1=··=xn=1时,AND(x1,…,xn)等于1;否则为0。
把结构元素B平移a后得到Ba,若Ba包含于X,我们记下这个a点,所有满足上述条件的a点组成的集合称做X被B腐蚀(Erosion)的结果。用公式表示为:E(X)={a| Ba∈X}=XB。原理图如下:
实际使用时示意图:

说明:左边是被处理的图象X(二值图象,我们针对的是黑点),中间是结构元素B,那个标有origin的点是中心点,即当前处理元素的位置,我们在介绍模板操作时也有过类似的概念。腐蚀的方法是,拿B的中心点和X上的点一个一个地对比,如果B上的所有点都在X的范围内,则该点保留,否则将该点去掉;右边是腐蚀后的结果。可以看出,它仍在原来X的范围内,且比X包含的点要少,就象X被腐蚀掉了一层。
private void erode_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
struction struForm = new struction();
struForm.Text = "腐蚀运算结构元素";
if (struForm.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(, , curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
Marshal.Copy(ptr, grayValues, , bytes); //得到结构元素
byte flagStru = struForm.GetStruction; byte[] tempArray = new byte[bytes];
for (int i = ; i < bytes; i++)
{
tempArray[i] = ;
}
switch (flagStru)
{
case 0x11:
//3位水平方向结构元素
for (int i = ; i < curBitmap.Height; i++)
{
for (int j = ; j < curBitmap.Width - ; j ++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
}
}
}
break;
case 0x21:
//5位水平方向结构元素
for (int i = ; i < curBitmap.Height; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
}
}
}
break;
case 0x12:
//3位垂直方向结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x22:
//5位垂直方向结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x14:
//3位十字形状结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x24:
//5位十字形状结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x18:
//3位方形结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j - ] == &&
grayValues[(i + ) * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j + ] == &&
grayValues[(i + ) * curBitmap.Width + j + ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x28:
//5位方形结构元素
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[(i - ) * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j + ] == &&
grayValues[(i - ) * curBitmap.Width + j + ] == &&
grayValues[(i - ) * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j - ] == &&
grayValues[(i - ) * curBitmap.Width + j] == &&
grayValues[(i - ) * curBitmap.Width + j + ] == &&
grayValues[(i - ) * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j - ] == &&
grayValues[i * curBitmap.Width + j - ] == &&
grayValues[i * curBitmap.Width + j] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[i * curBitmap.Width + j + ] == &&
grayValues[(i + ) * curBitmap.Width + j - ] == &&
grayValues[(i + ) * curBitmap.Width + j - ] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j + ] == &&
grayValues[(i + ) * curBitmap.Width + j + ] == &&
grayValues[(i + ) * curBitmap.Width + j - ] == &&
grayValues[(i + ) * curBitmap.Width + j - ] == &&
grayValues[(i + ) * curBitmap.Width + j] == &&
grayValues[(i + ) * curBitmap.Width + j + ] == &&
grayValues[(i + ) * curBitmap.Width + j + ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
default:
MessageBox.Show("错误的结构元素!");
break;
} grayValues = (byte[])tempArray.Clone();
Marshal.Copy(grayValues, , ptr, bytes);
curBitmap.UnlockBits(bmpData);
} Invalidate();
}
}
#region 关于图像尺寸的说明
//本代码只能处理8位深度的512*512图像。可自行修改,如修改3位水平方向结构元素代码:
//01修改成如下代码即可处理任意尺寸的8位深度的图像
//int bytes = bmpData.Stride * curBitmap.Height;
//for (int i = 0; i < curBitmap.Height; i++)
//{
// for (int j = 1; j < curBitmap.Width - 1; j++)
// {
// if (grayValues[i * bmpData.Stride + j] == 0 &&
// grayValues[i * bmpData.Stride + j + 3] == 0 &&
// grayValues[i * bmpData.Stride + j - 1] == 0)
// {
// tempArray[i * bmpData.Stride + j] = 0;
// tempArray[i * bmpData.Stride + j + 1] = 0;
// tempArray[i * bmpData.Stride + j + 2] = 0;
// }
// }
//}
//02修改成如下代码即可处理任意尺寸的24位深度的图像
//int bytes = bmpData.Stride * curBitmap.Height;
//for (int i = 0; i < curBitmap.Height; i++)
//{
// for (int j = 4; j < curBitmap.Width * 3 - 3; j += 3)
// {
// if (grayValues[i * bmpData.Stride + j] == 0 &&
// grayValues[i * bmpData.Stride + j + 3] == 0 &&
// grayValues[i * bmpData.Stride + j - 1] == 0)
// {
// tempArray[i * bmpData.Stride + j] = 0;
// tempArray[i * bmpData.Stride + j + 1] = 0;
// tempArray[i * bmpData.Stride + j + 2] = 0;
// }
// }
//}
#endregion
膨胀
图像膨胀运算定义
二值图像膨胀运算的数学表达式为:
g(x, y)=dilate[f(x, y), B]=OR[Bf(x,y)]
其中,g(x,y)为膨胀后的二值图像,f(x,y)为原二值图像,B为结构元素。
B(x,y)定义为:
Bf(x,y)={f(x-bx, y-by) ,(bx, by)∈B}
算子OR(x1…xn)定义为:当且仅当x1=…=xn=0时,OR(x1,…xn)等于0;否则为1
膨胀(dilation)可以看做是腐蚀的对偶运算,其定义是:把结构元素B平移a后得到Ba,若Ba击中X,我们记下这个a点。所有满足上述条件的a点组成的集合称做X被B膨胀的结果。用公式表示为:D(X)={a | Ba↑X}=X
B,如图6.13所示。图6.13中X是被处理的对象,B是结构元素,不难知道,对于任意一个在阴影部分的点a,Ba击中X,所以X被B膨胀的结果就是那个阴影部分。阴影部分包括X的所有范围,就象X膨胀了一圈似的,这就是为什么叫膨胀的原因。原理图如下:
实际使用时示意图:
说明:左边是被处理的图象X(二值图象,我们针对的是黑点),中间是结构元素B。膨胀的方法是,拿B的中心点和X上的点及X周围的点一个一个地对,如果B上有一个点落在X的范围内,则该点就为黑;右边是膨胀后的结果。可以看出,它包括X的所有范围,就象X膨胀了一圈似的。
private void dilate_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
struction struForm = new struction();
struForm.Text = "膨胀运算结构元素";
if (struForm.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(, , curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
Marshal.Copy(ptr, grayValues, , bytes); byte flagStru = struForm.GetStruction; byte[] tempArray = new byte[bytes];
for (int i = ; i < bytes; i++)
{
tempArray[i] = ;
} switch (flagStru)
{
case 0x11:
for (int i = ; i < curBitmap.Height; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x21:
for (int i = ; i < curBitmap.Height; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x12:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x22:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x14:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x24:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x18:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[i * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j - ] == ||
grayValues[(i + ) * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j + ] == ||
grayValues[(i + ) * curBitmap.Width + j + ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
case 0x28:
for (int i = ; i < curBitmap.Height - ; i++)
{
for (int j = ; j < curBitmap.Width - ; j++)
{
if (grayValues[(i - ) * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j + ] == ||
grayValues[(i - ) * curBitmap.Width + j + ] == ||
grayValues[(i - ) * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j - ] == ||
grayValues[(i - ) * curBitmap.Width + j] == ||
grayValues[(i - ) * curBitmap.Width + j + ] == ||
grayValues[(i - ) * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j - ] == ||
grayValues[i * curBitmap.Width + j - ] == ||
grayValues[i * curBitmap.Width + j] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[i * curBitmap.Width + j + ] == ||
grayValues[(i + ) * curBitmap.Width + j - ] == ||
grayValues[(i + ) * curBitmap.Width + j - ] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j + ] == ||
grayValues[(i + ) * curBitmap.Width + j + ] == ||
grayValues[(i + ) * curBitmap.Width + j - ] == ||
grayValues[(i + ) * curBitmap.Width + j - ] == ||
grayValues[(i + ) * curBitmap.Width + j] == ||
grayValues[(i + ) * curBitmap.Width + j + ] == ||
grayValues[(i + ) * curBitmap.Width + j + ] == )
{
tempArray[i * curBitmap.Width + j] = ;
} }
}
break;
default:
MessageBox.Show("错误的结构元素!");
break;
} grayValues = (byte[])tempArray.Clone(); System.Runtime.InteropServices.Marshal.Copy(grayValues, , ptr, bytes);
curBitmap.UnlockBits(bmpData);
} Invalidate();
}
}
#region 关于图像尺寸的说明
//本代码只能处理8位深度的512*512图像。可自行修改,例如修改3位水平方向结构元素代码:
//01修改成如下代码即可处理任意尺寸的8位深度的图像
//int bytes = bmpData.Stride * curBitmap.Height;
//for (int i = 0; i < curBitmap.Height; i++)
//{
// for (int j = 1; j < curBitmap.Width - 1; j++)
// {
// if (grayValues[i * bmpData.Stride + j] == 0 ||
// grayValues[i * bmpData.Stride + j + 3] == 0 ||
// grayValues[i * bmpData.Stride + j - 1] == 0)
// {
// tempArray[i * bmpData.Stride + j] = 0;
// tempArray[i * bmpData.Stride + j + 1] = 0;
// tempArray[i * bmpData.Stride + j + 2] = 0;
// }
// }
//}
//02修改成如下代码即可处理任意尺寸的24位深度的图像
//int bytes = bmpData.Stride * curBitmap.Height;
//for (int i = 0; i < curBitmap.Height; i++)
//{
// for (int j = 4; j < curBitmap.Width * 3 - 3; j += 3)
// {
// if (grayValues[i * bmpData.Stride + j] == 0 ||
// grayValues[i * bmpData.Stride + j + 3] == 0 ||
// grayValues[i * bmpData.Stride + j - 1] == 0)
// {
// tempArray[i * bmpData.Stride + j] = 0;
// tempArray[i * bmpData.Stride + j + 1] = 0;
// tempArray[i * bmpData.Stride + j + 2] = 0;
// }
// }
//}
#endregion
c#数字图像处理(十二)图像的腐蚀与膨胀的更多相关文章
- Win8MetroC#数字图像处理--2.2图像二值化函数
原文:Win8MetroC#数字图像处理--2.2图像二值化函数 [函数代码] /// <summary> /// Binary process. /// </summary> ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
- Win8 Metro(C#)数字图像处理--3.3图像直方图计算
原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...
- Win8 Metro(C#)数字图像处理--3.4图像信息熵计算
原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...
- Win8 Metro(C#)数字图像处理--3.5图像形心计算
原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...
- Win8 Metro(C#)数字图像处理--3.1图像均值计算
原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.74图像凸包计算
原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器
原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.52图像K均值聚类
原文:Win8 Metro(C#)数字图像处理--2.52图像K均值聚类 [函数名称] 图像KMeans聚类 KMeansCluster(WriteableBitmap src,i ...
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称] 图像雾化 AtomizationProcess(WriteableBitmap src,i ...
随机推荐
- JS闭包机制实现为DOM元素循环添加事件
HTML代码: <button type='button' class='btn' id='1'>按钮1</button> <button type='button' c ...
- linux查看文件内容跳到文件底部和回到文件顶部的快捷键
有时候需要查看一些日志文件,然后要从底部开始查看的话 可以按 shift+g 即可跳到文件底部 要返回文件顶部的时候 按 gg即可
- 使用idea构建Hibernate5项目
使用工具:IntelliJ IDEA 2017.2.5 x64 MySql-8.0.1 hibernate-release-5.3.1.Final 导入的jar包: 以及连接MySql的jdbc包.和 ...
- Java8 LocalDate计算两个日期的间隔天数
Java8新增了java.time包,提供了很多新封装好的类,使我们可以摆脱原先使用java.util.Time以及java.util.Calendar带来的复杂. 其中LocalDate正是本文中使 ...
- $CH5501$ 环路运输 环形$+$单调队列
CH Description 在一条环形公路旁均匀地分布着N座仓库,编号为1~N,编号为 i 的仓库与编号为 j 的仓库之间的距离定义为 dist(i,j)=min(|i-j|,N-|i-j|),也 ...
- [quack] A browser extension to Clean website pages ADs
1. youdao.com result page Before: After: Later we will add more and more. Or you can submit issues t ...
- ThreadLocal源码阅读
package java.lang; import java.lang.ref.WeakReference; import java.util.Objects; import java.util.co ...
- Centos虚拟机安装指南
按照文档安装有任何问题,欢迎随时留言 ·准备工作: linux发行版CentOS镜像下载地址: http://isoredirect.centos.org/centos/7/isos/x86_64/ ...
- 「CH2501」 矩阵距离 解题报告
CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...
- TensorFlow——训练模型的保存和载入的方法介绍
我们在训练好模型的时候,通常是要将模型进行保存的,以便于下次能够直接的将训练好的模型进行载入. 1.保存模型 首先需要建立一个saver,然后在session中通过saver的save即可将模型保存起 ...