C# 图片超分整理

发布时间 2024-01-02 10:53:01作者: 唐宋元明清2188

公司业务上需要对图片显示优化。比如获取到本地应用ICON,8K分辨率下有些logo显示不清晰。

我们可以通过图片超分,提高显示质量。这里整理下最优的图片超分操作

这里用到的是腾讯Real-Esrgan,经过验证realesrgan-x4plus-anime对图片优化情况最好。

图片超分处理

 1     /// <summary>
 2     /// 转化
 3     /// </summary>
 4     /// <param name="sourceImage">源图片/图片文件夹</param>
 5     /// <param name="outputImage">输出图片/图片文件夹</param>
 6     public async Task ConvertAsync(string sourceImage, string outputImage)
 7     {
 8         //工作线程运行,避免UI卡住
 9         await Task.Run(() =>
10         {
11             string args = $"-n realesrgan-x4plus-anime -i \"{sourceImage}\" -o \"{outputImage}\"";
12             using var process = new Process();
13             process.StartInfo.FileName = _esrganExePath;
14             process.StartInfo.Arguments = args;
15 
16             process.StartInfo.UseShellExecute = false;
17             process.StartInfo.CreateNoWindow = true;
18             process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
19 
20             process.StartInfo.RedirectStandardOutput = true;
21             process.StartInfo.RedirectStandardError = true;
22             process.StartInfo.RedirectStandardInput = true;
23             process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
24             process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
25             process.EnableRaisingEvents = true;
26             process.Start();
27 
28             //开始异步读取输出
29             process.BeginOutputReadLine();
30             //异步读取错误
31             process.BeginErrorReadLine();
32             //设置回调函数
33             process.OutputDataReceived += Process_OutputDataReceived;
34             process.ErrorDataReceived += OnErrorDataReceived;
35             //等待程序执行完退出进程
36             process.WaitForExit();
37         });
38     }

使用参数:

Usage: realesrgan-ncnn-vulkan.exe -i infile -o outfile [options]...

  -h                   show this help"
  -i input-path        input image path (jpg/png/webp) or directory"
  -o output-path       output image path (jpg/png/webp) or directory"
  -s scale             upscale ratio (can be 2, 3, 4. default=4)"
  -t tile-size         tile size (>=32/0=auto, default=0) can be 0,0,0 for multi-gpu"
  -m model-path        folder path to the pre-trained models. default=models"
  -n model-name        model name (default=realesr-animevideov3, can be realesr-animevideov3 | realesrgan-x4plus | realesrgan-x4plus-anime | realesrnet-x4plus)"
  -g gpu-id            gpu device to use (default=auto) can be 0,1,2 for multi-gpu"
  -j load:proc:save    thread count for load/proc/save (default=1:2:2) can be 1:2,2,2:2 for multi-gpu"
  -x                   enable tta mode"
  -f format            output image format (jpg/png/webp, default=ext/png)"
  -v                   verbose output"

显示超分进度

超分很耗时,一张244K的图片超分后3.66M需要耗时143219ms。所以业务可能需要进度,下面是我整理的,直接复制就行:

 1     private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
 2     {
 3         //超分完成
 4         ProgressChanged?.Invoke(this, 100d);
 5     }
 6 
 7     private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
 8     {
 9         if (string.IsNullOrEmpty(e.Data))
10         {
11             return;
12         }
13         //获取超分进度值
14         var match = Regex.Match(e.Data, @"^[0-9.]+%$");
15         if (match.Success)
16         {
17             var progress = Convert.ToDouble(e.Data.Trim('%'));
18             ProgressChanged?.Invoke(this, progress);
19         }
20     }
21     /// <summary>
22     /// 转化进度(百分比 0-100)
23     /// </summary>
24     public event EventHandler<double> ProgressChanged;

超分工具资源文件,可以点击下载,放在VS工程内设置内容编译输出。

 

参考列表: