图像基本变换---图像亮度对比度调增算法
来源:互联网 发布:软件特性介绍 编辑:程序博客网 时间:2024/04/29 13:25
[图像亮度]
图像亮度调整公式如公式2-(5)所示:
其中v属于[-255, 255]。
由于像素值的大小范围为[0,255],因此,像素的亮度值可以用原始值与调整增量的和表示,且最大亮度为255,即白色,最小亮度为0,即黑色。
[函数代码]
/// <summary>
/// Bright adjust process.
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="brightnessValue">Brightness value, from -255 to 255.</param>
/// <returns></returns>
public static WriteableBitmap BrightnessAdjustProcess(WriteableBitmap src,int brightnessValue)////5 亮度调整
{
if(src!=null )
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap brightImage = new WriteableBitmap(w,h);
byte[] temp = src.PixelBuffer.ToArray();
for (int i = 0; i < temp.Length; i += 4)
{
temp[i] = (byte)(((brightnessValue + temp[i]) > 255 ? 255 : (brightnessValue + temp[i])) < 0 ? 0 : ((brightnessValue + temp[i]) > 255 ? 255 : (brightnessValue + temp[i])));
temp[i+1] = (byte)(((brightnessValue + temp[i+1]) > 255 ? 255 : (brightnessValue + temp[i+1])) < 0 ? 0 : ((brightnessValue + temp[i+1]) > 255 ? 255 : (brightnessValue + temp[i+1])));
temp[i+2] = (byte)(((brightnessValue + temp[i+2]) > 255 ? 255 : (brightnessValue + temp[i+2])) < 0 ? 0 : ((brightnessValue + temp[i+2]) > 255 ? 255 : (brightnessValue + temp[i+2])));
}
Stream sTemp = brightImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return brightImage;
}
else
{
return null;
}
}
[图像效果]
Fig.1原图 Fig.2效果图(brightnessValue =192)
[图像对比度指]
对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,差异范围越大代表对比越大,差异范围越小代表对比越小,对比度调整就是在保证平均亮度不变的情况下,增大或缩小这个差异,因此可以用如下公式表示:
其中,k属于[-1,1],average为图像平均亮度。
简化后如公式2-(7)所示:
由于average计算量大,比较耗时,而它对图像亮度调整的贡献比较小,因此,这里我们一般取average=127.5,以此来代替平均亮度。于是,对比度调整公式变化如下:
[函数代码]
/// <summary>
/// Contrast adjust process.
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="contrastValue">Contrast value, from -1 to 1.</param>
/// <returns></returns>
public static WriteableBitmap ContrastAdjustProcess(WriteableBitmap src,double contrastValue)////6 对比度调整
{
if(src!=null )
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap contrastImage = new WriteableBitmap(w,h);
byte[] temp = src.PixelBuffer.ToArray();
for (int i = 0; i < temp.Length; i += 4)
{
temp[i] = Convert.ToByte((((temp[i] - 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])) < 0 ? 0 : (((temp[i] - 127.5) * contrastValue + temp[i]) > 255 ? 255 : ((temp[i] - 127.5) * contrastValue + temp[i])));
temp[i + 1] = Convert.ToByte((((temp[i + 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])) < 0 ? 0 : (((temp[i + 1] - 127.5) * contrastValue + temp[i + 1]) > 255 ? 255 : ((temp[i + 1] - 127.5) * contrastValue + temp[i + 1])));
temp[i + 2] = Convert.ToByte((((temp[i + 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])) < 0 ? 0 : (((temp[i + 2] - 127.5) * contrastValue + temp[i + 2]) > 255 ? 255 : ((temp[i + 2] - 127.5) * contrastValue + temp[i + 2])));
}
Stream sTemp = contrastImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return contrastImage;
}
else
{
return null;
}
}
[图像效果]
Fig.1原图 Fig.2效果图(contrastValue =0.6)
demo 下载: http://www.zealfilter.com/forum.php?mod=viewthread&tid=42&extra=page%3D1
- 图像基本变换---图像亮度对比度调增算法
- 图像的亮度变换和对比度变换
- 图像亮度、对比度 算法--cg
- 图像亮度、对比度调节
- 矫正图像亮度、对比度
- OpenCV 基于RGB三原色的基本线性变换 改变图像颜色和亮度 对比度增强算法
- OpenCV图像亮度、对比度调节
- Photoshop图像亮度/对比度调整
- 图像亮度/对比度的调整
- Photoshop图像亮度/对比度调整
- 图像对比度、亮度值调整
- saturate_cast 图像对比度与亮度
- 调整图像亮度和对比度
- 图像对比度与亮度调整算法实现(Python&OpenCV)
- 图像对比度与亮度调整算法实现(Python&OpenCV)
- 调节亮度、对比度及gamma值实现彩色图像的亮度变换
- 图像亮度变换
- 图像亮度变换函数
- sqlalchemy中文问题解决方案
- 设计模式之单例模式
- 图像基本变换---图像线性变换
- oracle插入数字类型能用单引号括起来为什么
- 自定义View,圆形图框
- 图像基本变换---图像亮度对比度调增算法
- 一个超级好用的加载网络图片的轮播图(3行代码就可搞定)
- cocoscreator项目共享给其他成员的方法
- Web Service 的工作原理
- 排列组合算法(递归)1
- 51nod 1065 最小正子段和(最小正字段和)
- 释放linux内存命令
- [剑指offer]翻转单词顺序列
- webkit里居然有youtube的专用代码····