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;
             }
         }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值