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);
- }
- }
-
- }
- }
下载
参考


评论记录:
回复评论: