C# OpenCvSharp DNN 黑白老照片上色

server/2024/10/21 15:31:32/

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.ppmy.cn/server/41638.html

相关文章

paddle ocr 版面分析

教程 https://github.com/PaddlePaddle/PaddleOCR/blob/a4b7d3ba4a8333a23bab1fc1472aa18deec211d1/ppstructure/layout/README_ch.md 额外的模型,但是yolov2的模型缺少yml配置文件,找不到 https://github.com/PaddlePaddle/PaddleOCR/blob/main/ppstruc…

83.网络游戏逆向分析与漏洞攻防-移动系统分析-游戏瞬移漏洞的测试与分析

免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动! 如果看不懂、不知道现在做的什么,那就跟着做完看效果,代码看不懂是正常的,只要会抄就行,抄着抄着就能懂了 内容…

Oracle 修改数据库的字符集

Oracle 修改数据库的字符集 alter system enable restricted session; alter database "cata" character set ZHS16CGB231280; alter database "cata" national character set ZHS16CGB231280; alter system enable restricted session; alter database…

图的深度优先遍历

way:栈,map(或set,只是我想用map)记录是否访问过,放入时记录为已访问,打印,邻接的没访问过先入cur,再入邻接的节点,放入一个邻接的节点后及时break去下一个深…

数据库系统概论第四章 数据库安全性

数据库的特点之一是由数据库管理系统提供统一的数据保护功能来保证数据的安全可靠和正确有效。数据库的数据保护主要包括数据库的安全性和完整性。 文章目录 4.1 数据库安全性概述 4.1.1 数据库的不安全因素 4.1.2 安全标准简介 4.2 数据库安全性控制 事前控制-事中控制-事…

VMware虚拟机提示内存不足

VMware虚拟机,k8s集群搭建内存不足的问题 疑问:我的电脑是8G8G双通道的内存,当我在搭建k8s集群时给master-2G内存,node1-3G内存,node2-3G内存; 当依次打开虚拟机到node2时VM提示“物理内存不足,…

Python图形界面(GUI)Tkinter笔记(一):根窗口的创建

Tkinter库是Python的内置关于图形界面编程(GUI全称为Graphical User Interface,中文意思为“图形用户界面”)的一个库。直接导入Tkinter使用即可。 其余笔记:【Python图形界面(GUI)Tkinter笔记(…

c#多态性的应用

设计一个电脑游戏,游戏中有猪、牛、兔子、青蛙、鸭子等动物,这些动 物都继承于Vertebrata 类(脊椎动物类),Vertebrata类有一个抽象方法Display(),每个动 物都从Vertebrata 类那里继承并重写了Display()方法…