C# DeOldify 黑白照片 老照片上色

2023-11-11 17:50

本文主要是介绍C# DeOldify 黑白照片 老照片上色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果

项目

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskBand;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;namespace DeOldify_黑白照片_老照片上色
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "Images (*.bmp; *.emf; *.exif; *.gif; *.ico; *.jpg; *.png; *.tiff; *.wmf)|*.bmp; *.emf; *.exif; *.gif; *.ico; *.jpg; *.png; *.tiff; *.wmf|All files|*.*";string image_path = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;string startupPath;string model;/// <summary>/// Input image./// </summary>private Bitmap __Input;/// <summary>/// Output image./// </summary>private Bitmap __Output;/// <summary>/// Normal output image./// </summary>private Bitmap __NormalOutput;/// <summary>/// Blurrified input image./// </summary>private Bitmap __BlurryInput;/// <summary>/// Blurrified output image./// </summary>private Bitmap __BlurryOutput;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;__Input = new Bitmap(image_path);//__BlurryInput = __Blurify(__Input);pictureBox1.Image = __Decolorize(__Input);textBox1.Text = "";pictureBox2.Image = null;}/// <summary>/// Converts the image to greyscale./// </summary>/// <param name="source">Input image.</param>/// <returns>Greyscale image.</returns>private static Bitmap __Decolorize(Bitmap source){var result = new Bitmap(source);for (int y = 0; y < result.Height; ++y){for (int x = 0; x < result.Width; ++x){var c = result.GetPixel(x, y);var l = (byte)((c.R + c.G + c.B) / 3);result.SetPixel(x, y, Color.FromArgb(c.A, l, l, l));}}return result;}/// <summary>/// Blurrifies the image./// </summary>/// <param name="source">Input image.</param>/// <returns>Blurrified image.</returns>private static Bitmap __Blurify(Bitmap source){var output = new Bitmap(source.Width, source.Height);for (int y = 0; y < output.Height; ++y){for (int x = 0; x < output.Width; ++x){var a = 0f;var r = 0f;var g = 0f;var b = 0f;for (int ky = 0; ky < 5; ++ky){var iy = y + ky - 2;if ((iy < 0) || (iy >= source.Height)){continue;}for (int kx = 0; kx < 5; ++kx){var ix = x + kx - 2;if ((ix < 0) || (ix >= source.Width)){continue;}var c = source.GetPixel(ix, iy);a += c.A;r += c.R;g += c.G;b += c.B;}}output.SetPixel(x, y, Color.FromArgb((byte)(a / 25), (byte)(r / 25), (byte)(g / 25), (byte)(b / 25)));}}return output;}private void button2_Click(object sender, EventArgs e){if (pictureBox1.Image == null){textBox1.Text = "请先选择图片";return;}button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "";Task task = new Task(() =>{dt1 = DateTime.Now;System.Threading.Thread.Sleep(2000);__Output = DeOldify.Colorize(__Input);//if (__Output.Height > __Output.Width)//{//    __NormalOutput = new Bitmap(__Output, (int)(256f / __Output.Height * __Output.Width), 256);//}//else//{//    __NormalOutput = new Bitmap(__Output, 256, (int)(256f / __Output.Width * __Output.Height));//}//__BlurryOutput = __Blurify(__NormalOutput);//__Output = __NormalOutput;pictureBox2.Image = __Output;dt2 = DateTime.Now;textBox1.Invoke(new Action(() =>{TimeSpan ts = dt2.Subtract(dt1);textBox1.Text = "耗时:" + ts.TotalSeconds + "s";}));button2.Invoke(new Action(() =>{button2.Enabled = true;}));//GC.Collect();});task.Start();}private void Form1_Load(object sender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;model = "Artistic.hmodel";//Artistic model with half-precision floating point weights. Less accurate than original float32 model, but requires 2 times less disk space.//Artistic.hmodel//Artistic model with single-precision floating point weights. More accurate than compressed float16 model.//Artistic.model//Stable model with single-precision floating point weights. Less accurate than original float32 model, but requires 2 times less disk space.//Stable.hmodel//Stable model with single-precision floating point weights. More accurate than compressed float16 model.//Stable.model";try{DeOldify.Initialize(model);textBox1.Text = "模型["+ model + "]初始化成功";DeOldify.Progress += (float Percent) =>{textBox1.Invoke(new Action(() =>{textBox1.Text = string.Format("完成进度:{0}%,请稍等……", Percent.ToString("f2"));}));};}catch (Exception ex){textBox1.Text = "模型初始化失败,异常信息:" + ex.Message;}}private void button3_Click(object sender, EventArgs e){if (pictureBox2.Image == null){return;}var SFD = new SaveFileDialog();SFD.Title = "保存";SFD.Filter = "Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";if (SFD.ShowDialog() == DialogResult.OK){switch (SFD.FilterIndex){case 1:{__Output.Save(SFD.FileName, ImageFormat.Bmp);break;}case 2:{__Output.Save(SFD.FileName, ImageFormat.Emf);break;}case 3:{__Output.Save(SFD.FileName, ImageFormat.Exif);break;}case 4:{__Output.Save(SFD.FileName, ImageFormat.Gif);break;}case 5:{__Output.Save(SFD.FileName, ImageFormat.Icon);break;}case 6:{__Output.Save(SFD.FileName, ImageFormat.Jpeg);break;}case 7:{__Output.Save(SFD.FileName, ImageFormat.Png);break;}case 8:{__Output.Save(SFD.FileName, ImageFormat.Tiff);break;}case 9:{__Output.Save(SFD.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:"+SFD.FileName);}}}
}

可执行程序exe下载

Demo下载

这篇关于C# DeOldify 黑白照片 老照片上色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/391806

相关文章

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时

C# $字符串插值的使用

《C#$字符串插值的使用》本文介绍了C#中的字符串插值功能,详细介绍了使用$符号的实现方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录$ 字符使用方式创建内插字符串包含不同的数据类型控制内插表达式的格式控制内插表达式的对齐方式内插表达式中使用转义序列内插表达式中使用

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字