C# OpenCV 部署RecRecNet广角图像畸变矫正

2024-06-17 09:28

本文主要是介绍C# OpenCV 部署RecRecNet广角图像畸变矫正,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C# OpenCV 部署RecRecNet广角图像畸变矫正

目录

说明

效果

模型信息

项目

代码

下载


说明

ICCV2023 - RecRecNet: Rectangling Rectified Wide-Angle Images by Thin-Plate Spline Model and DoF-based Curriculum Learning

参考:

https://github.com/KangLiao929/RecRecNet

https://github.com/hpc203/recrecnet-opencv-dnn

效果

模型信息

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

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

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

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        int inpHeight;
        int inpWidth;
        string modelpath;

        int grid_h = 8;
        int grid_w = 8;
        Mat grid;
        Mat W_inv;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_image;

        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)
        {
            modelpath = "model/model_deploy.onnx";

            inpHeight = 256;
            inpWidth = 256;

            opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

            Common.get_norm_rigid_mesh_inv_grid(ref grid, ref W_inv, inpHeight, inpWidth, grid_h, grid_w);

            image_path = "test_img/10.jpg";
            pictureBox1.Image = new Bitmap(image_path);

        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等……";
            pictureBox2.Image = null;
            Application.DoEvents();

            image = new Mat(image_path);
            dt1 = DateTime.Now;

            Mat img = new Mat();

            Cv2.Resize(image, img, new OpenCvSharp.Size(inpWidth, inpHeight));

            img.ConvertTo(img, MatType.CV_32FC3, 1.0f / 127.5f, -1.0f);

            BN_image = CvDnn.BlobFromImage(img);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[1] { new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            opencv_net.Forward(outs, outBlobNames);

            dt2 = DateTime.Now;

            float* offset = (float*)outs[0].Data;

            Mat tp = new Mat();
            Mat ori_mesh_np_x = new Mat();
            Mat ori_mesh_np_y = new Mat();
            Common.get_ori_rigid_mesh_tp(tp, ori_mesh_np_x, ori_mesh_np_y, offset, inpHeight, inpWidth, grid_h, grid_w);
            Mat T = W_inv * tp;   
            T = T.T();    

            Mat T_g = T * grid;

            Mat output_tps = Common._interpolate(BN_image, T_g, new OpenCvSharp.Size(inpWidth, inpHeight));
            Mat rectangling_np = (output_tps + 1) * 127.5;
            rectangling_np.ConvertTo(rectangling_np, MatType.CV_8UC3);
            Mat input_np = (img + 1) * 127.5;

            List<Mat> outputs = new List<Mat>();
            outputs.Add(rectangling_np);
            outputs.Add(input_np);
            outputs.Add(ori_mesh_np_x);
            outputs.Add(ori_mesh_np_y);

            Mat input_with_mesh = Common.draw_mesh_on_warp(outputs[1], outputs[2], outputs[3]);

            Cv2.CvtColor(outputs[0], outputs[0], ColorConversionCodes.BGR2RGB);

            Cv2.ImShow("mesh", input_with_mesh);

            result_image = outputs[0].Clone();
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}
 

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;namespace OpenCvSharp_DNN_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;int inpHeight;int inpWidth;string modelpath;int grid_h = 8;int grid_w = 8;Mat grid;Mat W_inv;Net opencv_net;Mat BN_image;Mat image;Mat result_image;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){modelpath = "model/model_deploy.onnx";inpHeight = 256;inpWidth = 256;opencv_net = CvDnn.ReadNetFromOnnx(modelpath);Common.get_norm_rigid_mesh_inv_grid(ref grid, ref W_inv, inpHeight, inpWidth, grid_h, grid_w);image_path = "test_img/10.jpg";pictureBox1.Image = new Bitmap(image_path);}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();image = new Mat(image_path);dt1 = DateTime.Now;Mat img = new Mat();Cv2.Resize(image, img, new OpenCvSharp.Size(inpWidth, inpHeight));img.ConvertTo(img, MatType.CV_32FC3, 1.0f / 127.5f, -1.0f);BN_image = CvDnn.BlobFromImage(img);//配置图片输入数据opencv_net.SetInput(BN_image);//模型推理,读取推理结果Mat[] outs = new Mat[1] { new Mat() };string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();opencv_net.Forward(outs, outBlobNames);dt2 = DateTime.Now;float* offset = (float*)outs[0].Data;Mat tp = new Mat();Mat ori_mesh_np_x = new Mat();Mat ori_mesh_np_y = new Mat();Common.get_ori_rigid_mesh_tp(tp, ori_mesh_np_x, ori_mesh_np_y, offset, inpHeight, inpWidth, grid_h, grid_w);Mat T = W_inv * tp;   T = T.T();    Mat T_g = T * grid;Mat output_tps = Common._interpolate(BN_image, T_g, new OpenCvSharp.Size(inpWidth, inpHeight));Mat rectangling_np = (output_tps + 1) * 127.5;rectangling_np.ConvertTo(rectangling_np, MatType.CV_8UC3);Mat input_np = (img + 1) * 127.5;List<Mat> outputs = new List<Mat>();outputs.Add(rectangling_np);outputs.Add(input_np);outputs.Add(ori_mesh_np_x);outputs.Add(ori_mesh_np_y);Mat input_with_mesh = Common.draw_mesh_on_warp(outputs[1], outputs[2], outputs[3]);Cv2.CvtColor(outputs[0], outputs[0], ColorConversionCodes.BGR2RGB);Cv2.ImShow("mesh", input_with_mesh);result_image = outputs[0].Clone();pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

源码下载

这篇关于C# OpenCV 部署RecRecNet广角图像畸变矫正的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1069069

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

opencv 滚动条

参数介绍:createTrackbar( trackbarname , "hello" , &alpha_slider ,alpha_max ,  on_trackbar )  ;在标签中显示的文字(提示滑动条的用途) TrackbarName创建的滑动条要放置窗体的名字 “hello”滑动条的取值范围从 0 到 alpha_max (最小值只能为 zero).滑动后的值存放在

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

在 Windows 上部署 gitblit

在 Windows 上部署 gitblit 在 Windows 上部署 gitblit 缘起gitblit 是什么安装JDK部署 gitblit 下载 gitblit 并解压配置登录注册为 windows 服务 修改 installService.cmd 文件运行 installService.cmd运行 gitblitw.exe查看 services.msc 缘起