当前位置: 首页 > news >正文

WPF 通过 WriteableBitmap 实现 TAGC 低光增强效果算法

开始之前,必须感谢 只(挚)爱图像处理 - Imageshop 大佬的分享。本文将参阅 伊拉克团队的TAGC(低光增强效果)算法实现。 - Imageshop - 博客园 进行实现

原论文信息如下:

论文标题: Tuning adaptive gamma correction (TAGC) for enhancing images in low light

发表日期: 2025年07月

作者: Ghufran Alhamzawi, Alfoudi Ali Saeed, Suha Mohammed Hadi等

发表单位: University of Al-Qadisiyah, University of Information Technology and Communications等

原文链接: http://arxiv.org/pdf/2507.19574v1

实现效果如下图所示

这个过程无需玄学算法参与,无需对接任何 AI 相关的算法,仅仅只是非常简单的逐个像素进行数学计算。核心实现代码如下

/// <summary>
/// Tuning adaptive gamma correction (TAGC) 低光增强效果算法
/// </summary>
public class TuningAdaptiveGammaCorrectionAlgorithm
{public static unsafe void Enhancement(WriteableBitmap sourceBitmap, WriteableBitmap targetBitmap){if (sourceBitmap.Format != targetBitmap.Format){return;}if (sourceBitmap.Format != PixelFormats.Bgra32){return;}float inv255 = 1.0f / 255;sourceBitmap.Lock();targetBitmap.Lock();byte* src = (byte*)sourceBitmap.BackBuffer;byte* dest = (byte*)targetBitmap.BackBuffer;var height = sourceBitmap.PixelHeight;var width = sourceBitmap.PixelWidth;var stride = sourceBitmap.BackBufferStride;int channel = stride / width;for (int yIndex = 0; yIndex < height; yIndex++){byte* linePixelSource = src + yIndex * stride;byte* linePixelDest = dest + yIndex * stride;for (int xIndex = 0; xIndex < width; xIndex++){float blue = linePixelSource[0] * inv255;float green = linePixelSource[1] * inv255;float red = linePixelSource[2] * inv255;double l = 0.2126f * red + 0.7152 * green + 0.0722 * blue;float a = (blue + green + red) / 3;double gamma = 5.0f + (0.5f - l) * (1 - a) - 2 * l;double y = 2 / gamma;byte tb = ClampToByte((int)(Math.Pow(blue, y) * 255 + 0.4999999f));double y1 = 2 / gamma;byte tg = ClampToByte((int)(Math.Pow(green, y1) * 255 + 0.4999999f));double y2 = 2 / gamma;byte tr = ClampToByte((int)(Math.Pow(red, y2) * 255 + 0.4999999f));byte ta = 0xFF;linePixelDest[0] = tb;linePixelDest[1] = tg;linePixelDest[2] = tr;linePixelDest[3] = ta;linePixelSource += channel;linePixelDest += channel;}}targetBitmap.AddDirtyRect(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight));sourceBitmap.Unlock();targetBitmap.Unlock();}private static byte ClampToByte(int value){return (byte)Math.Clamp(value, 0, byte.MaxValue);}
}

具体计算的原理,如 https://www.cnblogs.com/Imageshop/p/19025343 博客内容所述:

使用 Tuning adaptive gamma correction (TAGC) 确实能够帮我将很多拍摄暗光照片进行低光增强。但对于一些屏幕截图的效果或其他非拍摄的图片的处理效果不佳

整个算法代码看起来也比较清新,代码量也少,能够实现如此好的效果,也是需要给大佬们点赞。如果大家对此算法效果感兴趣,欢迎按照本文末尾的方法拉取我的代码跑跑看效果,或直接从 Imageshop 大佬那下载他已经构建好的程序

细心的伙伴也许看到了,在本文的 Enhancement 方法里面需要判断图片的格式,但并非所有的图片都能遵循此格式。好在 WPF 里面可以非常方便地通过 FormatConvertedBitmap 进行转换,此转换过程都是利用 WIC 多媒体进行转换,性能非常高,损耗非常低。核心代码如下

        var formatConvertedBitmap = new FormatConvertedBitmap();formatConvertedBitmap.BeginInit();formatConvertedBitmap.Source = source;formatConvertedBitmap.DestinationFormat = PixelFormats.Bgra32;formatConvertedBitmap.EndInit();

使用的时候直接将 FormatConvertedBitmap 当成 BitmapSource 传递就可以了。完全的从传入的图片文件路径,经过 TAGC 算法,将输出的 WriteableBitmap 给到 DestImage 控件的 Source 的代码如下

    private void Enhancement(string filePath){if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)){return;}filePath = Path.GetFullPath(filePath);var source = new BitmapImage(new Uri(filePath));SourceImage.Source = source;var formatConvertedBitmap = new FormatConvertedBitmap();formatConvertedBitmap.BeginInit();formatConvertedBitmap.Source = source;formatConvertedBitmap.DestinationFormat = PixelFormats.Bgra32;formatConvertedBitmap.EndInit();var sourceBitmap = new WriteableBitmap(formatConvertedBitmap);var targetBitmap = new WriteableBitmap(sourceBitmap.PixelWidth, sourceBitmap.PixelHeight, sourceBitmap.DpiX,sourceBitmap.DpiY, sourceBitmap.Format, sourceBitmap.Palette);TuningAdaptiveGammaCorrectionAlgorithm.Enhancement(sourceBitmap, targetBitmap);var pngBitmapEncoder = new PngBitmapEncoder();pngBitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));using var stream = File.Create("1.png");pngBitmapEncoder.Save(stream);DestImage.Source = targetBitmap;}

以上的 DestImage 就是 Image 控件,界面代码定义如下

    <Grid RowDefinitions="Auto,*"><Grid><StackPanel Orientation="Horizontal"><Button x:Name="OpenImageFileButton" Margin="10,10,10,0" Content="打开图片文件" Click="OpenImageFileButton_OnClick"/></StackPanel></Grid><Grid Grid.Row="1" ColumnDefinitions="*,*"><Image x:Name="SourceImage" Stretch="Fill" Margin="10,10,10,10"/><Image x:Name="DestImage" Grid.Column="1" Stretch="Fill" Margin="10,10,10,10"/></Grid></Grid>

按钮点击打开文件,其方法实现如下

    private void OpenImageFileButton_OnClick(object sender, RoutedEventArgs e){var openFileDialog = new OpenFileDialog(){Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;",Multiselect = false,};openFileDialog.ShowDialog(this);var file = openFileDialog.FileName;Enhancement(file);}

本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 13152fb73b02bc3ec1acaca8c34ee44761fdff2c

以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 13152fb73b02bc3ec1acaca8c34ee44761fdff2c

获取代码之后,进入 WPFDemo/JoyojabujeaCocherallli 文件夹,即可获取到源代码

更多技术博客,请参阅 博客导航

http://www.wxhsa.cn/company.asp?id=6314

相关文章:

  • 最新学王点读笔破解教程2025
  • css-3
  • 基于 RQ-VAE 的商品语义 ID 构建及应用案例
  • U3D 动作游戏开发中数学知识的综合实践案例
  • 删除根目录前的准备
  • Player Mini MP3 模块播放音乐
  • Linux服务器部署FRP及配置Token
  • 最大子列和问题
  • RSA 共模攻击
  • 计组博文
  • week1task
  • 《原子习惯》-读书笔记3
  • Linux系统编程笔记总结
  • Java SE 25新增特性
  • linux系统编程09-进程间通信
  • 谈谈语法糖
  • 2025年,秋天与冬天(长期)
  • ssl rsa解密
  • linux系统编程05-标准IO1
  • linux系统编程07-文件IO\系统调用IO
  • linux系统编程06-标准IO2
  • linux系统编程08-高级IO
  • 第03周 预习、实验与作业:面向对象入门2与类的识别
  • 第8篇、Kafka 监控与调优实战指南
  • linux系统编程02-进程基本知识
  • linux系统编程03-并发:信号
  • linux系统编程04-并发:线程
  • 新手高效制作PPT的3个步骤:告别逻辑混乱,从构思到完成!
  • Avalonia:用 ReactiveUI 的方法绑定数据、事件和命令
  • 【pyQT 专栏】程序设置 windows 任务栏缩略图(.ico)教程