Win8Metro(C#)数字图像处理--2.22二值图像膨胀
[函数名称]
二值图像膨胀函数DilationProcess(WriteableBitmap src)
[算法说明]
膨胀算法也是属于形态学算法的范畴,前一节已经简单介绍了形态学,这里不再累赘。
我们这里介绍的膨胀算法依旧采用上一节腐蚀中的结构元素S,则算法过程如下:
用通俗的话讲就是,用结构元素作为模板在原始二值图像种平滑一遍,扫描图像的每一个像素,用结构元素中的每一个元素与其覆盖的二值图像做“或”操作(假设结构元素都为1),如果结果为1,则二值图像中对应结构元素原点位置的像素值为1,否则为0。
[函数代码]
///<summary>
/// Dilation process.
///</summary>
///<param name="src">The source image(It should be the binary image).</param>
///<returns></returns>
publicstaticWriteableBitmap DilationProcess(WriteableBitmap src)////22图像膨胀运算
{
if (src !=null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap dilationImage =newWriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
if (i == 0 || i == w - 1 || j == 0 || j == h - 1)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
{
if (tempMask[i * 4 - 4 + j * w * 4] == 255 || tempMask[i * 4 + j * w * 4] == 255 || tempMask[i * 4 + 4 + j * w * 4] == 255
|| tempMask[i * 4 + (j - 1) * w * 4] == 255 || tempMask[i * 4 + (j + 1) * w * 4] == 255)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
{
temp[i * 4 + j * w * 4] = 0;
temp[i * 4 + 1 + j * w * 4] = 0;
temp[i * 4 + 2 + j * w * 4] = 0;
}
}
}
}
Stream sTemp = dilationImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return dilationImage;
}
else
{
returnnull;
}
}
[图像效果]
[函数名称]
二值图像膨胀函数DilationProcess(WriteableBitmap src)
[算法说明]
膨胀算法也是属于形态学算法的范畴,前一节已经简单介绍了形态学,这里不再累赘。
我们这里介绍的膨胀算法依旧采用上一节腐蚀中的结构元素S,则算法过程如下:
用通俗的话讲就是,用结构元素作为模板在原始二值图像种平滑一遍,扫描图像的每一个像素,用结构元素中的每一个元素与其覆盖的二值图像做“或”操作(假设结构元素都为1),如果结果为1,则二值图像中对应结构元素原点位置的像素值为1,否则为0。
[函数代码]
///<summary>
/// Dilation process.
///</summary>
///<param name="src">The source image(It should be the binary image).</param>
///<returns></returns>
publicstaticWriteableBitmap DilationProcess(WriteableBitmap src)////22图像膨胀运算
{
if (src !=null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap dilationImage =newWriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
if (i == 0 || i == w - 1 || j == 0 || j == h - 1)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
{
if (tempMask[i * 4 - 4 + j * w * 4] == 255 || tempMask[i * 4 + j * w * 4] == 255 || tempMask[i * 4 + 4 + j * w * 4] == 255
|| tempMask[i * 4 + (j - 1) * w * 4] == 255 || tempMask[i * 4 + (j + 1) * w * 4] == 255)
{
temp[i * 4 + j * w * 4] = (byte)255;
temp[i * 4 + 1 + j * w * 4] = (byte)255;
temp[i * 4 + 2 + j * w * 4] = (byte)255;
}
else
{
temp[i * 4 + j * w * 4] = 0;
temp[i * 4 + 1 + j * w * 4] = 0;
temp[i * 4 + 2 + j * w * 4] = 0;
}
}
}
}
Stream sTemp = dilationImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return dilationImage;
}
else
{
returnnull;
}
}
[图像效果]