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#如何去掉文件夹或文件名非法字符

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

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

C#继承之里氏替换原则分析

《C#继承之里氏替换原则分析》:本文主要介绍C#继承之里氏替换原则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#里氏替换原则一.概念二.语法表现三.类型检查与转换总结C#里氏替换原则一.概念里氏替换原则是面向对象设计的基本原则之一:核心思想:所有引py

C#实现访问远程硬盘的图文教程

《C#实现访问远程硬盘的图文教程》在现实场景中,我们经常用到远程桌面功能,而在某些场景下,我们需要使用类似的远程硬盘功能,这样能非常方便地操作对方电脑磁盘的目录、以及传送文件,这次我们将给出一个完整的... 目录引言一. 远程硬盘功能展示二. 远程硬盘代码实现1. 底层业务通信实现2. UI 实现三. De

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序