图像算法---洪水填充算法函数
2016-05-13 08:54
1081 查看
洪水填充算法也叫种子生长算法,算法步骤如下:
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。
[函数代码]
///
/// Flood fill.
///
/// The source image.
/// The start point to be filled.
/// The color to be filled.
/// One parameter to control fill effect, from 0 to 255.
///
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;
}
}
[图像效果]
Fig.2 原图 Fig.3效果图
demo下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=121&extra=page%3D1
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。
[函数代码]
///
/// Flood fill.
///
/// The source image.
/// The start point to be filled.
/// The color to be filled.
/// One parameter to control fill effect, from 0 to 255.
///
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;
}
}
[图像效果]
Fig.2 原图 Fig.3效果图
demo下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=121&extra=page%3D1
相关文章推荐
- 图像基础---直方图均衡化
- ConcurrentHashMap线程安全
- webAPP和原生Android的APP有什
- jQuery插件之lightBox图片灯箱
- javac参数(-classpath, -sourcepath)详解
- 最简单的tab切换实例代码
- 图像基本变换---KMeans聚类算法
- Android Studio安装genymotion插件
- JS回到顶部代码小记
- 图像基本变换---Canny边缘检测算法
- 关于hibernate总是报错 配置factory的id找不到,mapping配置文件Could not parse mapping document from input stream
- 关于Android v7包的资源文件错误问题解决方案
- 学习Android自定义Spinner适配器
- ByteBuffer的allocate和allocateDirect区别
- Linux中rar软件安装方法及使用
- js/jQuery判断浏览器名称、内核版本、浏览器壳
- 事情闹成这样,平安信托还有必要收购汽车之家么?
- 图像基本变换---Harris角点检测算法
- Flask的点点滴滴
- 图像基本变换---图像快速高斯模糊算法