C# OpenCvSharp DNN 黑白老照片上色

C# OpenCvSharp DNN 黑白老照片上色

目录

效果

项目

代码

下载 

参考


效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string startupPath;
        string image_path;

        Stopwatch stopwatch = new Stopwatch();

        Mat image;
        Mat result_image;

        /*
         // 初始化
        extern "C" _declspec(dllexport) void* __cdecl init(char* prototxt, char* caffe_model,int* res);

        // 上色
        extern "C" _declspec(dllexport) int __cdecl colorization(void* CvDNN_Net,Mat* image, Mat** returnValue);
         
         */

        const string DllName = "ColorizationSharp.dll";

        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
        public extern static IntPtr init(string prototxt, string caffe_model, ref int res);

        [DllImport(DllName, EntryPoint = "colorization", CallingConvention = CallingConvention.Cdecl)]
        public extern static int colorization(IntPtr CvDNN_Net, IntPtr image, out IntPtr returnValue);

        IntPtr CvDNN_Net;

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;

            image_path = "images/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);

            string prototxt = startupPath + "\\model\\colorization_deploy_v2.prototxt";
            string caffe_model = startupPath + "\\model\\colorization_release_v2.caffemodel";
            int res = 0;
            CvDNN_Net = init(prototxt, caffe_model, ref res);
            if (res != 0)
            {
                MessageBox.Show("初始化失败!");
            }
            else
            {
                button2.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            textBox1.Text = "";
            pictureBox2.Image = null;
            button2.Enabled = false;

            Application.DoEvents();

            stopwatch.Restart();

            IntPtr returnValue = IntPtr.Zero;
            int res = colorization(CvDNN_Net, image.Clone().CvPtr, out returnValue);
            if (res == 0)
            {
                result_image = new Mat(returnValue);

                double costTime = stopwatch.Elapsed.TotalMilliseconds;

                textBox1.Text = $"耗时:{costTime:F2}ms";
                pictureBox2.Image = result_image.ToBitmap();
            }
            else
            {
                MessageBox.Show("代码异常,上色失败!");
            }
            button2.Enabled = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            var sdf = new SaveFileDialog();
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }
                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }

    }
}

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace OpenCvSharp_Demo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string startupPath;string image_path;Stopwatch stopwatch = new Stopwatch();Mat image;Mat result_image;/*// 初始化extern "C" _declspec(dllexport) void* __cdecl init(char* prototxt, char* caffe_model,int* res);// 上色extern "C" _declspec(dllexport) int __cdecl colorization(void* CvDNN_Net,Mat* image, Mat** returnValue);*/const string DllName = "ColorizationSharp.dll";[DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]public extern static IntPtr init(string prototxt, string caffe_model, ref int res);[DllImport(DllName, EntryPoint = "colorization", CallingConvention = CallingConvention.Cdecl)]public extern static int colorization(IntPtr CvDNN_Net, IntPtr image, out IntPtr returnValue);IntPtr CvDNN_Net;private void Form1_Load(object sender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;image_path = "images/1.jpg";pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);string prototxt = startupPath + "\\model\\colorization_deploy_v2.prototxt";string caffe_model = startupPath + "\\model\\colorization_release_v2.caffemodel";int res = 0;CvDNN_Net = init(prototxt, caffe_model, ref res);if (res != 0){MessageBox.Show("初始化失败!");}else{button2.Enabled = true;}}private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "";pictureBox2.Image = null;button2.Enabled = false;Application.DoEvents();stopwatch.Restart();IntPtr returnValue = IntPtr.Zero;int res = colorization(CvDNN_Net, image.Clone().CvPtr, out returnValue);if (res == 0){result_image = new Mat(returnValue);double costTime = stopwatch.Elapsed.TotalMilliseconds;textBox1.Text = $"耗时:{costTime:F2}ms";pictureBox2.Image = result_image.ToBitmap();}else{MessageBox.Show("代码异常,上色失败!");}button2.Enabled = true;}private void button3_Click(object sender, EventArgs e){if (pictureBox2.Image == null){return;}Bitmap output = new Bitmap(pictureBox2.Image);var sdf = new SaveFileDialog();sdf.Title = "保存";sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";if (sdf.ShowDialog() == DialogResult.OK){switch (sdf.FilterIndex){case 1:{output.Save(sdf.FileName, ImageFormat.Jpeg);break;}case 2:{output.Save(sdf.FileName, ImageFormat.Png);break;}case 3:{output.Save(sdf.FileName, ImageFormat.Bmp);break;}case 4:{output.Save(sdf.FileName, ImageFormat.Emf);break;}case 5:{output.Save(sdf.FileName, ImageFormat.Exif);break;}case 6:{output.Save(sdf.FileName, ImageFormat.Gif);break;}case 7:{output.Save(sdf.FileName, ImageFormat.Icon);break;}case 8:{output.Save(sdf.FileName, ImageFormat.Tiff);break;}case 9:{output.Save(sdf.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}}
}

下载 

源码下载

参考

https://github.com/richzhang/colorization/tree/caffe

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你有没有调用过第三方接口?碰到过哪些坑?

在我们的业务开发中,调用第三方接口已经成为常态,比如对接一些ERP系统、WMS系统、一些数据服务系统等,它极大地扩展了我们应用的功能和服务范围。然而,实际对接过程中,我们往往会在这一环节遇到各种意想不到的问题&…

项目实施方案:多点异地机动车典型系统试验状态可视监控系统

目录 一、需求分析 1.1项目背景 1.2项目概述 二、系统优势 2.1兼容性能力强 2.2接入协议多样 2.3并发能力强 2.3.1 单平台参数 2.3.2 多平台性能参数 2.4 系统稳定性 三、建设目标 3.1安全性 3.2可扩展性 3.3易用性 3.4兼容性 3.5 响应能力 四、系统整体解决方…

单区域OSPF实验

实验目的: 理解OSPF的基本概念。掌握单曲于OSPF的配置掌握OSPF邻居状态的解读掌握通过Cost控制OSPF选路的方法掌握OSPF认证的配置方法 一、基础配置: 搭建实验拓扑图; 配置路由器接口的IP地址以及配置环回地址待后续使用 (1&a…

文献速递:深度学习医学影像心脏疾病检测与诊断--基于深度学习的低剂量SPECT心肌灌注图像去噪:定量评估与临床表现

Title 题目 Deep learning–based denoising of low‑dose SPECT myocardialperfusion images: quantitative assessment and clinical performance 基于深度学习的低剂量SPECT心肌灌注图像去噪:定量评估与临床表现 01 文献速递介绍 单光子发射计算机断层扫描&a…

Spring的IOC和AOP机制?

我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂。 主要用到的设计模式有工厂模式和代理模式。 IOC就是典型的工厂模式,通过sessionfactory去注入…

leetcode刷题指南

本文我将分享给大家一套我自己使用良久并觉得非常高效的 学习论,它可以运用到 Leetcode 上的刷题,也可以 generalize 到生活中涉及到学习以及记忆的方方面面。当然,本文将以 Leetcode 刷题为 case study 去进行讲解。 更具体一点, 我会教大家…

JavaScript的综合案例

案例要求&#xff1a; 实现一个表单验证 1.当输入框失去焦点时&#xff0c;验证输入的内容是否符合要求 2.当点击注册按钮时&#xff0c;判断所有输入框的内容是否都符合要求&#xff0c;如果不符合要求阻止表单提交 简单的页面实现 <!DOCTYPE html> <html lang&…

在阿里云服务器上安装MySQL

目录 一、先卸载不需要的环境 1.关闭MySQL服务 2.查看安装包以及卸载安装包 3.依次卸载所有包 4. 获取mysql官⽅yum源 二、安装&#xff08;密钥过期解决方法&#xff09; 三、启动并进入 关于MySQL MySQL是一个广泛使用的开源关系型数据库管理系统&#xff08;RDBMS&…

Linux中如何配置虚拟机网络(NAT方法)

首先我们要在Linux中找到配置文件的路径/etc/sysconfig/network-scripts/&#xff0c;然后找到配置文件的名称ifcfg-xxx&#xff08;如&#xff1a;ifcfg-ens33&#xff09;&#xff0c;然后打开这个文件内 容如下&#xff1a; TYPEEthernet # 指定网卡类型是以太网 BOOTPROT…

【平衡二叉树】AVL树(双旋)

&#x1f389;博主首页&#xff1a; 有趣的中国人 &#x1f389;专栏首页&#xff1a; C进阶 &#x1f389;其它专栏&#xff1a; C初阶 | Linux | 初阶数据结构 小伙伴们大家好&#xff0c;本片文章将会讲解AVL树的左双选和右双旋的相关内容。 如果看到最后您觉得这篇文章写…

C++基础语法之数组

一、一维数组 在C中&#xff0c;一维数组是一系列具有相同数据类型的元素的集合。它们在内存中是连续存储的&#xff0c;可以通过索引访问每个元素。 一维数组的声明形式如下&#xff1a; 数据类型 数组名[常量表达式] 例如&#xff1a; // 声明一个能存储10个整数的数组 in…

【AI学习】对指令微调(instruction tuning)的理解

前面对微调&#xff08;Fine-tuning&#xff09;的学习中&#xff0c;提到指令微调。当时&#xff0c;不清楚何为指令微调&#xff0c;也一直没来得及仔细学习。 什么是指令微调&#xff1f;LLM经过预训练后&#xff0c;通过指令微调提升模型的指令遵循能力。所谓指令&#xf…

车载GPT爆红前夜:一场巨头竞逐的游戏

在基于GPT-3.5的ChatGPT问世之前&#xff0c;OpenAI作为深度学习领域并不大为人所看好的技术分支玩家&#xff0c;已经在GPT这个赛道默默耕耘了七八年的时间。 好几年的时间里&#xff0c;GPT始终没有跨越从“不能用”到“能用”的奇点。转折点发生在2020年6月份发布的GPT-3&a…

【STM32】状态机实现定时器按键消抖,处理单击、双击、三击、长按事件

目录 一、简单介绍 二、模块与接线 三、cubemx配置 四、驱动编写 状态图 按键类型定义 参数初始化/复位 按键扫描 串口重定向 主函数 五、效果展示 六、驱动附录 key.c key.h 一、简单介绍 众所周知&#xff0c;普通的机械按键会产生抖动&#xff0c;可以采取硬件…

注意力机制篇 | YOLOv8改进之在C2f模块引入反向残差注意力模块iRMB | CVPR 2023

前言:Hello大家好,我是小哥谈。反向残差注意力模块iRMB是一种用于图像分类和目标检测的深度学习模块。它结合了反向残差和注意力机制的优点,能够有效地提高模型的性能。在iRMB中,反向残差指的是将原始的残差块进行反转,即将卷积操作和批量归一化操作放在了后面。这样做的好…

软件工程期末复习(6)需求分析的任务

需求分析 需求分析的任务 “建造一个软件系统的最困难的部分是决定要建造什么……没有别的工作在做错时会如此影响最终系统&#xff0c;没有别的工作比以后矫正更困难。” —— Fred Brooks 需求难以建立的原因&#x…

矩阵相关运算1

矩阵运算是线性代数中的一个核心部分&#xff0c;它包含了许多不同类型的操作&#xff0c;可以应用于各种科学和工程问题中。 矩阵加法和减法 矩阵加法和减法需要两个矩阵具有相同的维度。操作是逐元素进行的&#xff1a; CAB or CA−B其中 A,B 和 C 是矩阵&#xff0c;且 C…

7nm项目之模块实现——02 Placeopt分析

一、Log需要看什么 1.log最后的error 注意&#xff1a;warnning暂时可以不用过于关注&#xff0c;如果特别的warning出现问题&#xff0c;在其他方面也会体现 2.run time 在大型项目实际开发中&#xff0c;周期一般较长&#xff0c;可能几天过这几周&#xff0c;所以这就需要…

探讨 cs2019 c++ 的STL 库中的模板 conjunction 与 disjunction

&#xff08;1&#xff09;在 STL 库源码中这俩模板经常出现&#xff0c;用来给源码编译中的条件选择&#xff0c;模板的版本选择等提供依据。先给出其定义&#xff1a; 以及&#xff1a; 可以得出结论&#xff1a; conj 是为了查找逻辑布尔型模板参数中的第一个 false &#x…

vs2019中__cplusplus一直显示199711

vs2019中__cplusplus一直显示199711&#xff0c;如何修改&#xff1f; 打开属性->C/C->命令行&#xff0c;其他选项&#xff0c;输入&#xff1a;/Zc:__cplusplus