找回密码
 立即注册
首页 业界区 安全 WPF 通过 WriteableBitmap 实现 TAGC 低光增强效果算法 ...

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

移国拱 2025-10-1 17:45:08
开始之前,必须感谢 只(挚)爱图像处理 - 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
实现效果如下图所示
1.png

2.png

这个过程无需玄学算法参与,无需对接任何 AI 相关的算法,仅仅只是非常简单的逐个像素进行数学计算。核心实现代码如下
  1. /// <summary>
  2. /// Tuning adaptive gamma correction (TAGC) 低光增强效果算法
  3. /// </summary>
  4. public class TuningAdaptiveGammaCorrectionAlgorithm
  5. {
  6.     public static unsafe void Enhancement(WriteableBitmap sourceBitmap, WriteableBitmap targetBitmap)
  7.     {
  8.         if (sourceBitmap.Format != targetBitmap.Format)
  9.         {
  10.             return;
  11.         }
  12.         if (sourceBitmap.Format != PixelFormats.Bgra32)
  13.         {
  14.             return;
  15.         }
  16.         float inv255 = 1.0f / 255;
  17.         sourceBitmap.Lock();
  18.         targetBitmap.Lock();
  19.         byte* src = (byte*)sourceBitmap.BackBuffer;
  20.         byte* dest = (byte*)targetBitmap.BackBuffer;
  21.         var height = sourceBitmap.PixelHeight;
  22.         var width = sourceBitmap.PixelWidth;
  23.         var stride = sourceBitmap.BackBufferStride;
  24.         int channel = stride / width;
  25.         for (int yIndex = 0; yIndex < height; yIndex++)
  26.         {
  27.             byte* linePixelSource = src + yIndex * stride;
  28.             byte* linePixelDest = dest + yIndex * stride;
  29.             for (int xIndex = 0; xIndex < width; xIndex++)
  30.             {
  31.                 float blue = linePixelSource[0] * inv255;
  32.                 float green = linePixelSource[1] * inv255;
  33.                 float red = linePixelSource[2] * inv255;
  34.                 double l = 0.2126f * red + 0.7152 * green + 0.0722 * blue;
  35.                 float a = (blue + green + red) / 3;
  36.                 double gamma = 5.0f + (0.5f - l) * (1 - a) - 2 * l;
  37.                 double y = 2 / gamma;
  38.                 byte tb = ClampToByte((int)(Math.Pow(blue, y) * 255 + 0.4999999f));
  39.                 double y1 = 2 / gamma;
  40.                 byte tg = ClampToByte((int)(Math.Pow(green, y1) * 255 + 0.4999999f));
  41.                 double y2 = 2 / gamma;
  42.                 byte tr = ClampToByte((int)(Math.Pow(red, y2) * 255 + 0.4999999f));
  43.                 byte ta = 0xFF;
  44.                 linePixelDest[0] = tb;
  45.                 linePixelDest[1] = tg;
  46.                 linePixelDest[2] = tr;
  47.                 linePixelDest[3] = ta;
  48.                 linePixelSource += channel;
  49.                 linePixelDest += channel;
  50.             }
  51.         }
  52.         targetBitmap.AddDirtyRect(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight));
  53.         sourceBitmap.Unlock();
  54.         targetBitmap.Unlock();
  55.     }
  56.     private static byte ClampToByte(int value)
  57.     {
  58.         return (byte)Math.Clamp(value, 0, byte.MaxValue);
  59.     }
  60. }
复制代码
具体计算的原理,如 https://www.cnblogs.com/Imageshop/p/19025343 博客内容所述:
3.png

使用 Tuning adaptive gamma correction (TAGC) 确实能够帮我将很多拍摄暗光照片进行低光增强。但对于一些屏幕截图的效果或其他非拍摄的图片的处理效果不佳
整个算法代码看起来也比较清新,代码量也少,能够实现如此好的效果,也是需要给大佬们点赞。如果大家对此算法效果感兴趣,欢迎按照本文末尾的方法拉取我的代码跑跑看效果,或直接从 Imageshop 大佬那下载他已经构建好的程序
细心的伙伴也许看到了,在本文的 Enhancement 方法里面需要判断图片的格式,但并非所有的图片都能遵循此格式。好在 WPF 里面可以非常方便地通过 FormatConvertedBitmap 进行转换,此转换过程都是利用 WIC 多媒体进行转换,性能非常高,损耗非常低。核心代码如下
  1.         var formatConvertedBitmap = new FormatConvertedBitmap();
  2.         formatConvertedBitmap.BeginInit();
  3.         formatConvertedBitmap.Source = source;
  4.         formatConvertedBitmap.DestinationFormat = PixelFormats.Bgra32;
  5.         formatConvertedBitmap.EndInit();
复制代码
使用的时候直接将 FormatConvertedBitmap 当成 BitmapSource 传递就可以了。完全的从传入的图片文件路径,经过 TAGC 算法,将输出的 WriteableBitmap 给到 DestImage 控件的 Source 的代码如下
  1.     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();
  2.         formatConvertedBitmap.BeginInit();
  3.         formatConvertedBitmap.Source = source;
  4.         formatConvertedBitmap.DestinationFormat = PixelFormats.Bgra32;
  5.         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 控件,界面代码定义如下
  1.     <Grid RowDefinitions="Auto,*">
  2.         <Grid>
  3.             <StackPanel Orientation="Horizontal">
  4.                 <Button x:Name="OpenImageFileButton" Margin="10,10,10,0" Content="打开图片文件" Click="OpenImageFileButton_OnClick"/>
  5.             </StackPanel>
  6.         </Grid>
  7.         <Grid Grid.Row="1" ColumnDefinitions="*,*">
  8.             <Image x:Name="SourceImage" Stretch="Fill" Margin="10,10,10,10"/>
  9.             <Image x:Name="DestImage" Grid.Column="1" Stretch="Fill" Margin="10,10,10,10"/>
  10.         </Grid>
  11.     </Grid>
复制代码
按钮点击打开文件,其方法实现如下
  1.     private void OpenImageFileButton_OnClick(object sender, RoutedEventArgs e)
  2.     {
  3.         var openFileDialog = new OpenFileDialog()
  4.         {
  5.             Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;",
  6.             Multiselect = false,
  7.         };
  8.         openFileDialog.ShowDialog(this);
  9.         var file = openFileDialog.FileName;
  10.         Enhancement(file);
  11.     }
复制代码
本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快
先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
  1. git init
  2. git remote add origin https://gitee.com/lindexi/lindexi_gd.git
  3. git pull origin 13152fb73b02bc3ec1acaca8c34ee44761fdff2c
复制代码
以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码
  1. git remote remove origin
  2. git remote add origin https://github.com/lindexi/lindexi_gd.git
  3. git pull origin 13152fb73b02bc3ec1acaca8c34ee44761fdff2c
复制代码
获取代码之后,进入 WPFDemo/JoyojabujeaCocherallli 文件夹,即可获取到源代码
更多技术博客,请参阅 博客导航

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册