C# Onnx 特征匹配 DeDoDe 检测,不描述---描述,不检测

目录

介绍

效果

模型信息

项目

代码 

下载 


介绍

github地址:https://github.com/Parskatt/DeDoDe

DeDoDe 🎶 Detect, Don't Describe - Describe, Don't Detect, for Local Feature Matching

The DeDoDe detector learns to detect 3D consistent repeatable keypoints, which the DeDoDe descriptor learns to match. The result is a powerful decoupled local feature matcher.

Training DeDoDe

DISCLAMER: I've (Johan) not yet tested that the training scripts here reproduces our original results. This repo is very similar to the internal training repo, but there might be bugs introduced by refactoring etc. Let me know if you face any issues reproducing our results (or if you somehow get better results :D).

See experiments for the scripts to train DeDoDe. We trained on a single A100-40GB with a batchsize of 8. Note that you need to do the data prep first, see data_prep.

As usual, we require that you have the MegaDepth dataset already downloaded, and that you have the prepared scene info from DKM.

效果

模型信息

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

Outputs
-------------------------
name:matches_A
tensor:Float[-1, -1]
name:matches_B
tensor:Float[-1, -1]
name:batch_ids
tensor:Int64[-1]
---------------------------------------------------------------

项目

VS2022

.net framework 4.8

OpenCvSharp 4.8

Microsoft.ML.OnnxRuntime 1.16.2

代码 

using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.ML.OnnxRuntime;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Drawing;
using static System.Net.Mime.MediaTypeNames;
using System.Numerics;

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

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

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

        int inpWidth;
        int inpHeight;

        float[] mean =new float[] { 0.485f, 0.456f, 0.406f };
        float[] std = new float[] { 0.229f, 0.224f, 0.225f };

        Mat image;
        Mat image2;

        string model_path = "";

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        Tensor<float> mask_tensor;
        List<NamedOnnxValue> input_ontainer;

        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        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 System.Drawing.Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();

            // 创建输出会话
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

            // 创建推理模型类,读取本地模型文件
            model_path = "model/dedode_end2end_1024.onnx";

            inpHeight = 256;
            inpWidth = 256;

            onnx_session = new InferenceSession(model_path, options);

            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();

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

            image_path2 = "test_img/im_B.jpg";
            pictureBox3.Image = new Bitmap(image_path2);

        }

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

            image = new Mat(image_path);
            image2 = new Mat(image_path2);

            float[] input_tensor_data = new float[2 * 3 * inpWidth * inpHeight];

            //preprocess
            Mat dstimg = new Mat();
            Cv2.CvtColor(image, dstimg, ColorConversionCodes.BGR2RGB);
            Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));
            for (int c = 0; c < 3; c++)
            {
                for (int i = 0; i < inpHeight; i++)
                {
                    for (int j = 0; j < inpWidth; j++)
                    {
                        float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
                        input_tensor_data[c * inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
                    }
                }
            }

            Cv2.CvtColor(image2, dstimg, ColorConversionCodes.BGR2RGB);
            Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));
            for (int c = 0; c < 3; c++)
            {
                for (int i = 0; i < inpHeight; i++)
                {
                    for (int j = 0; j < inpWidth; j++)
                    {
                        float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
                        input_tensor_data[(3+c )* inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
                    }
                }
            }

            input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 2, 3, inpHeight, inpWidth });

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_ontainer);
            dt2 = DateTime.Now;

            //Postprocessing
            //将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            float[] matches_A = results_onnxvalue[0].AsTensor<float>().ToArray();
            float[] matches_B = results_onnxvalue[1].AsTensor<float>().ToArray();
            int num_points = results_onnxvalue[0].AsTensor<float>().Dimensions[0];

            List<KeyPoint> points_A = new List<KeyPoint>();
            List<KeyPoint> points_B = new List<KeyPoint>();

            KeyPoint temp;
            for (int i = 0; i < num_points; i++)
            {
                temp = new KeyPoint();
                temp.Pt.X = (float)((matches_A[i * 2] + 1) * 0.5 * image.Cols);
                temp.Pt.Y = (float)((matches_A[i * 2 + 1] + 1) * 0.5 * image.Rows);
                temp.Size = 1f;
                points_A.Add(temp);
            }

            num_points = results_onnxvalue[1].AsTensor<float>().Dimensions[0];
            for (int i = 0; i < num_points; i++)
            {
                temp = new KeyPoint();
                temp.Pt.X = (float)((matches_B[i * 2] + 1) * 0.5 * image2.Cols);
                temp.Pt.Y = (float)((matches_B[i * 2 + 1] + 1) * 0.5 * image2.Rows);
                temp.Size = 1f;
                points_B.Add(temp);
            }

            //匹配结果放在matches里面
            num_points = points_A.Count();
            List<DMatch> matches=new List<DMatch>();
            for (int i = 0; i < num_points; i++)
            {
                matches.Add(new DMatch(i, i, 0f));
            }

            //按照匹配关系将图画出来,背景图为match_img
            Mat match_img = new Mat();
            Cv2.DrawMatches(image, points_A, image2, points_B, matches, match_img);

            pictureBox2.Image = new System.Drawing.Bitmap(match_img.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

        }

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

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

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

            image_path2 = ofd.FileName;
            pictureBox3.Image = new System.Drawing.Bitmap(image_path2);
            image2 = new Mat(image_path2);
        }

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

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

using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.ML.OnnxRuntime;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Drawing;
using static System.Net.Mime.MediaTypeNames;
using System.Numerics;namespace Onnx_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string image_path2 = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;int inpWidth;int inpHeight;float[] mean =new float[] { 0.485f, 0.456f, 0.406f };float[] std = new float[] { 0.229f, 0.224f, 0.225f };Mat image;Mat image2;string model_path = "";SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;Tensor<float> mask_tensor;List<NamedOnnxValue> input_ontainer;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;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 System.Drawing.Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){// 创建输入容器input_ontainer = new List<NamedOnnxValue>();// 创建输出会话options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行// 创建推理模型类,读取本地模型文件model_path = "model/dedode_end2end_1024.onnx";inpHeight = 256;inpWidth = 256;onnx_session = new InferenceSession(model_path, options);// 创建输入容器input_ontainer = new List<NamedOnnxValue>();image_path = "test_img/im_A.jpg";pictureBox1.Image = new Bitmap(image_path);image_path2 = "test_img/im_B.jpg";pictureBox3.Image = new Bitmap(image_path2);}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;System.Windows.Forms.Application.DoEvents();image = new Mat(image_path);image2 = new Mat(image_path2);float[] input_tensor_data = new float[2 * 3 * inpWidth * inpHeight];//preprocessMat dstimg = new Mat();Cv2.CvtColor(image, dstimg, ColorConversionCodes.BGR2RGB);Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));for (int c = 0; c < 3; c++){for (int i = 0; i < inpHeight; i++){for (int j = 0; j < inpWidth; j++){float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];input_tensor_data[c * inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);}}}Cv2.CvtColor(image2, dstimg, ColorConversionCodes.BGR2RGB);Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));for (int c = 0; c < 3; c++){for (int i = 0; i < inpHeight; i++){for (int j = 0; j < inpWidth; j++){float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];input_tensor_data[(3+c )* inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);}}}input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 2, 3, inpHeight, inpWidth });//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);dt2 = DateTime.Now;//Postprocessing//将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();float[] matches_A = results_onnxvalue[0].AsTensor<float>().ToArray();float[] matches_B = results_onnxvalue[1].AsTensor<float>().ToArray();int num_points = results_onnxvalue[0].AsTensor<float>().Dimensions[0];List<KeyPoint> points_A = new List<KeyPoint>();List<KeyPoint> points_B = new List<KeyPoint>();KeyPoint temp;for (int i = 0; i < num_points; i++){temp = new KeyPoint();temp.Pt.X = (float)((matches_A[i * 2] + 1) * 0.5 * image.Cols);temp.Pt.Y = (float)((matches_A[i * 2 + 1] + 1) * 0.5 * image.Rows);temp.Size = 1f;points_A.Add(temp);}num_points = results_onnxvalue[1].AsTensor<float>().Dimensions[0];for (int i = 0; i < num_points; i++){temp = new KeyPoint();temp.Pt.X = (float)((matches_B[i * 2] + 1) * 0.5 * image2.Cols);temp.Pt.Y = (float)((matches_B[i * 2 + 1] + 1) * 0.5 * image2.Rows);temp.Size = 1f;points_B.Add(temp);}//匹配结果放在matches里面num_points = points_A.Count();List<DMatch> matches=new List<DMatch>();for (int i = 0; i < num_points; i++){matches.Add(new DMatch(i, i, 0f));}//按照匹配关系将图画出来,背景图为match_imgMat match_img = new Mat();Cv2.DrawMatches(image, points_A, image2, points_B, matches, match_img);pictureBox2.Image = new System.Drawing.Bitmap(match_img.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void button3_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox3.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path2 = ofd.FileName;pictureBox3.Image = new System.Drawing.Bitmap(image_path2);image2 = new Mat(image_path2);}private void pictureBox3_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox3.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载 

源码下载

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

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

相关文章

Redis主从,缓存击穿,雪崩,哨兵等问题

Redis的性能管理&#xff1a; Redis的数据缓存在内存当中 INFO memory used_memory:853808 Redis中数据占用的内存 used_memory_rss:3715072 Redis向操作系统申请的内容 used_memory_peak:853808 Redis使用的内存的峰值 系统巡检&#xff1a;硬件巡检&#xff0c;数据库…

流量主如何在广告收益和用户体验中找到平衡

流量主在广告收益和用户体验之间找到平衡是一个关键的挑战&#xff0c;因为过多或不恰当的广告可能会影响到用户的满意度和留存率。以下是一些方法&#xff0c;可以帮助流量主在这两者之间找到平衡&#xff1a; admaoyan猫眼聚合 优质内容为先&#xff1a; 提供高质量、有价值的…

解析IBM SPSS Statistics 26 forMac/win中文版:全面统计分析解决方案

作为一款强大的统计分析软件&#xff0c;IBM SPSS Statistics 26&#xff08;spss统计软件&#xff09;在全球范围内被广泛使用。无论是学术研究、市场调研还是商业决策&#xff0c;SPSS统计软件都能提供全面的解决方案&#xff0c;帮助用户快速、准确地分析数据。 首先&#…

第二证券:什么是权重股?权重股可以长期持有吗?

权重版块是指该版块股票市值巨大&#xff0c;在股票总市值中的比重很大&#xff08;即权重很大&#xff09;&#xff0c;其涨跌对股票指数的影响很大的一个版块&#xff0c;比方&#xff0c;商场上的证券、钢铁、银行、保险、石油等板块的个股。 权重股适合长时间持有&#xf…

Mac- Iterm 2 (替换mac terminal 终端的下一代终端工具)

1.应用场景 主要用于macOS中作为终端工具使用, 执行终端命令, Iterm 2 (替换mac terminal 终端的下一代终端工具) 2.学习/操作 1.文档阅读 chatgpt & 其他资料 2.整理输出 2.1 是什么 Iterm 2 (替换mac terminal 终端的下一代终端工具) 2.2 为什么需要「应用场景」 macOS…

skywalking中gateway的拓扑图没有出现

背景&#xff1a; 刚开始的时候gateway没有出现&#xff0c;后来百度说添加插件的jar包&#xff0c; apm-spring-cloud-gateway-2.1.x-plugin-8.15.0.jar apm-spring-webflux-5.x-plugin-8.15.0.jar 然后解决了gateway节点出来了&#xff0c; 但是&#xff1a;拓扑图却是User指…

sam和mobilesam导出预处理的onnx

一、前言 sam或者mobilesam的python推理都存在一些前处理,如下所示: sam.to(device=cuda) predictor = SamPredictor(sam) predictor.set_image(image) image_embedding = predictor.get_image_embedding().cpu().numpy() checkpoint = "./weights/mobile_sam.pt"…

利用NVIDIA DALI读取视频帧

1. NVIDIA DALI简介 NVIDIA DALI全称是NVIDIA Data Loading Library&#xff0c;是一个用GPU加速的数据加载和预处理库&#xff0c;可用于图像、视频和语音数据的加载和处理&#xff0c;从而为深度学习的训练和推理加速。 NVIDIA DALI库的出发点是&#xff0c;深度学习应用中…

文心大模型商业化领跑,百度在自我颠覆中重构生长力

随着科技巨头竞逐AI大模型&#xff0c;人工智能技术成为今年最受瞩目的新技术。但是&#xff0c;AI大模型的创新之路&#xff0c;还缺少一个足够有力的商业化答案。 作为全球最先发布大模型的互联网大厂&#xff0c;百度能否加速大模型的应用落地&#xff0c;以及文心大模型能…

VMware 系列:vmware vsphere虚拟化简介以及产品系下载、Esxi安装

vmware vsphere虚拟化简介以及产品系下载、Esxi安装 一. vmware vsphere 虚拟化(1.1)为什么要学习虚拟化(1.2)虚拟化产品介绍(1.3)Vsphere6.7特性二. ESXI(2.0)如何知道哪些品牌对应支持哪些版本的 ESXI,ESXI各个版本对OS的支持又是什么?比如看看哪些机器支持哪些版本…

排查生产环境:MySQLTransactionRollbackException数据库死锁

一. 问题现状 程序直接宕机&#xff0c;并在error.log日志中发现大量的报错日志&#xff0c;如下&#xff1a; ### Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting trans…

C++ Boost 实现异步端口扫描器

端口扫描是一种用于识别目标系统上哪些网络端口处于开放、关闭或监听状态的网络活动。在计算机网络中&#xff0c;端口是一个虚拟的通信端点&#xff0c;用于在计算机之间传输数据。每个端口都关联着特定类型的网络服务或应用程序。端口扫描通常是网络管理员、安全专业人员用来…

springboot项目中获取业务功能的导入数据模板文件

场景: 在实际业务场景中,经常会遇到某些管理功能需要数据导入共功能,但既然是导入数据,肯定会有规则限制,有规则就会有数据模板,但这个模板一般是让客户自己下载固定规则模板,而不是让客户自己随便上传模板。下面介绍直接下载模板 一、下载模板示例 1、在项目的…

MySQL 中 DELETE 语句中可以使用别名么?

某天&#xff0c;正按照业务的要求删除不需要的数据&#xff0c;在执行 DELETE 语句时&#xff0c;竟然出现了报错&#xff01; 作者&#xff1a;林靖华&#xff0c;开源数据库技术爱好者&#xff0c;擅长MySQL和Redis的运维 爱可生开源社区出品&#xff0c;原创内容未经授权不…

Mysql查看Binlog文件

前期准备 检查是否开启binlog mysql> SHOW VARIABLES LIKE log_bin; // 或者 mysql> SHOW VARIABLES LIKE log%;ON代表开启&#xff0c;OFF代表关闭。如为OFF需 开启 后才能查看&#xff0c;但只能查看开启之后时间点的。 查看binlog文件有哪些 一般yum安装的mysql…

【python学习】基础篇-常用第三方库-chardet:检测文本文件的编码格式

chardet是一个Python库&#xff0c;用于检测文本文件的编码格式。 以下是一些基本的用法&#xff1a; 使用chardet.detect()函数检测文件编码 with open(example.txt, rb) as f:result chardet.detect(f.read()) print(result[encoding])使用chardet.detect()函数检测字节串…

【python学习】基础篇-常用第三方库-requests库:用于发送各种类型的HTTP请求

在Python中&#xff0c;requests库是一个常用的HTTP请求库&#xff0c;用于发送各种类型的HTTP请求。 以下是一些基本的用法&#xff1a; 更多高级功能可以参考官方文档&#xff1a;https://docs.python-requests.org/ 发送GET请求 response requests.get(https://www.examp…

Program Header Table(转载)

程序头表与段表相互独立&#xff0c;由ELF文件头统一管理。 程序头表负责ELF文件从文件到加载后映像的映射关系&#xff0c;一般只有可执行文件包含。 1. segment和section segment: 程序头表项描述的对象称为segment&#xff0c;即elf文件加载后的数据块&#xff1b; 它提供…

exports和module.exports 区别

在 Node.js 中&#xff0c;“exports”和“module.exports”两者的区别有以下几点&#xff1a; 导出对象类型不同&#xff1a; exports 是对 module.exports 的一个全局引用&#xff0c;而实际导出的是 module.exports 对象。即 exports 只是为了方便&#xff0c;可以在不断开…

C++学习(2):分配器allocator

new和operator new new是关键字&#xff0c;new 操作符的执行过程&#xff1a; 调用operator new分配内存&#xff1b;调用构造函数在operator new返回的内存地址处生成类对象&#xff1b; operator new是一个函数&#xff0c;可以被重载&#xff0c;通过重载它&#xff0c;…