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#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,