原文:Win8 Metro(C#)数字图像处理--2.42图像光照效果算法



[函数名称]

图像光照效果  SunlightProcess(WriteableBitmap src,int X,int Y,float thresould)

[算法说明]

  图像光照效果就是在图像中添加上一个太阳光源,以此模仿光照条件。主要算法包括:

1光源选择;2光照像素值求取。

  1,光源选择。我们假设光源位置坐标为S(X,Y),其中光源坐标一定要保证在图像大小

范围内。有了光源位置,我们就可以来构建一个圆形区域模拟光照了。我们设定光源半

径为R,那么,光照范围就是以S(X,Y)为圆心,以R为半径的圆了。

  根据光源特性,在图像中表现为中间靠近圆心最亮,亮度延半径方向向四周逐渐减弱。

因此,我们根据光源圆形的范围内像素距离圆心的欧几里得距离来构建线性变换的公式,

假设欧几里得距离为D,变换后的像素值为f,则公式如下:

       /// <summary>
/// Sun light process.
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="A">X location of light source.</param>
/// <param name="B">Y location of light source.</param>
/// <param name="thresould">Light intensity value.</param>
/// <returns></returns>
public static WriteableBitmap SunlightProcess(WriteableBitmap src,int X,int Y,float thresould)////41图像光照函数
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap srcImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
double b = 0, g = 0, r = 0;
if (X >= w || Y >= h || X < 0 || Y < 0)
{
X = w / 2;
Y = h / 2;
}
Point Cen = new Point(X, Y);
int R = Math.Min(X, Y);
float curR = 0;
float pixelValue = 0;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i ++)
{
b = tempMask[i * 4 + j * w * 4];
g = tempMask[i * 4 + 1 + j * w * 4];
r = tempMask[i * 4 + 2 + j * w * 4];
curR=(float)Math .Sqrt(Math .Pow((i-Cen .X ),2)+Math .Pow ((j-Cen.Y ),2));
if (curR < R)
{
pixelValue = thresould * (1.0f - curR / R);
b = b + pixelValue;
g = g + pixelValue;
r = r + pixelValue;
temp[i*4 + j * w * 4] = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);
temp[i * 4 + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);
temp[i * 4 + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);
b = 0; g = 0; r = 0;
}
}
}
Stream sTemp = srcImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return srcImage;
}
else
{
return null;
}
}


Win8 Metro(C#)数字图像处理--2.42图像光照效果算法的更多相关文章

  1. Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法

    原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称]   图像雾化         AtomizationProcess(WriteableBitmap src,i ...

  2. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

  3. Win8 Metro(C#)数字图像处理--3.3图像直方图计算

    原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...

  4. Win8 Metro(C#)数字图像处理--3.4图像信息熵计算

    原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...

  5. Win8 Metro(C#)数字图像处理--3.5图像形心计算

    原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...

  6. Win8 Metro(C#)数字图像处理--3.1图像均值计算

    原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...

  7. Win8 Metro(C#)数字图像处理--2.74图像凸包计算

    原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...

  8. Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器

    原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...

  9. Win8 Metro(C#)数字图像处理--2.52图像K均值聚类

    原文:Win8 Metro(C#)数字图像处理--2.52图像K均值聚类  [函数名称]   图像KMeans聚类      KMeansCluster(WriteableBitmap src,i ...

随机推荐

  1. [tmux] Manage terminal workspaces using session naming

    It's a lot easier to manage your tmux session when they have sensible names. We'll cover: How to cre ...

  2. 【机器学习实战】第8章 预测数值型数据:回归(Regression)

    第8章 预测数值型数据:回归 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/ ...

  3. [Ramda] Change Object Properties with Ramda Lenses

    In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...

  4. [Ramda] Pick and Omit Properties from Objects Using Ramda

    Sometimes you just need a subset of an object. In this lesson, we'll cover how you can accomplish th ...

  5. sublime Package Control 设备

    sublime  插件的官方网站 https://sublime.wbond.net/ 点击Installation watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...

  6. hudson添加批处理编译命令的注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 Hudson可以在Build Step里添加自定义的批处理命令,如下图: 需要注意的是这些批处理命令并不能识别huds ...

  7. LoadFromStr的使用中出现错误“未结束的字符串常量”

    最近遇到个奇怪的问题,就是关于js参数中待换行符时,出现了错误“为结束的字符串常量”. 解决方法是:不直接将该数据以参数形式传递,而是先将其赋值在一个隐藏的文本内,需要调用的函数里只需读取该文本里的内 ...

  8. jquery 源码学习(二)

    在网上找到一篇广为流传的文章<常用正则表达式>,逐一分析,不足地方进行补充和纠正   作者:nuysoft/JS攻城师/高云 QQ:47214707 EMail:nuysoft@gmail ...

  9. C#6

    C#6   1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: 1 public string FirstName { get; privat ...

  10. elasticsearch start

    启动.停止服务 默认官方版启动: linux:./bin/elasticsearch start window:直接运行bin/elasticsearch.bat 默认官方版停止: linux:kil ...