当前位置:   article > 正文

图像填充算法--洪水填充FloodFill_洪水填充算法

洪水填充算法


  

    洪水填充算法也叫种子生长算法,洪水填充算法是在很多图形绘制软件中常用的填充算法,比如Windows paint的油漆桶功能,这个算法的实现有多种方式,比如递归实现,扫描线算法等等,基于像素四邻域/八邻域等,本文基于四邻域实现,主要算法步骤如下:

1,在图像中选择一个种子点,如下图所示:

                        

                                Fig.1示意图

    种子点为图像中的黑色方框,即黑色像素点。

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

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

    主要函数实现代码如下所示:

  1. /// <summary>
  2. /// Flood fill algorithm.
  3. /// </summary>
  4. /// <param name="src">The source image.</param>
  5. /// <param name="location">The start point.</param>
  6. /// <param name="fillColor">The color to be filled.</param>
  7. /// <param name="threshould">One parameter to control fill effect,from 0 to 255.</param>
  8. /// <returns></returns>
  9. public Bitmap FloodFill(Bitmap src, Point location, Color fillColor, int threshould)
  10. {
  11. try
  12. {
  13. Bitmap a = new Bitmap(src);
  14. int w = a.Width;
  15. int h = a.Height;
  16. Stack<Point> fillPoints = new Stack<Point>(w * h);
  17. System.Drawing.Imaging.BitmapData bmpData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  18. IntPtr ptr = bmpData.Scan0;
  19. int stride = bmpData.Stride;
  20. int bytes = bmpData.Stride * a.Height;
  21. byte[] grayValues = new byte[bytes];
  22. System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
  23. byte[] temp = (byte[])grayValues.Clone();
  24. Color backColor = Color.FromArgb(temp[location.X * 3 + 2 + location.Y * stride], temp[location.X * 3 + 1 + location.Y * stride], temp[location.X * 3 + location.Y * stride]);
  25. int gray = (int)((backColor.R + backColor.G + backColor.B) / 3);
  26. if (location.X < 0 || location.X >= w || location.Y < 0 || location.Y >= h) return null;
  27. fillPoints.Push(new Point(location.X, location.Y));
  28. int[,] mask = new int[w, h];
  29. while (fillPoints.Count > 0)
  30. {
  31. Point p = fillPoints.Pop();
  32. mask[p.X, p.Y] = 1;
  33. temp[3 * p.X + p.Y * stride] = (byte)fillColor.B;
  34. temp[3 * p.X + 1 + p.Y * stride] = (byte)fillColor.G;
  35. temp[3 * p.X + 2 + p.Y * stride] = (byte)fillColor.R;
  36. if (p.X > 0 && (Math.Abs(gray - (int)((temp[3 * (p.X - 1) + p.Y * stride] + temp[3 * (p.X - 1) + 1 + p.Y * stride] + temp[3 * (p.X - 1) + 2 + p.Y * stride]) / 3)) < threshould) && (mask[p.X - 1, p.Y] != 1))
  37. {
  38. temp[3 * (p.X - 1) + p.Y * stride] = (byte)fillColor.B;
  39. temp[3 * (p.X - 1) + 1 + p.Y * stride] = (byte)fillColor.G;
  40. temp[3 * (p.X - 1) + 2 + p.Y * stride] = (byte)fillColor.R;
  41. fillPoints.Push(new Point(p.X - 1, p.Y));
  42. mask[p.X - 1, p.Y] = 1;
  43. }
  44. if (p.X < w - 1 && (Math.Abs(gray - (int)((temp[3 * (p.X + 1) + p.Y * stride] + temp[3 * (p.X + 1) + 1 + p.Y * stride] + temp[3 * (p.X + 1) + 2 + p.Y * stride]) / 3)) < threshould) && (mask[p.X + 1, p.Y] != 1))
  45. {
  46. temp[3 * (p.X + 1) + p.Y * stride] = (byte)fillColor.B;
  47. temp[3 * (p.X + 1) + 1 + p.Y * stride] = (byte)fillColor.G;
  48. temp[3 * (p.X + 1) + 2 + p.Y * stride] = (byte)fillColor.R;
  49. fillPoints.Push(new Point(p.X + 1, p.Y));
  50. mask[p.X + 1, p.Y] = 1;
  51. }
  52. if (p.Y > 0 && (Math.Abs(gray - (int)((temp[3 * p.X + (p.Y - 1) * stride] + temp[3 * p.X + 1 + (p.Y - 1) * stride] + temp[3 * p.X + 2 + (p.Y - 1) * stride]) / 3)) < threshould) && (mask[p.X, p.Y - 1] != 1))
  53. {
  54. temp[3 * p.X + (p.Y - 1) * stride] = (byte)fillColor.B;
  55. temp[3 * p.X + 1 + (p.Y - 1) * stride] = (byte)fillColor.G;
  56. temp[3 * p.X + 2 + (p.Y - 1) * stride] = (byte)fillColor.R;
  57. fillPoints.Push(new Point(p.X, p.Y - 1));
  58. mask[p.X, p.Y - 1] = 1;
  59. }
  60. if (p.Y < h - 1 && (Math.Abs(gray - (int)((temp[3 * p.X + (p.Y + 1) * stride] + temp[3 * p.X + 1 + (p.Y + 1) * stride] + temp[3 * p.X + 2 + (p.Y + 1) * stride]) / 3)) < threshould) && (mask[p.X, p.Y + 1] != 1))
  61. {
  62. temp[3 * p.X + (p.Y + 1) * stride] = (byte)fillColor.B;
  63. temp[3 * p.X + 1 + (p.Y + 1) * stride] = (byte)fillColor.G;
  64. temp[3 * p.X + 2 + (p.Y + 1) * stride] = (byte)fillColor.R;
  65. fillPoints.Push(new Point(p.X, p.Y + 1));
  66. mask[p.X, p.Y + 1] = 1;
  67. }
  68. }
  69. fillPoints.Clear();
  70. grayValues = (byte[])temp.Clone();
  71. System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
  72. a.UnlockBits(bmpData);
  73. return a;
  74. }
  75. catch (Exception exp)
  76. {
  77. MessageBox.Show(exp.Message);
  78. return null;
  79. }
  80. }

    最后给个Demo的例子,免费下载地址:点击打开链接

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/859535
推荐阅读
相关标签
  

闽ICP备14008679号