C# 图片超分整理
公司业务上需要对图片显示优化。比如获取到本地应用ICON,8K分辨率下有些logo显示不清晰。
我们可以通过图片超分,提高显示质量。这里整理下最优的图片超分操作
这里用到的是腾讯Real-Esrgan,经过验证realesrgan-x4plus-anime对图片优化情况最好。
ESGRAN,是深度学习一类图像超分算法,通过深度学习方法提高低分辨率图像的分辨率和视觉质量。ESRGAN 是 SRGAN(Super-Resolution Generative Adversarial Network)的改进版本,结合了生成对抗网络(GAN)的强大能力和一些新的改进策略,如自注意力机制和感知损失等。图像超分也有一些简单/传统的超分技术,比如插值方法,通过计算邻近像素值加权平均。
图片超分处理
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 }
使用参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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-animevideov 3 , can be realesr-animevideov 3 | realesrgan-x 4 plus | realesrgan-x 4 plus-anime | realesrnet-x 4 plus)" -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工程内设置内容编译输出。
参考列表:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)