原文:Win8 Metro(C#)数字图像处理--2.66FloodFill算法



[函数名称]

  洪水填充算法函数

WriteableBitmap FloodfillProcess(WriteableBitmap src,Point location, Color fillColor, int threshold)

2,以这个点为起点,将它压入栈中,假设我们要填充的颜色为A,则将该点颜色设置为A,然后判断它的四邻域像素,这里我们设置一个颜色阈值T,假设当前像素灰度值为P(x,y),四邻域像素为M(n),n=1,2,3,4,那么判断当前像素与四邻域像素的灰度差值D=|P-M|,如果D小于T, 那么我们将该像素M作为下一个种子点,压入栈中,否则继续判断。如图中黑色像素的四邻域内有一灰色点,与其差值小于T,则把它作为新的种子点压入栈中,继续判断。

3,当栈为空时,种子填充结束,否则重做步骤2。

[函数代码]

       /// <summary>
/// Flood fill.
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="location">The start point to be filled.</param>
/// <param name="fillColor">The color to be filled.</param>
/// <param name="threshold">One parameter to control fill effect, from 0 to 255.</param>
/// <returns></returns>
public static WriteableBitmap FloodfillProcess(WriteableBitmap src,Point location, Color fillColor, int threshold)////洪水填充算法
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
Stack<Point> fillPoints = new Stack<Point>(w * h);
int[,] mask = new int[w, h];
WriteableBitmap fillImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
Color backColor = Color.FromArgb(0,tempMask[(int)location.X * 4 + 2 + (int)location.Y * w * 4], tempMask[(int)location.X * 4 + 1 + (int)location.Y * w * 4], tempMask[(int)location.X * 4 + (int)location.Y * w * 4]);
int gray = (int)((backColor.R + backColor.G + backColor.B) / 3);
if (location.X < 0 || location.X >= w || location.Y < 0 || location.Y >= h) return null;
fillPoints.Push(new Point(location.X, location.Y));
while (fillPoints.Count > 0)
{
Point p = fillPoints.Pop();
mask[(int)p.X, (int)p.Y] = 1;
tempMask[4 * (int)p.X + (int)p.Y * w*4] = (byte)fillColor.B;
tempMask[4 * (int)p.X + 1 + (int)p.Y * w*4] = (byte)fillColor.G;
tempMask[4 * (int)p.X + 2 +(int) p.Y * w*4] = (byte)fillColor.R;
if (p.X > 0 && (Math.Abs(gray - (int)((tempMask[4 * ((int)p.X - 1) + (int)p.Y * w * 4] + tempMask[4 * ((int)p.X - 1) + 1 + (int)p.Y * w * 4] + tempMask[4 * ((int)p.X - 1) + 2 + (int)p.Y * w * 4]) / 3)) < threshold) && (mask[(int)p.X - 1, (int)p.Y] != 1))
{
tempMask[4 * ((int)p.X - 1) + (int)p.Y * w*4] = (byte)fillColor.B;
tempMask[4 * ((int)p.X - 1) + 1 + (int)p.Y * w*4] = (byte)fillColor.G;
tempMask[4 * ((int)p.X - 1) + 2 + (int)p.Y * w*4] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X - 1, p.Y));
mask[(int)p.X - 1, (int)p.Y] = 1;
}
if (p.X < w - 1 && (Math.Abs(gray - (int)((tempMask[4 * ((int)p.X + 1) + (int)p.Y * w * 4] + tempMask[4 * ((int)p.X + 1) + 1 + (int)p.Y * w * 4] + tempMask[4 * ((int)p.X + 1) + 2 + (int)p.Y * w * 4]) / 3)) < threshold) && (mask[(int)p.X + 1, (int)p.Y] != 1))
{
tempMask[4 * ((int)p.X + 1) + (int)p.Y * w * 4] = (byte)fillColor.B;
tempMask[4 * ((int)p.X + 1) + 1 + (int)p.Y * w * 4] = (byte)fillColor.G;
tempMask[4 * ((int)p.X + 1) + 2 + (int)p.Y * w * 4] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X + 1, p.Y));
mask[(int)p.X + 1, (int)p.Y] = 1;
}
if (p.Y > 0 && (Math.Abs(gray - (int)((tempMask[4 * (int)p.X + ((int)p.Y - 1) * w * 4] + tempMask[4 * (int)p.X + 1 + ((int)p.Y - 1) * w * 4] + tempMask[4 * (int)p.X + 2 + ((int)p.Y - 1) * w * 4]) / 3)) < threshold) && (mask[(int)p.X, (int)p.Y - 1] != 1))
{
tempMask[4 * (int)p.X + ((int)p.Y - 1) * w * 4] = (byte)fillColor.B;
tempMask[4 * (int)p.X + 1 + ((int)p.Y - 1) * w * 4] = (byte)fillColor.G;
tempMask[4 * (int)p.X + 2 + ((int)p.Y - 1) * w * 4] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X, p.Y - 1));
mask[(int)p.X, (int)p.Y - 1] = 1;
}
if (p.Y < h - 1 && (Math.Abs(gray - (int)((tempMask[4 * (int)p.X + ((int)p.Y + 1) * w * 4] + tempMask[4 * (int)p.X + 1 + ((int)p.Y + 1) * w * 4] + tempMask[4 * (int)p.X + 2 + ((int)p.Y + 1) * w * 4]) / 3)) < threshold) && (mask[(int)p.X, (int)p.Y + 1] != 1))
{
tempMask[4 * (int)p.X + ((int)p.Y + 1) * w * 4] = (byte)fillColor.B;
tempMask[4 * (int)p.X + 1 + ((int)p.Y + 1) * w * 4] = (byte)fillColor.G;
tempMask[4 * (int)p.X + 2 + ((int)p.Y + 1) * w * 4] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X, p.Y + 1));
mask[(int)p.X, (int)p.Y + 1] = 1;
}
}
fillPoints.Clear();
temp = (byte[])tempMask.Clone();
Stream sTemp = fillImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return fillImage;
}
else
{
return null;
}
}

最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载:

Win8 Metro(C#)数字图像处理--2.66FloodFill算法的更多相关文章

  1. Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法

    原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...

  2. Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法

    原文:Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法  [函数名称]   高斯平滑滤波器      GaussFilter(WriteableBitmap src,int r ...

  3. Win8 Metro(C#)数字图像处理--2.60部分彩色保留算法

    原文:Win8 Metro(C#)数字图像处理--2.60部分彩色保留算法  [函数名称]   部分彩色保留函数       WriteableBitmap PartialcolorProcess ...

  4. Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法

    原文:Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法  [算法说明] Canny边缘检测算法可以分为4步:高斯滤波器平滑处理.梯度计算.非极大值抑制.双阈值边缘检 测和 ...

  5. Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法

    原文:Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法  [函数名称]   二值图像细化算法      WriteableBitmap ThinningProcess ...

  6. Win8 Metro(C#)数字图像处理--2.44图像油画效果算法

    原文:Win8 Metro(C#)数字图像处理--2.44图像油画效果算法  [函数名称]   图像油画效果      OilpaintingProcess(WriteableBitmap src ...

  7. Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法

    原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称]   图像雾化         AtomizationProcess(WriteableBitmap src,i ...

  8. Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法

    原文:Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法  [函数名称]   红眼去除     RedeyeRemoveProcess(WriteableBitmap src) ...

  9. Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法

    原文:Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法  [函数名称]   二值图像轮廓提取         ContourExtraction(WriteableBitm ...

随机推荐

  1. js进阶 9-12 js如何实现级联菜单 (章节测试)

    js进阶 9-12  js如何实现级联菜单 (章节测试) 一.总结 一句话总结: 1.js如何实现级联菜单 ? 二维数组,以第一级菜单的文本值做键,以对应的二级菜单选项的文本做值 2.用哪个属性可以获 ...

  2. Eclipse离线单独安装hibernate tools成功率低

    原因:单独下载的hibernate tools插件应该缺少部分需要的组件,安装时,边联网,成功率很低 解决方法:下载jboss tools的全插件包,安装时,只选择hibernate tools插件可 ...

  3. ANR问题分析实例

    ANR监测机制包含三种: Service ANR,前台进程中Service生命周期不能超过20秒,后台进程中Service的生命周期不能超过200秒. 在启动Service时,抛出定时消息SERVIC ...

  4. 【p094】道路游戏

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有n个机器人工厂,两个相邻机器人工厂之间由 ...

  5. 【t045】细菌

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 近期,农场出现了D (1<= D <=15)种细菌.John 要从他的 N (1<= ...

  6. docker部署netcore应用(二)

    基于第一章已经安装好了docker,这次将把netcore应用部署到docker容器中 开发工具vs2017,准备个DotNet Core的Console应用程序,测试一下 发布DockerTest项 ...

  7. Winform中GridView分组排序实现功能

    由于客户最近要扩充公司的业务,之前基于Winform+web开发混合式的系统已经不能满足他们的需求,需要从新对系统进行分区处理. 考虑到系统模块里面用到的GridView视图比较多,我就结合了DevE ...

  8. JackSon fasterxml学习

    概述 Jackson框架是基于Java平台的一套数据处理工具,被称为"最好的JavaJson解析器".  Jackson框架包含了3个核心库:streaming,databind, ...

  9. java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结

    代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...

  10. 一个2013届毕业生(踏上IT行业)的迷茫(4)

    等了大概三个月,终于到9月份了,以前没有出过远门,这次要去西安上学,一个人父母还是不放心,带了几件衣服就和父亲匆匆去坐火车,这一路有多少个第一次啊,第一次和父亲一块坐车.第一次坐火车.第一次出县城.第 ...