首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenCvSharp DNN 黑白老照片上色

  • 25-02-19 03:20
  • 3368
  • 7304
blog.csdn.net

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);
            }
        }

    }
}

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Runtime.InteropServices;
  8. using System.Windows.Forms;
  9. namespace OpenCvSharp_Demo
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  18. string startupPath;
  19. string image_path;
  20. Stopwatch stopwatch = new Stopwatch();
  21. Mat image;
  22. Mat result_image;
  23. /*
  24. // 初始化
  25. extern "C" _declspec(dllexport) void* __cdecl init(char* prototxt, char* caffe_model,int* res);
  26. // 上色
  27. extern "C" _declspec(dllexport) int __cdecl colorization(void* CvDNN_Net,Mat* image, Mat** returnValue);
  28. */
  29. const string DllName = "ColorizationSharp.dll";
  30. [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
  31. public extern static IntPtr init(string prototxt, string caffe_model, ref int res);
  32. [DllImport(DllName, EntryPoint = "colorization", CallingConvention = CallingConvention.Cdecl)]
  33. public extern static int colorization(IntPtr CvDNN_Net, IntPtr image, out IntPtr returnValue);
  34. IntPtr CvDNN_Net;
  35. private void Form1_Load(object sender, EventArgs e)
  36. {
  37. startupPath = System.Windows.Forms.Application.StartupPath;
  38. image_path = "images/1.jpg";
  39. pictureBox1.Image = new Bitmap(image_path);
  40. image = new Mat(image_path);
  41. string prototxt = startupPath + "\\model\\colorization_deploy_v2.prototxt";
  42. string caffe_model = startupPath + "\\model\\colorization_release_v2.caffemodel";
  43. int res = 0;
  44. CvDNN_Net = init(prototxt, caffe_model, ref res);
  45. if (res != 0)
  46. {
  47. MessageBox.Show("初始化失败!");
  48. }
  49. else
  50. {
  51. button2.Enabled = true;
  52. }
  53. }
  54. private void button1_Click(object sender, EventArgs e)
  55. {
  56. OpenFileDialog ofd = new OpenFileDialog();
  57. ofd.Filter = fileFilter;
  58. if (ofd.ShowDialog() != DialogResult.OK) return;
  59. pictureBox1.Image = null;
  60. pictureBox2.Image = null;
  61. textBox1.Text = "";
  62. image_path = ofd.FileName;
  63. pictureBox1.Image = new Bitmap(image_path);
  64. image = new Mat(image_path);
  65. }
  66. private void button2_Click(object sender, EventArgs e)
  67. {
  68. if (image_path == "")
  69. {
  70. return;
  71. }
  72. textBox1.Text = "";
  73. pictureBox2.Image = null;
  74. button2.Enabled = false;
  75. Application.DoEvents();
  76. stopwatch.Restart();
  77. IntPtr returnValue = IntPtr.Zero;
  78. int res = colorization(CvDNN_Net, image.Clone().CvPtr, out returnValue);
  79. if (res == 0)
  80. {
  81. result_image = new Mat(returnValue);
  82. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  83. textBox1.Text = $"耗时:{costTime:F2}ms";
  84. pictureBox2.Image = result_image.ToBitmap();
  85. }
  86. else
  87. {
  88. MessageBox.Show("代码异常,上色失败!");
  89. }
  90. button2.Enabled = true;
  91. }
  92. private void button3_Click(object sender, EventArgs e)
  93. {
  94. if (pictureBox2.Image == null)
  95. {
  96. return;
  97. }
  98. Bitmap output = new Bitmap(pictureBox2.Image);
  99. var sdf = new SaveFileDialog();
  100. sdf.Title = "保存";
  101. 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";
  102. if (sdf.ShowDialog() == DialogResult.OK)
  103. {
  104. switch (sdf.FilterIndex)
  105. {
  106. case 1:
  107. {
  108. output.Save(sdf.FileName, ImageFormat.Jpeg);
  109. break;
  110. }
  111. case 2:
  112. {
  113. output.Save(sdf.FileName, ImageFormat.Png);
  114. break;
  115. }
  116. case 3:
  117. {
  118. output.Save(sdf.FileName, ImageFormat.Bmp);
  119. break;
  120. }
  121. case 4:
  122. {
  123. output.Save(sdf.FileName, ImageFormat.Emf);
  124. break;
  125. }
  126. case 5:
  127. {
  128. output.Save(sdf.FileName, ImageFormat.Exif);
  129. break;
  130. }
  131. case 6:
  132. {
  133. output.Save(sdf.FileName, ImageFormat.Gif);
  134. break;
  135. }
  136. case 7:
  137. {
  138. output.Save(sdf.FileName, ImageFormat.Icon);
  139. break;
  140. }
  141. case 8:
  142. {
  143. output.Save(sdf.FileName, ImageFormat.Tiff);
  144. break;
  145. }
  146. case 9:
  147. {
  148. output.Save(sdf.FileName, ImageFormat.Wmf);
  149. break;
  150. }
  151. }
  152. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  153. }
  154. }
  155. }
  156. }

下载 

源码下载

参考

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

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/138863488"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top