C# OpenCvSharp 部署MOWA:多合一图像扭曲模型

目录

说明

效果

项目

代码

下载

参考


C# OpenCvSharp 部署MOWA:多合一图像扭曲模型

说明

算法模型的paper名称是《MOWA: Multiple-in-One Image Warping Model》

ariv链接 https://arxiv.org/pdf/2404.10716 

效果

Stitched Image

翻译成中文意思是:拼接图像。效果图如下,可以看到输入原图的边界是不规则的,在经过模型推理之后输出的图的边界是规整了。

Rectified Wide-Angle Image

翻译成中文意思是:矫正广角图像。效果图如下,可以看到输入原图的左右上下边界是扭曲的,在经过模型推理之后输出的图的边界是规整了。

Unrolling Shutter Image

翻译成中文意思是:卷帘快门图像。效果图如下,可以看到输入原图的右边界是弯曲的,在经过模型推理之后输出的图的边界是规整了。

Rotated Image

翻译成中文意思是:旋转图像。效果图如下,可以看到输入原图是有明显倾斜的,在经过模型推理之后输出的图是摆正了。

Fisheye Image

翻译成中文意思是:鱼眼图像。效果图如下,可以看到输入原图里的建筑物有明显扭曲的,在经过模型推理之后输出的图里的建筑物是正直的了。

项目

代码

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        Stopwatch stopwatch = new Stopwatch();
        Mat image;
        Mat mask;
        Mat mesh_img;
        Mat grid_mesh_img;
        Mat flow_img;

        string image_path;
        string mask_path;
        string startupPath;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        const string DllName = "MOWASharp.dll";
        IntPtr engine;

        /*
         //初始化
        extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* model_dir,char* msg);

        //detect
        extern "C" _declspec(dllexport) int __cdecl  detect(void* engine, Mat* srcimg, Mat* maskimg, char* msg, Mat* mesh_img, Mat* grid_mesh_img, Mat* flow_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 model_dir, StringBuilder msg);

        [DllImport(DllName, EntryPoint = "detect", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int detect(IntPtr engine, IntPtr srcimg, IntPtr maskimg, StringBuilder msg, IntPtr mesh_img, IntPtr grid_mesh_img, IntPtr flow_img);

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

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            string model_dir = startupPath + "\\model\\";

            StringBuilder msg = new StringBuilder(512);

            int res = init(ref engine, model_dir, msg);
            if (res == -1)
            {
                MessageBox.Show(msg.ToString());
                return;
            }
            else
            {
                Console.WriteLine(msg.ToString());
            }
            image_path = startupPath + "\\test_img\\39501.png";
            pictureBox1.Image = new Bitmap(image_path);

            mask_path = startupPath + "\\test_img\\39501_mask.png";
            pictureBox3.Image = new Bitmap(mask_path);
        }

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

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

            textBox1.Text = "执行中……";
            button2.Enabled = false;
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
                pictureBox2.Image = null;
            }
            Application.DoEvents();

            Cv2.DestroyAllWindows();
            if (image != null) image.Dispose();
            if (mask != null) mask.Dispose();
            if (mesh_img != null) mesh_img.Dispose();
            if (grid_mesh_img != null) grid_mesh_img.Dispose();
            if (flow_img != null) flow_img.Dispose();

            StringBuilder msg = new StringBuilder(512);
            image = new Mat(image_path);
            mask = new Mat(mask_path,ImreadModes.Grayscale);
            mesh_img = new Mat();
            grid_mesh_img = new Mat();
            flow_img = new Mat();

            stopwatch.Restart();

            int res = detect(engine, image.CvPtr, mask.CvPtr, msg, mesh_img.CvPtr, grid_mesh_img.CvPtr, flow_img.CvPtr);
            if (res == 0)
            {
                stopwatch.Stop();
                double costTime = stopwatch.Elapsed.TotalMilliseconds;
                pictureBox2.Image = new Bitmap(flow_img.ToMemoryStream());
                Cv2.ImShow("mesh_img", mesh_img);
                Cv2.ImShow("grid_mesh_img", grid_mesh_img);
                textBox1.Text = $"耗时:{costTime:F2}ms";
            }
            else
            {
                textBox1.Text = "失败," + msg.ToString();
            }
            button2.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            ofd.InitialDirectory = startupPath + "\\test_img";
            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 button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            ofd.InitialDirectory = startupPath+"\\test_img";
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox3.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            mask_path = ofd.FileName;
            pictureBox3.Image = new Bitmap(mask_path);
            mask = new Mat(image_path);
        }

        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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace MOWA
{public partial class Form1 : Form{public Form1(){InitializeComponent();}Stopwatch stopwatch = new Stopwatch();Mat image;Mat mask;Mat mesh_img;Mat grid_mesh_img;Mat flow_img;string image_path;string mask_path;string startupPath;string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";const string DllName = "MOWASharp.dll";IntPtr engine;/*//初始化extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* model_dir,char* msg);//detectextern "C" _declspec(dllexport) int __cdecl  detect(void* engine, Mat* srcimg, Mat* maskimg, char* msg, Mat* mesh_img, Mat* grid_mesh_img, Mat* flow_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 model_dir, StringBuilder msg);[DllImport(DllName, EntryPoint = "detect", CallingConvention = CallingConvention.Cdecl)]internal extern static int detect(IntPtr engine, IntPtr srcimg, IntPtr maskimg, StringBuilder msg, IntPtr mesh_img, IntPtr grid_mesh_img, IntPtr flow_img);[DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]internal extern static int destroy(IntPtr engine);private void Form1_Load(object sender, EventArgs e){startupPath = Application.StartupPath;string model_dir = startupPath + "\\model\\";StringBuilder msg = new StringBuilder(512);int res = init(ref engine, model_dir, msg);if (res == -1){MessageBox.Show(msg.ToString());return;}else{Console.WriteLine(msg.ToString());}image_path = startupPath + "\\test_img\\39501.png";pictureBox1.Image = new Bitmap(image_path);mask_path = startupPath + "\\test_img\\39501_mask.png";pictureBox3.Image = new Bitmap(mask_path);}private void Form1_FormClosed(object sender, FormClosedEventArgs e){destroy(engine);}private void button2_Click(object sender, EventArgs e){if (image_path == "" || mask_path == ""){return;}textBox1.Text = "执行中……";button2.Enabled = false;if (pictureBox2.Image != null){pictureBox2.Image.Dispose();pictureBox2.Image = null;}Application.DoEvents();Cv2.DestroyAllWindows();if (image != null) image.Dispose();if (mask != null) mask.Dispose();if (mesh_img != null) mesh_img.Dispose();if (grid_mesh_img != null) grid_mesh_img.Dispose();if (flow_img != null) flow_img.Dispose();StringBuilder msg = new StringBuilder(512);image = new Mat(image_path);mask = new Mat(mask_path,ImreadModes.Grayscale);mesh_img = new Mat();grid_mesh_img = new Mat();flow_img = new Mat();stopwatch.Restart();int res = detect(engine, image.CvPtr, mask.CvPtr, msg, mesh_img.CvPtr, grid_mesh_img.CvPtr, flow_img.CvPtr);if (res == 0){stopwatch.Stop();double costTime = stopwatch.Elapsed.TotalMilliseconds;pictureBox2.Image = new Bitmap(flow_img.ToMemoryStream());Cv2.ImShow("mesh_img", mesh_img);Cv2.ImShow("grid_mesh_img", grid_mesh_img);textBox1.Text = $"耗时:{costTime:F2}ms";}else{textBox1.Text = "失败," + msg.ToString();}button2.Enabled = true;}private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;ofd.InitialDirectory = startupPath + "\\test_img";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 button4_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;ofd.InitialDirectory = startupPath+"\\test_img";if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox3.Image = null;pictureBox2.Image = null;textBox1.Text = "";mask_path = ofd.FileName;pictureBox3.Image = new Bitmap(mask_path);mask = new Mat(image_path);}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/MOWA-onnxrun

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/15047.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java】线上故障排查实战

引言 JVM命令详细可以看前一篇文章,本篇文章基于之前的命令做一次简单的线上故障排查分析 JVM常见命令 实战 1. 一般显示都是Linux系统,我们排查winodows系统想知道CPU和内存使用情况,打开任务管理器就可以出现图形化界面,而L…

编译spring 6.2.2

如何编译Spring 6.2.2 下载spring 6.2.2 首先,下载spring 6.2.2,地址:下载 解压到你的目录下。 下载gradle 下载gradle,这是spring项目的依赖管理工具,本文下载的是8.12.1 gradle下载 下载合适的JDK 本文下载的是…

深度求索(DeepSeek)的AI革命:NLP、CV与智能应用的技术跃迁

Deepseek官网:DeepSeek 引言:AI技术浪潮中的深度求索 近年来,人工智能技术以指数级速度重塑全球产业格局。在这场技术革命中,深度求索(DeepSeek)凭借其前沿的算法研究、高效的工程化能力以及对垂直场景的…

Android Studio超级详细讲解下载、安装配置教程(建议收藏)

博主介绍:✌专注于前后端、机器学习、人工智能应用领域开发的优质创作者、秉着互联网精神开源贡献精神,答疑解惑、坚持优质作品共享。本人是掘金/腾讯云/阿里云等平台优质作者、擅长前后端项目开发和毕业项目实战,深受全网粉丝喜爱与支持✌有…

计算机毕业设计Python+Vue.js游戏推荐系统 Steam游戏推荐系统 Django Flask 游 戏可视化 游戏数据分析 游戏大数据 爬虫

温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 温馨提示:文末有 CSDN 平台官方提供的学长联系方式的名片! 作者简介:Java领…

版本更新|OpenCSG AutoHub v0.2.8

AutoHub v0.2.8现已发布! AutoHub v0.2.8本次更新致力于提升用户体验、增强系统的兼容性和流畅性。通过优化单页应用的支持、提示语推荐功能以及新增页面跳转支持,用户在执行工作流时能够更加高效、便捷。同时,针对界面的多项优化&#xff0…

DeepSeek-R1模型的数学原理(说人话)

文章目录 1、什么是GRPO2、数学原理3、比喻4、流程总结 🍃作者介绍:双非本科大四网络工程专业在读,阿里云专家博主,前三年专注于Java领域学习,擅长web应用开发,目前已转行人工智能领域。 🦅个人…

智慧停车场解决方案(文末联系,领取整套资料,可做论文)

一、方案概述 本智慧停车场解决方案旨在通过硬件设备与软件系统的深度整合,实现停车场的智能化管理与服务,提升车主的停车体验,优化停车场运营效率。 二、硬件架构 硬件设备说明: 车牌识别摄像机:安装在停车场入口和…

对“云原生”的初印象

一、背景 最近因为在工作中以及一些技术博客中听的比较火的一个关键词 "云原生",于是产生了好奇,云原生到底是什么东西?自己对云原生也是一个纯小白,于是带着这个问题去好好了解一下,什么是"云原生&qu…

物联网软件开发与应用方向应该怎样学习,学习哪些内容,就业方向是怎样?(文末领取整套学习视频,课件)物联网硬件开发与嵌入式系统

随着物联网技术的飞速发展,物联网软件开发与应用方向成为了众多开发者关注的焦点。那么,如何在这个领域中脱颖而出呢?本文将为你提供一份详细的学习指南,帮助你从零开始,逐步掌握物联网软件开发与应用的核心技能。 一…

数据结构-基础

1、概念: 程序 数据结构 算法 2、程序的好坏 可读性,稳定性,扩展性,时间复杂度,空间复杂度。 3、数据结构 是指存储、组织数据的方式,以便高效地进行访问和修改。通过选择适当的数据结构, 能…

蓝耘智算平台与DeepSeek R1模型:推动深度学习发展

公主请阅 前言何为DeepSeek R1DeepSeek R1 的特点DeepSeek R1 的应用领域DeepSeek R1 与其他模型的对比 何为蓝耘智算平台使用蓝耘智算平台深度使用DeepSeek R1代码解释:处理示例输入:输出结果: 前言 在深度学习领域,创新迭代日新…

神经网络(Neural Network)

引言 神经网络,作为人工智能和机器学习领域的核心组成部分,近年来在诸多领域取得了显著的进展。受生物神经系统的启发,神经网络通过模拟人脑神经元的工作机制,能够从大量数据中学习复杂的模式和关系。其强大的非线性建模能力使其在图像识别、自然语言处理、语音识别和预测…

基于python多线程多进程爬虫的maa作业站技能使用分析

基于python多线程多进程爬虫的maa作业站技能使用分析 技能使用分析 多线程(8核) import json import multiprocessing import requests from multiprocessing.dummy import Pooldef maa(st):url "https://prts.maa.plus/copilot/get/"m …

npm无法加载文件 因为此系统禁止运行脚本

安装nodejs后遇到问题: 在项目里【node -v】可以打印出来,【npm -v】打印不出来,显示npm无法加载文件 因为此系统禁止运行脚本。 但是在winr,cmd里【node -v】,【npm -v】都也可打印出来。 解决方法: cmd里可以打印出…

2.9寒假作业

web:[SWPUCTF 2022 新生赛]ez_ez_php(revenge) 打开环境,进行代码审计 下面有提示访问游戏flag.php,尝试看看 提示了正确的flag,还有要使用为协议,之前也了解过,关于执行包含文件例如include可使用为协议绕…

【Matlab优化算法-第13期】基于多目标优化算法的水库流量调度

一、前言 水库流量优化是水资源管理中的一个重要环节,通过合理调度水库流量,可以有效平衡防洪、发电和水资源利用等多方面的需求。本文将介绍一个水库流量优化模型,包括其约束条件、目标函数以及应用场景。 二、模型概述 水库流量优化模型…

Mybatis

入门 配置SQL提示 JDBC介绍 JDBC:(Java DataBase Connectivity),就是使用Java语言操作关系型数据库的一套API 本质 sun公司官方定义的一套操作所有关系型数据库的规范,即接口 各个数据库厂商去实现这套接口,提供数据库驱动jar包 我们可以使…

Deepseek的MLA技术原理介绍

DeepSeek的MLA(Multi-head Latent Attention)技术是一种创新的注意力机制,旨在优化Transformer模型的计算效率和内存使用,同时保持模型性能。以下是MLA技术的详细原理和特点: 1. 核心思想 MLA技术通过低秩联合压缩技术,将多个注意力头的键(Key)和值(Value)映射到一…

使用 SDKMAN! 在 Mac(包括 ARM 架构的 M1/M2 芯片)上安装 Java 8

文章目录 1. 安装 SDKMAN!2. 查找可用的 Java 8 版本3. 安装 Java 84. 验证安装5. 切换 Java 版本(可选)6. 解决 ARM 架构兼容性问题总结 可以使用 SDKMAN! 在 Mac(包括 ARM 架构的 M1/M2 芯片)上安装 Java 8。SDKMAN! 是一个强大…