C# OpenCvSharp 部署文檔矯正,包括文檔扭曲/模糊/陰影等情況

目錄

說明

效果

模型

項目

代碼

下載

參考


C# OpenCvSharp 部署文檔矯正,包括文檔扭曲/模糊/陰影等情況

說明

地址:https://github.com/RapidAI/RapidUnDistort

修正文檔扭曲/模糊/陰影等情況,使用onnx模型簡單輕量部署,未來持續跟進最新最好的文檔矯正方案和模型,Correct document distortion using a lightweight ONNX model for easy deployment. We will continue to follow and integrate the latest and best document correction solutions and models in the future.?

效果

模型

drnet.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 6, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

gcnet.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

nafdpm.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, -1, -1, -1]
---------------------------------------------------------------

unetcnn.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, 1, -1, -1]
---------------------------------------------------------------

uvdoc.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, 2, -1, -1]
name:546
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

項目

代碼

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

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

? ? ? ? Stopwatch stopwatch = new Stopwatch();
? ? ? ? Mat image;
? ? ? ? Mat out_img;
? ? ? ? string image_path;
? ? ? ? string startupPath;
? ? ? ? string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
? ? ? ? const string DllName = "DocumentUndistortSharp.dll";
? ? ? ? IntPtr engine;

? ? ? ? /*
? ? ? ? ?//初始化
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?init(void** engine, char* binary_model_path, char* unblur_model_path, char* unshadow_model_gcnet_path, char* unshadow_model_drnet_path, char* unwrap_model_path, char* msg);

? ? ? ? //binary
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?binary(void* engine, Mat* srcimg, char* msg, Mat* out_img);

? ? ? ? //unblur
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?unblur(void* engine, Mat* srcimg, char* msg, Mat* out_img);

? ? ? ? //unshadow
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?unshadow(void* engine, Mat* srcimg, char* msg, Mat* out_img);

? ? ? ? //unwrap
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?unwrap(void* engine, Mat* srcimg, char* msg, Mat* out_img);

? ? ? ? //openCvBilateral
? ? ? ? extern "C" _declspec(dllexport) int __cdecl ?openCvBilateral(Mat* srcimg, char* msg, Mat* out_img);

? ? ? ? //釋放
? ? ? ? extern "C" _declspec(dllexport) void __cdecl destroy(void* engine);
? ? ? ? ?*/


? ? ? ? [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int init(ref IntPtr engine, string binary_model_path, string unblur_model_path, string unshadow_model_gcnet_path, string unshadow_model_drnet_path, string unwrap_model_path, StringBuilder msg);

? ? ? ? [DllImport(DllName, EntryPoint = "binary", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int binary(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);

? ? ? ? [DllImport(DllName, EntryPoint = "unblur", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int unblur(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);

? ? ? ? [DllImport(DllName, EntryPoint = "unshadow", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int unshadow(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);

? ? ? ? [DllImport(DllName, EntryPoint = "unwrap", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int unwrap(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);

? ? ? ? [DllImport(DllName, EntryPoint = "openCvBilateral", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int openCvBilateral(IntPtr srcimg, StringBuilder msg, IntPtr out_img);

? ? ? ? [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]
? ? ? ? internal extern static int destroy(IntPtr engine);

? ? ? ? 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 Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? startupPath = Application.StartupPath;

? ? ? ? ? ? string binary_model_path = startupPath + "\\model\\unetcnn.onnx";
? ? ? ? ? ? string unblur_model_path = startupPath + "\\model\\nafdpm.onnx";
? ? ? ? ? ? string unshadow_model_gcnet_path = startupPath + "\\model\\gcnet.onnx";
? ? ? ? ? ? string unshadow_model_drnet_path = startupPath + "\\model\\drnet.onnx";
? ? ? ? ? ? string unwrap_model_path = startupPath + "\\model\\uvdoc.onnx";

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);

? ? ? ? ? ? int res = init(ref engine, binary_model_path, unblur_model_path, unshadow_model_gcnet_path, unshadow_model_drnet_path, unwrap_model_path, msg);
? ? ? ? ? ? if (res == -1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show(msg.ToString());
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(msg.ToString());
? ? ? ? ? ? }
? ? ? ? ? ? image_path = startupPath + "\\test_img\\2.jpg";
? ? ? ? ? ? pictureBox1.Image = new Bitmap(image_path);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? }

? ? ? ? private void Form1_FormClosed(object sender, FormClosedEventArgs e)
? ? ? ? {
? ? ? ? ? ? destroy(engine);
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// unwrap
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (image_path == "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? textBox1.Text = "執行中……";
? ? ? ? ? ? Application.DoEvents();

? ? ? ? ? ? if (image != null) image.Dispose();
? ? ? ? ? ? if (out_img != null) out_img.Dispose();
? ? ? ? ? ? if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? ? ? out_img = new Mat();

? ? ? ? ? ? stopwatch.Restart();

? ? ? ? ? ? int res = unwrap(engine, image.CvPtr, msg, out_img.CvPtr);
? ? ? ? ? ? if (res == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? ? ? double costTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? ? ? pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());
? ? ? ? ? ? ? ? textBox1.Text = $"耗時:{costTime:F2}ms";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "失敗," + msg.ToString();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// openCvBilateral
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button7_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (image_path == "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? textBox1.Text = "執行中……";
? ? ? ? ? ? Application.DoEvents();

? ? ? ? ? ? if (image != null) image.Dispose();
? ? ? ? ? ? if (out_img != null) out_img.Dispose();
? ? ? ? ? ? if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? ? ? out_img = new Mat();

? ? ? ? ? ? stopwatch.Restart();

? ? ? ? ? ? int res = openCvBilateral(image.CvPtr, msg, out_img.CvPtr);
? ? ? ? ? ? if (res == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? ? ? double costTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? ? ? pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());
? ? ? ? ? ? ? ? textBox1.Text = $"耗時:{costTime:F2}ms";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "失敗," + msg.ToString();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// unshadow
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button6_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (image_path == "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? textBox1.Text = "執行中……";
? ? ? ? ? ? Application.DoEvents();

? ? ? ? ? ? if (image != null) image.Dispose();
? ? ? ? ? ? if (out_img != null) out_img.Dispose();
? ? ? ? ? ? if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? ? ? out_img = new Mat();

? ? ? ? ? ? stopwatch.Restart();

? ? ? ? ? ? int res = unshadow(engine, image.CvPtr, msg, out_img.CvPtr);
? ? ? ? ? ? if (res == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? ? ? double costTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? ? ? pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());
? ? ? ? ? ? ? ? textBox1.Text = $"耗時:{costTime:F2}ms";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "失敗," + msg.ToString();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// unblur
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button5_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (image_path == "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? textBox1.Text = "執行中……";
? ? ? ? ? ? Application.DoEvents();

? ? ? ? ? ? if (image != null) image.Dispose();
? ? ? ? ? ? if (out_img != null) out_img.Dispose();
? ? ? ? ? ? if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? ? ? out_img = new Mat();

? ? ? ? ? ? stopwatch.Restart();

? ? ? ? ? ? int res = unblur(engine, image.CvPtr, msg, out_img.CvPtr);
? ? ? ? ? ? if (res == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? ? ? double costTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? ? ? pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());
? ? ? ? ? ? ? ? textBox1.Text = $"耗時:{costTime:F2}ms";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "失敗," + msg.ToString();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// binary
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button4_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (image_path == "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? textBox1.Text = "執行中……";
? ? ? ? ? ? Application.DoEvents();

? ? ? ? ? ? if (image != null) image.Dispose();
? ? ? ? ? ? if (out_img != null) out_img.Dispose();
? ? ? ? ? ? if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

? ? ? ? ? ? StringBuilder msg = new StringBuilder(512);
? ? ? ? ? ? image = new Mat(image_path);
? ? ? ? ? ? out_img = new Mat();

? ? ? ? ? ? stopwatch.Restart();

? ? ? ? ? ? int res = binary(engine, image.CvPtr, msg, out_img.CvPtr);
? ? ? ? ? ? if (res == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? stopwatch.Stop();
? ? ? ? ? ? ? ? double costTime = stopwatch.Elapsed.TotalMilliseconds;
? ? ? ? ? ? ? ? pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());
? ? ? ? ? ? ? ? textBox1.Text = $"耗時:{costTime:F2}ms";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "失敗," + msg.ToString();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? 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";
? ? ? ? ? ? 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;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MessageBox.Show("保存成功,位置:" + sdf.FileName);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
?

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;namespace DocumentUndistort
{public partial class Form1 : Form{public Form1(){InitializeComponent();}Stopwatch stopwatch = new Stopwatch();Mat image;Mat out_img;string image_path;string startupPath;string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";const string DllName = "DocumentUndistortSharp.dll";IntPtr engine;/*//初始化extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* binary_model_path, char* unblur_model_path, char* unshadow_model_gcnet_path, char* unshadow_model_drnet_path, char* unwrap_model_path, char* msg);//binaryextern "C" _declspec(dllexport) int __cdecl  binary(void* engine, Mat* srcimg, char* msg, Mat* out_img);//unblurextern "C" _declspec(dllexport) int __cdecl  unblur(void* engine, Mat* srcimg, char* msg, Mat* out_img);//unshadowextern "C" _declspec(dllexport) int __cdecl  unshadow(void* engine, Mat* srcimg, char* msg, Mat* out_img);//unwrapextern "C" _declspec(dllexport) int __cdecl  unwrap(void* engine, Mat* srcimg, char* msg, Mat* out_img);//openCvBilateralextern "C" _declspec(dllexport) int __cdecl  openCvBilateral(Mat* srcimg, char* msg, Mat* out_img);//釋放extern "C" _declspec(dllexport) void __cdecl destroy(void* engine);*/[DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]internal extern static int init(ref IntPtr engine, string binary_model_path, string unblur_model_path, string unshadow_model_gcnet_path, string unshadow_model_drnet_path, string unwrap_model_path, StringBuilder msg);[DllImport(DllName, EntryPoint = "binary", CallingConvention = CallingConvention.Cdecl)]internal extern static int binary(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);[DllImport(DllName, EntryPoint = "unblur", CallingConvention = CallingConvention.Cdecl)]internal extern static int unblur(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);[DllImport(DllName, EntryPoint = "unshadow", CallingConvention = CallingConvention.Cdecl)]internal extern static int unshadow(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);[DllImport(DllName, EntryPoint = "unwrap", CallingConvention = CallingConvention.Cdecl)]internal extern static int unwrap(IntPtr engine, IntPtr srcimg, StringBuilder msg, IntPtr out_img);[DllImport(DllName, EntryPoint = "openCvBilateral", CallingConvention = CallingConvention.Cdecl)]internal extern static int openCvBilateral(IntPtr srcimg, StringBuilder msg, IntPtr out_img);[DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]internal extern static int destroy(IntPtr engine);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 Form1_Load(object sender, EventArgs e){startupPath = Application.StartupPath;string binary_model_path = startupPath + "\\model\\unetcnn.onnx";string unblur_model_path = startupPath + "\\model\\nafdpm.onnx";string unshadow_model_gcnet_path = startupPath + "\\model\\gcnet.onnx";string unshadow_model_drnet_path = startupPath + "\\model\\drnet.onnx";string unwrap_model_path = startupPath + "\\model\\uvdoc.onnx";StringBuilder msg = new StringBuilder(512);int res = init(ref engine, binary_model_path, unblur_model_path, unshadow_model_gcnet_path, unshadow_model_drnet_path, unwrap_model_path, msg);if (res == -1){MessageBox.Show(msg.ToString());return;}else{Console.WriteLine(msg.ToString());}image_path = startupPath + "\\test_img\\2.jpg";pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void Form1_FormClosed(object sender, FormClosedEventArgs e){destroy(engine);}/// <summary>/// unwrap/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "執行中……";Application.DoEvents();if (image != null) image.Dispose();if (out_img != null) out_img.Dispose();if (pictureBox1.Image != null) pictureBox1.Image.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);out_img = new Mat();stopwatch.Restart();int res = unwrap(engine, image.CvPtr, msg, out_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());textBox1.Text = $"耗時:{costTime:F2}ms";}else{textBox1.Text = "失敗," + msg.ToString();}}/// <summary>/// openCvBilateral/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button7_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "執行中……";Application.DoEvents();if (image != null) image.Dispose();if (out_img != null) out_img.Dispose();if (pictureBox1.Image != null) pictureBox1.Image.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);out_img = new Mat();stopwatch.Restart();int res = openCvBilateral(image.CvPtr, msg, out_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());textBox1.Text = $"耗時:{costTime:F2}ms";}else{textBox1.Text = "失敗," + msg.ToString();}}/// <summary>/// unshadow/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button6_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "執行中……";Application.DoEvents();if (image != null) image.Dispose();if (out_img != null) out_img.Dispose();if (pictureBox1.Image != null) pictureBox1.Image.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);out_img = new Mat();stopwatch.Restart();int res = unshadow(engine, image.CvPtr, msg, out_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());textBox1.Text = $"耗時:{costTime:F2}ms";}else{textBox1.Text = "失敗," + msg.ToString();}}/// <summary>/// unblur/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button5_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "執行中……";Application.DoEvents();if (image != null) image.Dispose();if (out_img != null) out_img.Dispose();if (pictureBox1.Image != null) pictureBox1.Image.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);out_img = new Mat();stopwatch.Restart();int res = unblur(engine, image.CvPtr, msg, out_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());textBox1.Text = $"耗時:{costTime:F2}ms";}else{textBox1.Text = "失敗," + msg.ToString();}}/// <summary>/// binary/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button4_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "執行中……";Application.DoEvents();if (image != null) image.Dispose();if (out_img != null) out_img.Dispose();if (pictureBox1.Image != null) pictureBox1.Image.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);out_img = new Mat();stopwatch.Restart();int res = binary(engine, image.CvPtr, msg, out_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(out_img.ToMemoryStream());textBox1.Text = $"耗時:{costTime:F2}ms";}else{textBox1.Text = "失敗," + msg.ToString();}}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";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;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}}
}

下載

源碼下載

參考

https://github.com/hpc203/document-undistort-onnxrun

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/66534.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/66534.shtml
英文地址,請注明出處:http://en.pswp.cn/web/66534.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CSS 溢出問題及解決方案:實用案例與技巧

在網頁開發中&#xff0c;CSS 的布局和樣式起著至關重要的作用&#xff0c;但經常會遇到一個棘手的問題——溢出問題。溢出是指元素內的內容超出了其設定的容器大小&#xff0c;這不僅會影響頁面的美觀&#xff0c;還可能干擾用戶體驗。本文將詳細探討 CSS 溢出問題的案例&…

生成樹機制實驗

1 實驗內容 1、基于已有代碼,實現生成樹運行機制,對于給定拓撲(four_node_ring.py),計算輸出相應狀態下的生成樹拓撲 2、構造一個不少于7個節點,冗余鏈路不少于2條的拓撲,節點和端口的命名規則可參考four_node_ring.py,使用stp程序計算輸出生成樹拓撲 2 實驗原理 一、…

數據結構詳解——堆與二叉樹

? 目錄 樹的概念樹的表示方法二叉樹的概念特殊的二叉樹二叉樹的性質二叉樹的存儲結構順序存儲鏈式存儲 堆的概念與結構堆的性質堆的實現堆的初始化入堆堆的擴容向上調整算法出堆&#xff08;最頂端元素&#xff09;向下調整算法 二叉樹的實現二叉樹的創建二叉樹的銷毀二叉樹的…

【藍橋杯】43694.正則問題

題目描述 考慮一種簡單的正則表達式&#xff1a; 只由 x ( ) | 組成的正則表達式。 小明想求出這個正則表達式能接受的最長字符串的長度。 例如 ((xx|xxx)x|(x|xx))xx 能接受的最長字符串是&#xff1a; xxxxxx&#xff0c;長度是 6。 輸入描述 一個由 x()| 組成的正則表達式。…

mac m1下載maven安裝并配置環境變量

下載地址&#xff1a;Download Apache Maven – Maven 解壓到一個沒有中文和空格的文件夾 輸入pwd查看安裝路徑 輸入cd返回根目錄再輸入 code .zshrc 若顯示 command not found: code你可以通過以下步驟來安裝和配置 code 命令&#xff1a; 1. 確保你已經安裝了 Visual Studio…

【自己動手開發Webpack插件:開啟前端構建工具的個性化定制之旅】

在前端開發的世界里&#xff0c;Webpack無疑是構建工具中的“明星”。它強大的功能可以幫助我們高效地打包和管理前端資源。然而&#xff0c;有時候默認的Webpack功能可能無法完全滿足我們的特定需求&#xff0c;這時候就需要自定義Webpack插件來大展身手啦&#xff01;今天&am…

移遠通信多模衛星通信模組BG95-S5獲得Skylo網絡認證,進一步拓展全球衛星物聯網市場

近日&#xff0c;全球領先的物聯網整體解決方案供應商移遠通信正式宣布&#xff0c;其支持“衛星蜂窩”多模式的高集成度NTN衛星通信模組BG95-S5已成功獲得NTN網絡運營商Skylo的網絡認證。BG95-S5也成為了獲得該認證的最新款移遠衛星通信模組。 BG95-S5模組順利獲得Skylo認證&a…

1.3.淺層神經網絡

目錄 1.3.淺層神經網絡 1.3.1 淺層神經網絡表示 1.3.2 單個樣本的向量化表示 1.3.4 激活函數的選擇 1.3.5 修改激活函數 1.3.5 練習??????? 1.3.淺層神經網絡 1.3.1 淺層神經網絡表示 之前已經說過神經網絡的結構了&#xff0c;在這不重復敘述。假設我們有如下…

StarRocks強大的實時數據分析

代碼倉庫&#xff1a;https://github.com/StarRocks/starrocks?tabreadme-ov-file StarRocks | A High-Performance Analytical Database 快速開始&#xff1a;StarRocks | StarRocks StarRocks 是一款高性能分析型數據倉庫&#xff0c;使用向量化、MPP 架構、CBO、智能物化…

2024年博客之星主題創作|貓頭虎分享AI技術洞察:2025年AI發展趨勢前瞻與展望

2025年AI發展趨勢前瞻&#xff1a;貓頭虎深度解析未來科技與商業機遇 摘要 2024年&#xff0c;AI技術迎來爆發式增長&#xff0c;AIGC、智能體、AIRPA、AI搜索、推理模型等技術不斷突破&#xff0c;AI應用場景持續擴展。2025年&#xff0c;AI將進入全新發展階段&#xff0c;W…

PG vs MySQL mvcc機制實現的異同

MVCC實現方法比較 MySQL 寫新數據時&#xff0c;把舊數據寫入回滾段中&#xff0c;其他人讀數據時&#xff0c;從回滾段中把舊的數據讀出來 PostgreSQL 寫新數據時&#xff0c;舊數據不刪除&#xff0c;直接插入新數據。 MVCC實現的原理 PG的MVCC實現原理 定義多版本的數據…

Android SystemUI——CarSystemBar視圖解析(十一)

前面文章我們已經把 CarSystemBar 從啟動到構建視圖,再到將視圖添加到 Window 的流程分析完畢,我們知道默認情況下在車載系統中只顯示頂部欄和底部欄視圖的。這里我們在前面文章的基礎上以頂部欄為例具體解析其視圖的結構。 一、頂部欄解析 通過《CarSystemBar車載狀態欄》這…

51c~ONNX~合集1

我自己的原文哦~ https://blog.51cto.com/whaosoft/11608027 一、使用Pytorch進行簡單的自定義圖像分類 ~ONNX 推理 圖像分類是計算機視覺中的一項基本任務&#xff0c;涉及訓練模型將圖像分類為預定義類別。本文中&#xff0c;我們將探討如何使用 PyTorch 構建一個簡單的自定…

每打開一個chrome頁面都會【自動打開F12開發者模式】,原因是 使用HBuilderX會影響谷歌瀏覽器的瀏覽模式

打開 HBuilderX&#xff0c;點擊 運行 -> 運行到瀏覽器 -> 設置web服務器 -> 添加chrome瀏覽器安裝路徑 chrome谷歌瀏覽器插件 B站視頻下載助手插件&#xff1a; 參考地址&#xff1a;Chrome插件 - B站下載助手&#xff08;輕松下載bilibili嗶哩嗶哩視頻&#xff09…

go語言之OOP特性和演示

一、OOP特性 Go語言中的OOP特性 結構體&#xff1a;在Go中&#xff0c;結構體用于定義復合類型&#xff0c;類似于其他語言中的類。它可以包含字段&#xff08;屬性&#xff09;和方法&#xff08;行為&#xff09;。方法&#xff1a;Go允許為任何自定義類型&#xff08;包括…

USB3020任意波形發生器4路16位同步模擬量輸出卡1MS/s頻率 阿爾泰科技

信息社會的發展&#xff0c;在很大程度上取決于信息與信號處理技術的先進性。數字信號處理技術的出現改變了信息 與信號處理技術的整個面貌&#xff0c;而數據采集作為數字信號處理的必不可少的前期工作在整個數字系統中起到關鍵 性、乃至決定性的作用&#xff0c;其應用已經深…

ChatGPT大模型極簡應用開發-目錄

引言 要理解 ChatGPT&#xff0c;了解其背后的 Transformer 架構和 GPT 技術一路的演進則變得非常必要。 ChatGPT 背后的 LLM 技術使普通人能夠通過自然語言完成過去只能由程序員通過編程語言實現的任務&#xff0c;這是一場巨大的變革。然而&#xff0c;人類通常容易高估技術…

C++入門基礎篇:域、C++的輸入輸出、缺省參數、函數重載、引用、inline、nullptr

本篇文章是對C學習前期的一些基礎部分的學習分享&#xff0c;希望也能夠對你有所幫助。 那咱們廢話不多說&#xff0c;直接開始吧&#xff01; 目錄 1.第一個C程序 2. 域 3. namespace 3.1 namespace的作用 3.2 namespace的定義 3.3 namespace使用說明 4.C的輸入和輸出…

RabbitMQ---TTL與死信

&#xff08;一&#xff09;TTL 1.TTL概念 TTL又叫過期時間 RabbitMQ可以對隊列和消息設置TTL&#xff0c;當消息到達過期時間還沒有被消費時就會自動刪除 注&#xff1a;這里我們說的對隊列設置TTL,是對隊列上的消息設置TTL并不是對隊列本身&#xff0c;不是說隊列過期時間…

先進制造aps專題二十七 西門子opcenter aps架構分析

歐美的商業aps&#xff0c;主要就是sap apo,西門子opcenter aps,達索quintiq 從技術的層面&#xff0c;西門子aps是不如sap apo的&#xff0c;但是西門子aps是西門子數字化工廠產品的核心&#xff0c;有很多特色&#xff0c;所以分析 西門子aps主要分計劃器和排產器兩個部分 計…