原文: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. 【vs调试】PDB 文件:每个开发人员都必须知道的

    [vs调试]PDB文件:每个开发人员都必须知道的 GDB:The GNU Project Debugger, 将会包含代码中符号(自定义变量, 数据类型), 还有函数调用或类引用的关联性, 有了pdb ...

  2. 百度消息推送REST API探究

    一.百度云推送介绍 云推送(Push)是百度开放云向开发者提供的消息推送服务:通过利用云端与客户端之间建立稳定.可靠的长连接来为开发者提供向客户端应用推送实时消息服务. 百度云推送服务支持推送三种类型 ...

  3. show binlog events 命令查看某个binlog日志内容

    mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];   选项解析:   IN 'l ...

  4. 微信小程序从零开始开发步骤(一)

    从零到有写一个小程序系列专题,很早以前就想写来分享,但由于项目一直在进展,没有过多的时间研究技术,现在可以继续分享了.1:注册用没有注册过微信公众平台的邮箱注册一个微信公众号, 申请帐号 ,点击 ht ...

  5. 《写给大忙人看的Java SE 8》——Java8新特性总结

    阅读目录 接口中的默认方法和静态方法 函数式接口和Lambda表达式 Stream API 新的日期和时间 API 杂项改进 参考资料 回到顶部 接口中的默认方法和静态方法 先考虑一个问题,如何向Ja ...

  6. NOIP模拟 string - ac自动机

    题目大意: 给n个字符串(100位以内),和一个长串s(100000位以内),求n个字符串在s中出现的次数.然后给出m次修改,每次修改s中的一个字符,对于每次修改,输出更新后的答案(修改会保存). 题 ...

  7. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. WPF依赖属性值源(BaseValueSource)

    原文:WPF依赖属性值源(BaseValueSource)   WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源 其以BaseValueSource枚举表示 1.Default public ...

  9. QT之二级菜单(二级菜单的箭头可以使用QSS设置图片)

    QT之二级菜单 QT之二级菜单 开场白 效果图 上代码 可参考文章 下代码 结尾 开场白 今天我们一起来了解下,在我们QT中,二级菜单是如何实现的,在上篇我们学习了QT之系统托盘,QT之自定义菜单,  ...

  10. listview分页载入问题

    方案一: 底部有查看很多其它能够使用HeaderViewListAdapter 假设须要加入数据, 就向Adapter绑定的数据里面加入. 然后调用Adapter.notifyDataSetChang ...