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#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

C#中的 StreamReader/StreamWriter 使用示例详解

《C#中的StreamReader/StreamWriter使用示例详解》在C#开发中,StreamReader和StreamWriter是处理文本文件的核心类,属于System.IO命名空间,本... 目录前言一、什么是 StreamReader 和 StreamWriter?1. 定义2. 特点3. 用

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.