C# Onnx 轻量实时的M-LSD直线检测

目录

介绍

效果

效果1

效果2

效果3

效果4

模型信息

项目

代码

下载

其他


介绍

github地址:https://github.com/navervision/mlsd 

M-LSD: Towards Light-weight and Real-time Line Segment Detection
Official Tensorflow implementation of "M-LSD: Towards Light-weight and Real-time Line Segment Detection" (AAAI 2022 Oral session)

Geonmo Gu*, Byungsoo Ko*, SeoungHyun Go, Sung-Hyun Lee, Jingeun Lee, Minchul Shin (* Authors contributed equally.)

First figure: Comparison of M-LSD and existing LSD methods on GPU. Second figure: Inference speed and memory usage on mobile devices.

We present a real-time and light-weight line segment detector for resource-constrained environments named Mobile LSD (M-LSD). M-LSD exploits extremely efficient LSD architecture and novel training schemes, including SoL augmentation and geometric learning scheme. Our model can run in real-time on GPU, CPU, and even on mobile devices.

效果

效果1

效果2

效果3

效果4

模型信息

Inputs
-------------------------
name:input_image_with_alpha:0
tensor:Float[1, 512, 512, 4]
---------------------------------------------------------------

Outputs
-------------------------
name:Identity
tensor:Int32[1, 200, 2]
name:Identity_1
tensor:Float[1, 200]
name:Identity_2
tensor:Float[1, 256, 256, 4]
---------------------------------------------------------------

项目

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;namespace Onnx_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 inpWidth;int inpHeight;Mat image;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;float conf_threshold = 0.5f;float dist_threshold = 20.0f;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/model_512x512_large.onnx";inpWidth = 512;inpHeight = 512;onnx_session = new InferenceSession(model_path, options);// 创建输入容器input_ontainer = new List<NamedOnnxValue>();image_path = "test_img/4.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;System.Windows.Forms.Application.DoEvents();image = new Mat(image_path);Mat resize_image = new Mat();Cv2.Resize(image, resize_image, new OpenCvSharp.Size(512, 512));float h_ratio = (float)image.Rows / 512;float w_ratio = (float)image.Cols / 512;int row = resize_image.Rows;int col = resize_image.Cols;float[] input_tensor_data = new float[1 * 4 * row * col];int k = 0;for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){for (int c = 0; c < 3; c++){float pix = ((byte*)(resize_image.Ptr(i).ToPointer()))[j * 3 + c];input_tensor_data[k] = pix;k++;}input_tensor_data[k] = 1;k++;}}input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 512, 512, 4 });//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input_image_with_alpha:0", input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);dt2 = DateTime.Now;//将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();int[] pts = results_onnxvalue[0].AsTensor<int>().ToArray();float[] pts_score = results_onnxvalue[1].AsTensor<float>().ToArray();float[] vmap = results_onnxvalue[2].AsTensor<float>().ToArray();List<List<int>> segments_list = new List<List<int>>();int num_lines = 200;int map_h = 256;int map_w = 256;for (int i = 0; i < num_lines; i++){int y = pts[i * 2];int x = pts[i * 2 + 1];float disp_x_start = vmap[0 + y * map_w * 4 + x * 4];float disp_y_start = vmap[1 + y * map_w * 4 + x * 4];float disp_x_end = vmap[2 + y * map_w * 4 + x * 4];float disp_y_end = vmap[3 + y * map_w * 4 + x * 4];float distance = (float)Math.Sqrt(Math.Pow(disp_x_start - disp_x_end, 2) + Math.Pow(disp_y_start - disp_y_end, 2));if (pts_score[i] > conf_threshold && distance > dist_threshold){float x_start = (x + disp_x_start) * 2 * w_ratio;float y_start = (y + disp_y_start) * 2 * h_ratio;float x_end = (x + disp_x_end) * 2 * w_ratio;float y_end = (y + disp_y_end) * 2 * h_ratio;List<int> line = new List<int>() { (int)x_start, (int)y_start, (int)x_end, (int)y_end };segments_list.Add(line);}}Mat result_image = image.Clone();for (int i = 0; i < segments_list.Count; i++){Cv2.Line(result_image, new OpenCvSharp.Point(segments_list[i][0], segments_list[i][1]), new OpenCvSharp.Point(segments_list[i][2], segments_list[i][3]), new Scalar(0, 0, 255), 3);}pictureBox2.Image = new System.Drawing.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# OpenCvSharp 图像校正_天天代码码天天的博客-CSDN博客

C# OpenCvSharp 透视变换(图像摆正)Demo-CSDN博客

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

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

相关文章

使用 maven 自动将源码打包并发布

maven-source-plugin 作用&#xff1a; 在构建过程中将项目的源代码进行打包&#xff0c;并作为一个jar文件附着在主构件上&#xff0c;在 pom.xml 中添加如下内容&#xff0c;使用 maven 生成 jar 的同时生成 sources 包 在 pom 中配置如下&#xff1a; <build><p…

Hive 查询优化

Hive 查询优化 -- 本地 set mapreduce.framework.namelocal; set hive.exec.mode.local.autotrue; set mapperd.job.trackerlocal; -- yarn set mapreduce.framework.nameyarn; set hive.exec.mode.local.autofalse; set mapperd.job.trackeryarn-- 向量模式 set hive.vectori…

最小二乘法及参数辨识

文章目录 一、最小二乘法1.1 定义1.2 SISO系统运用最小二乘估计进行辨识1.3 几何解释1.4 最小二乘法性质 二、加权最小二乘法三、递推最小二乘法四、增广最小二乘法 一、最小二乘法 1.1 定义 1974年高斯提出的最小二乘法的基本原理是未知量的最可能值是使各项实际观测值和计算…

[数据结构]—带头双向循环链表——超详解

&#x1f493;作者简介&#x1f389;&#xff1a;在校大二迷茫大学生 &#x1f496;个人主页&#x1f389;&#xff1a;小李很执着 &#x1f497;系列专栏&#x1f389;&#xff1a;数据结构 每日分享✨&#xff1a;旅行是为了迷路&#xff0c;迷路是为了遇上美好❣️❣️❣️ …

XoT:一种新的大语言模型的提示技术

这是微软在11月最新发布的一篇论文&#xff0c;题为“Everything of Thoughts: Defying the Law of Penrose Triangle for Thought Generation”&#xff0c;介绍了一种名为XOT的提示技术&#xff0c;它增强了像GPT-3和GPT-4这样的大型语言模型(llm)解决复杂问题的潜力。 当前提…

Spring底层原理学习笔记--第九讲--(aop之ajc增强)

AOP实现之ajc编译器 AOP的另一种实现及原理 A10Application.java package com.lucifer.itheima.a10;import com.lucifer.itheima.a10.service.MyService; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframewor…

如何让组织的KPI成为敏捷转型的推手而不是杀手 | IDCF

作者&#xff1a;IDCF学员 伍雪锋 某知名通讯公司首席敏捷教练&#xff0c;DevOps布道者。2020年到2021年小100人团队从0-1初步完成敏捷转型&#xff0c;专注传统制造业的IT转型&#xff0c;研发效能提升。 一、前言 在公司我们常常听见这么一个流传的故事&#xff0c;只要…

HCIA-经典综合实验(二)

经典综合实验&#xff08;二&#xff09; 实验拓扑配置步骤配置Eth-Trunk聚合链路第一步 配置二层VLAN第二步 配置MSTP生成树第三步 配置相关IP地址第四步 配置DHCP及DHCP中继第五步 配置三层的网关冗余协议 VRRP及OSPF第六步 配置静态路由,NAT地址转换及其他配置完善 配置验证…

Linux Ubuntu系统中添加磁盘

在学习与训练linux系统的磁盘概念、文件系统等&#xff0c;需要增加磁盘、扩展现有磁盘容量等&#xff0c;对于如何添加新的磁盘&#xff0c;我们在“Linux centos系统中添加磁盘”中对centos7/8版本中如何添加、查看、删除等&#xff0c;作了介绍&#xff0c;而对Ubuntu版本中…

解决k8s通过traefik暴露域名失败并报错:Connection Refused的问题

我敢说本篇文章是网上为数不多的解决traefik暴露域名失败问题的正确文章。 我看了网上太多讲述traefik夸夸其谈的文章了&#xff0c;包含一大堆复制粘贴的水文和还有什么所谓“阿里技术专家”的文章&#xff0c;讲的全都是错的&#xff01;基本没有一个能说到点子上去&#xf…

Python之函数进阶-递归函数

Python之函数进阶-递归函数 递归 函数直接或者间接调用自身就是 递归递归需要有边界条件、递归前进段、递归返回段递归一定要有边界条件当边界条件不满足的时候&#xff0c;递归前进当边界条件满足的时候&#xff0c;递归返回 递归要求 递归一定要有退出条件&#xff0c;递…

SQL 主从数据库实时备份

在SQL数据库中&#xff0c;主从复制&#xff08;Master-Slave Replication&#xff09;是一种常见的实时备份和高可用性解决方案。这种配置允许将一个数据库服务器&#xff08;主服务器&#xff09;的更改同步到一个或多个其他数据库服务器&#xff08;从服务器&#xff09;&am…

解决:element ui表格表头自定义输入框单元格el-input不能输入问题

表格表头如图所示&#xff0c;有 40-45&#xff0c;45-50 数据&#xff0c;且以输入框形式呈现&#xff0c;现想修改其数据或点击右侧加号增加新数据编辑。结果不能输入&#xff0c;部分代码如下 <template v-if"columnData.length > 0"><el-table-colu…

八股文-面向对象的理解

近年来&#xff0c;IT行业的环境相较以往显得有些严峻&#xff0c;因此一直以来&#xff0c;我都怀有一个愿望&#xff0c;希望能够创建一个分享面试经验的网站。由于个人有些懒惰&#xff0c;也较为喜欢玩乐&#xff0c;导致计划迟迟未能实现。然而&#xff0c;随着年底的临近…

智慧城市项目建设介绍

1. 项目建设背景 随着城市化进程的加速&#xff0c;城市发展面临着诸多挑战&#xff0c;如环境污染、城镇综合管理、经济发展布局等。为了应对这些挑战&#xff0c;智慧城市应运而生&#xff0c;成为城市发展的重要方向。智慧城市通过运用信息技术和智能化技术&#xff0c;实…

Qt UDP通信

UDP通信中单个套接字既是服务器又是客户端。 创建UDP套接字&#xff1a; QUdpSocket *udpSocket; udpSocketnew QUdpSocket(this); 绑定本地端口作为服务端口&#xff1a; udpSocket->bind(port)解除绑定udpSocket->abort(); 向指定ip和端口的主机发送数据报&#…

mmdetection安装与训练

一、什么是mmdetection 商汤科技&#xff08;2018 COCO 目标检测挑战赛冠军&#xff09;和香港中文大学最近开源了一个基于Pytorch实现的深度学习目标检测工具箱mmdetection&#xff0c;支持Faster-RCNN&#xff0c;Mask-RCNN&#xff0c;Fast-RCNN等主流的目标检测框架&#…

Linux 图形界面配置RAID

目录 RAID 1 配置 RAID 5配置 , RAID 配置起来要比 LVM 方便&#xff0c;因为它不像 LVM 那样分了物理卷、卷组和逻辑卷三层&#xff0c;而且每层都需要配置。我们在图形安装界面中配置 RAID 1和 RAID 5&#xff0c;先来看看 RAID 1 的配置方法。 RAID 1 配置 配置 RAID 1…

力扣学习笔记——283. 移动零

力扣学习笔记——283. 移动零 题目描述 https://leetcode.cn/problems/move-zeroes/description/?envTypestudy-plan-v2&envIdtop-100-liked 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &a…

Java架构师分布式搜索词库解决方案

目录 1 IK分词器字典热加载实现思路2 分析IK分词器的配置3 基于MySQL更新字典的实现4 常见报错4.1 java.lang.ExceptionInInitializerError: null …access denied (“java.lang.RuntimePermission” “setContextClassLoader”)4.2 java.sql.SQLNonTransientConnectionExcepti…