Win8 Metro(C#)数字图像处理--2.66FloodFill算法
原文: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算法的更多相关文章
- Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法
原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...
- Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法
原文:Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法 [函数名称] 高斯平滑滤波器 GaussFilter(WriteableBitmap src,int r ...
- Win8 Metro(C#)数字图像处理--2.60部分彩色保留算法
原文:Win8 Metro(C#)数字图像处理--2.60部分彩色保留算法 [函数名称] 部分彩色保留函数 WriteableBitmap PartialcolorProcess ...
- Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法
原文:Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法 [算法说明] Canny边缘检测算法可以分为4步:高斯滤波器平滑处理.梯度计算.非极大值抑制.双阈值边缘检 测和 ...
- Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
原文:Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法 [函数名称] 二值图像细化算法 WriteableBitmap ThinningProcess ...
- Win8 Metro(C#)数字图像处理--2.44图像油画效果算法
原文:Win8 Metro(C#)数字图像处理--2.44图像油画效果算法 [函数名称] 图像油画效果 OilpaintingProcess(WriteableBitmap src ...
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称] 图像雾化 AtomizationProcess(WriteableBitmap src,i ...
- Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法
原文:Win8 Metro(C#)数字图像处理--2.47人脸红眼去除算法 [函数名称] 红眼去除 RedeyeRemoveProcess(WriteableBitmap src) ...
- Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法
原文:Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法 [函数名称] 二值图像轮廓提取 ContourExtraction(WriteableBitm ...
随机推荐
- 【37%】【poj1436】Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5200 Accepted: 1903 Description There ...
- url前面双斜杠、单斜杠、无斜杠、点+单斜杠的总结
原文:url前面双斜杠.单斜杠.无斜杠.点+单斜杠的总结 本来只是一个绝对url和相对url的简单问题,但实际使用中会碰到一些不常见的,比如双斜杠,经常不用竟然忘了,做一下总结.可以参考一下这篇文章 ...
- 检索05 --static静态方法 和 非静态方法
C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问一个类的所有实例的同一C#静 ...
- 微信小程序的轮播图swiper问题
微信小程序的轮播图swiper,调用后,怎样覆盖系统的 点,达到自己想要的效果 不多说,先上一图望大家多给意见: 这个是效果图: 微信小程序效果图就成这样子: <view class=" ...
- js限制文本框input只能输入数字
JS判断只能是数字和小数点. ,文本框只能输入数字代码(小数点也不能输入) 复制代码 代码示例:<input onkeyup="this.value=this.value.replac ...
- Android平台第三方应用分享到微信开发
一.申请APPID 微信公共平台和微博分享一样,也需要申请一个ID,来作为调起微信.分享到微信的唯一标识. 申请微信APPID可以到微信平台http://open.weixin.qq.com/app/ ...
- .NET 中使用 Mutex 进行跨越进程边界的同步 - walterlv
原文:.NET 中使用 Mutex 进行跨越进程边界的同步 - walterlv .NET 中使用 Mutex 进行跨越进程边界的同步 2018-12-30 08:41 Mutex 是 Mutual ...
- ajax的跨域请求问题:减少options请求
服务器端在Response Headers里添加字段Access-Control-Max-Age: 86400 , "Access-Control-Max-Age"表明在86400 ...
- 一言不合就写socket的post和get请求(拼内容,然后发出去即可)
一言不合就写socket的post和get请求.写个桌面程序,利用java写get和post请求.测试成功: SocketReq.java package com.test.CipherIndex; ...
- EF codefirst第一篇
一直以来喜欢dbfirst 因为简单,一直不明白为什么codefirst会是主流,根据对ddd的学习终于知道了codefirst的目的 本文是对博客园 小崔的笔记本 文章 EF实体框架之CodeFi ...