百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

C# OpenCvSharp 部署文档矫正,包括文档扭曲/模糊/阴影等情况

csdh11 2025-02-10 11:58 32 浏览

说明

地址: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]
---------------------------------------------------------------

项目

C++封装DLL,C#调用

代码

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 = ;
pictureBox2.Image = ;
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);
}

///
/// unwrap
///

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

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

if (image != ) image.Dispose();
if (out_img != ) out_img.Dispose();
if (pictureBox1.Image != ) 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();
}
}

///
/// openCvBilateral
///

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

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

if (image != ) image.Dispose();
if (out_img != ) out_img.Dispose();
if (pictureBox1.Image != ) 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();
}
}

///
/// unshadow
///

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

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

if (image != ) image.Dispose();
if (out_img != ) out_img.Dispose();
if (pictureBox1.Image != ) 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();
}
}

///
/// unblur
///

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

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

if (image != ) image.Dispose();
if (out_img != ) out_img.Dispose();
if (pictureBox1.Image != ) 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();
}
}

///
/// binary
///

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

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

if (image != ) image.Dispose();
if (out_img != ) out_img.Dispose();
if (pictureBox1.Image != ) 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 == )
{
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


相关推荐

探索Java项目中日志系统最佳实践:从入门到精通

探索Java项目中日志系统最佳实践:从入门到精通在现代软件开发中,日志系统如同一位默默无闻却至关重要的管家,它记录了程序运行中的各种事件,为我们排查问题、监控性能和优化系统提供了宝贵的依据。在Java...

用了这么多年的java日志框架,你真的弄懂了吗?

在项目开发过程中,有一个必不可少的环节就是记录日志,相信只要是个程序员都用过,可是咱们自问下,用了这么多年的日志框架,你确定自己真弄懂了日志框架的来龙去脉嘛?下面笔者就详细聊聊java中常用日志框架的...

物理老师教你学Java语言(中篇)(物理专业学编程)

第四章物质的基本结构——类与对象...

一文搞定!Spring Boot3 定时任务操作全攻略

各位互联网大厂的后端开发小伙伴们,在使用SpringBoot3开发项目时,你是否遇到过定时任务实现的难题呢?比如任务调度时间不准确,代码报错却找不到方向,是不是特别头疼?如今,随着互联网业务规模...

你还不懂java的日志系统吗 ?(java的日志类)

一、背景在java的开发中,使用最多也绕不过去的一个话题就是日志,在程序中除了业务代码外,使用最多的就是打印日志。经常听到的这样一句话就是“打个日志调试下”,没错在日常的开发、调试过程中打印日志是常干...

谈谈枚举的新用法--java(java枚举的作用与好处)

问题的由来前段时间改游戏buff功能,干了一件愚蠢的事情,那就是把枚举和运算集合在一起,然后运行一段时间后buff就出现各种问题,我当时懵逼了!事情是这样的,做过游戏的都知道,buff,需要分类型,且...

你还不懂java的日志系统吗(javaw 日志)

一、背景在java的开发中,使用最多也绕不过去的一个话题就是日志,在程序中除了业务代码外,使用最多的就是打印日志。经常听到的这样一句话就是“打个日志调试下”,没错在日常的开发、调试过程中打印日志是常干...

Java 8之后的那些新特性(三):Java System Logger

去年12月份log4j日志框架的一个漏洞,给Java整个行业造成了非常大的影响。这个事情也顺带把log4j这个日志框架推到了争议的最前线。在Java领域,log4j可能相对比较流行。而在log4j之外...

Java开发中的日志管理:让程序“开口说话”

Java开发中的日志管理:让程序“开口说话”日志是程序员的朋友,也是程序的“嘴巴”。它能让程序在运行过程中“开口说话”,告诉我们它的状态、行为以及遇到的问题。在Java开发中,良好的日志管理不仅能帮助...

吊打面试官(十二)--Java语言中ArrayList类一文全掌握

导读...

OS X 效率启动器 Alfred 详解与使用技巧

问:为什么要在Mac上使用效率启动器类应用?答:在非特殊专业用户的环境下,(每天)用户一般可以在系统中进行上百次操作,可以是点击,也可以是拖拽,但这些只是过程,而我们的真正目的是想获得结果,也就是...

Java中 高级的异常处理(java中异常处理的两种方式)

介绍异常处理是软件开发的一个关键方面,尤其是在Java中,这种语言以其稳健性和平台独立性而闻名。正确的异常处理不仅可以防止应用程序崩溃,还有助于调试并向用户提供有意义的反馈。...

【性能调优】全方位教你定位慢SQL,方法介绍下!

1.使用数据库自带工具...

全面了解mysql锁机制(InnoDB)与问题排查

MySQL/InnoDB的加锁,一直是一个常见的话题。例如,数据库如果有高并发请求,如何保证数据完整性?产生死锁问题如何排查并解决?下面是不同锁等级的区别表级锁:开销小,加锁快;不会出现死锁;锁定粒度...

看懂这篇文章,你就懂了数据库死锁产生的场景和解决方法

一、什么是死锁加锁(Locking)是数据库在并发访问时保证数据一致性和完整性的主要机制。任何事务都需要获得相应对象上的锁才能访问数据,读取数据的事务通常只需要获得读锁(共享锁),修改数据的事务需要获...