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下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/147213.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

NPDP产品经理知识(市场调研-文化,团队,领导力)

--- VOC --- 市场调研的关键步骤 1.> 定义问题 2.>定义结果的准确度 3.>收集数据 4.>分析和解读数据 5.>得出结论 6.>实施 --- 二级市场研究/一级市场研究 --- 定性 > 焦点小组 > 深度访谈 > 人种学(On-Site In-Home) > 客户…

Rust 使用Cargo

Rust 使用技巧 Rust 使用crates 假设你正在编写一个 Rust 程序&#xff0c;要使用一个名为 rand 的第三方库来生成随机数。首先&#xff0c;你需要在 Cargo.toml 文件中添加以下依赖项&#xff1a; toml [dependencies] rand "0.7.3" 然后运行 cargo build&…

C++ 学习系列 -- std::stack 与 std::queue

一 std::stack 与 std::queue 分别是什么&#xff1f; 两者均是 c 中的序列化容器&#xff0c;区别在于&#xff1a; std::stack 元素是先进后出 std::queue 元素是先进先出 二 std::stack 与 std::queue 原理 1 std:statck 2. std::queue 两者底层容器可以是 list 也可以…

数据结构与算法基础(青岛大学-王卓)(8)

哎呀呀&#xff0c;sorry艾瑞波地&#xff0c;这次真的断更一个月了&#xff0c;又发生了很多很多事情&#xff0c;秋风开始瑟瑟了&#xff0c;老父亲身体查出肿瘤了&#xff0c;有病请及时就医&#xff0c;愿每一个人都有一个健康的身体&#xff0c;God bless U and FAMILY. 直…

GraphQL全面深度讲解

目录 一、GraphQL 是什么 二、GraphQL 规范 数据模型 字段 参数 三、运行示例 四、优势和劣势 优势 劣势 一、GraphQL 是什么 GraphQL 是一种用于 API 的查询语言&#xff0c;也是一个基于服务端的运行引擎。 GraphQL 提供了一套完整的规范和描述用于查询 API&#xf…

Sketch mac98.3(ui设计矢量绘图)

Sketch Mac是一款矢量绘图软件&#xff0c;适用于UI设计、网页设计、图标制作等领域。以下是Sketch Mac的一些主要特点&#xff1a; 简单易用的界面设计&#xff1a;Sketch Mac的用户界面简洁明了&#xff0c;使得用户可以轻松上手操作&#xff0c;不需要复杂的学习过程。强大…

【QT】自定义组件ui类添加到主ui界面方法

1.添加自定义组件到项目中 add new选择如下 写好类方法&#xff0c;确定即可 2.将新创建的ui类加入到主ui界面 选中新创建ui类的父类空块&#xff0c;右键选择提升为 选择并添加新创建的类

【目标检测】——Gold-YOLO为啥能超过YOLOV8

华为 https://arxiv.org/pdf/2309.11331.pdf 文章的出发点&#xff1a;FPN中的信息传输问题 1. 简介 基于全局信息融合的概念&#xff0c;提出了一种新的收集和分发机制&#xff08;GD&#xff09;&#xff0c;用于在YOLO中进行有效的信息交换。通过全局融合多层特征并将全局信…

【DLoopDetector(C++)】DBow2词袋模型loop close学习

0.前言 最近读了两篇论文&#xff0c;论文作者开源了一种基于词袋模型DBoW2库的DLoopDetector算法&#xff0c;自己运行demo测试一下 对应论文介绍&#xff1a;Bags of Binary Words for Fast Place Recognition in Image Sequences 开源项目Github地址&#xff1a;https://gi…

ThreeJS - 封装一个GLB模型展示组件(TypeScript)

一、引言 最近基于Three.JS&#xff0c;使用class封装了一个GLB模型展示&#xff0c;支持TypeScript、支持不同框架使用&#xff0c;具有多种功能。 &#xff08;下图展示一些基础的功能&#xff0c;可以自行扩展&#xff0c;比如光源等&#xff09; 二、主要代码 本模块依赖…

vue3学习实战

vue3新增变化 diff算法变化 vue3的diff算法没有vue2的头尾、尾头之间的diff&#xff0c;对diff算法进行了优化&#xff0c;最长递归子序列。 ref VS reactive ref 支持所有的类型&#xff0c;reactive 支持引用类型&#xff0c;array object Map Setref取值、赋值&#xff…

2023-油猴(Tampermonkey)脚本推荐

2023-油猴&#xff08;Tampermonkey&#xff09;脚本推荐 知乎增强 链接 https://github.com/XIU2/UserScript https://greasyfork.org/zh-CN/scripts/419081 介绍 移除登录弹窗、屏蔽首页视频、默认收起回答、快捷收起回答/评论&#xff08;左键两侧&#xff09;、快捷回…

[CSCCTF 2019 Qual]FlaskLight 过滤 url_for globals 绕过globals过滤

目录 subprocess.Popen FILE warnings.catch_warnings site._Printer 这题很明显就是 SSTI了 源代码 我们试试看 {{7*7}} 然后我们就开始吧 原本我的想法是直接{{url_for.__globals__}} 但是回显是直接500 猜测过滤 我们正常来吧 {{"".__class__}} 查看当前…

Mysql 分布式序列算法

接上文 Mysql分库分表 1.分布式序列简介 在分布式系统下&#xff0c;怎么保证ID的生成满足以上需求&#xff1f; ShardingJDBC支持以上两种算法自动生成ID。这里&#xff0c;使用ShardingJDBC让主键ID以雪花算法进行生成&#xff0c;首先配置数据库&#xff0c;因为默认的注…

Linux shell编程学习笔记4:修改命令行提示符格式(内容和颜色)

一、命令行提示符格式内容因shell类型而异 Linux终端命令行提示符内容格式则因shell的类型而异&#xff0c;例如CoreLinux默认的shell是sh&#xff0c;其命令行提示符为黑底白字&#xff0c;内容为&#xff1a; tcbox:/$ 其中&#xff0c;tc为当前用户名&#xff0c;box为主机…

Spring Boot 技术架构图(InsCode AI 创作助手辅助)

Spring Boot 技术架构是一种用于构建现代应用程序的框架&#xff0c;它可以与各种前端、代理、网关、业务服务、中间件、存储、持续集成和容器服务集成在一起&#xff0c;以创建功能强大的应用程序。 源文件下载链接&#xff01;&#xff01;&#xff01;&#xff01;&#xff…

BASH shell脚本篇3——字符串处理

这篇文章介绍下BASH shell中的字符串处理的相关命令。之前有介绍过shell的其它命令&#xff0c;请参考&#xff1a; BASH shell脚本篇1——基本命令 BASH shell脚本篇2——条件命令 Bash字符串也是一种数据类型&#xff0c;它用于表示文本而不是数字&#xff0c;它是一组可能…

nginx-proxy反向代理流程

1.浏览器发送请求数据到nginx。 2.nginx先处理请求头&#xff0c;后处理请求体。 client_header_buffer_size #ginx可设置客户端上传header缓冲区大小 client_body_buffer_size #nginx可设置客户端上传数据缓冲区大小 client_body_t…

Tomcat Servlet

Tomcat & Servlet 一、What is “Tomcat”?二、 What is “Servlet”?1、HttpServlet2、HttpServletRequest3、HttpServletResponse 一、What is “Tomcat”? Tomcat 本质上是一个基于 TCP 协议的 HTTP 服务器。我们知道HTTP是一种应用层协议&#xff0c;是 HTTP 客户端…

(四)动态阈值分割

文章目录 一、基本概念二、实例解析 一、基本概念 基于局部阈值分割的dyn_threshold()算子&#xff0c;适用于一些无法用单一灰度进行分割的情况&#xff0c;如背景比较复杂&#xff0c;有的部分比前景目标亮&#xff0c;或者有的部分比前景目标暗&#xff1b;又比如前景目标包…