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

 更新时间:2023年11月15日 10:19:16   作者:天天代码码天天  
这篇文章主要为大家详细介绍了C#如何结合Onnx实现轻量实时的M-LSD直线检测,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

介绍

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实现图像校正

C#使用OpenCvSharp实现透视变换

以上就是C# Onnx实现轻量实时的M-LSD直线检测的详细内容,更多关于C#直线检测的资料请关注脚本之家其它相关文章!

相关文章

  • C#生成互不相同随机数的实现方法

    C#生成互不相同随机数的实现方法

    这篇文章主要介绍了C#生成互不相同随机数的实现方法,文中详细描述了C#生成互不相同随机数的各个步骤及所用到的函数,非常具有借鉴价值,需要的朋友可以参考下
    2014-09-09
  • C#使用LINQ查询操作符实例代码(一)

    C#使用LINQ查询操作符实例代码(一)

    这篇文章介绍了C#使用LINQ查询操作符的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 详解从ObjectPool到CAS指令

    详解从ObjectPool到CAS指令

    这篇文章主要介绍了从ObjectPool到CAS指令 ,本文主要是带大家看了下ObjectPool的源码,然后看了看ObjectPool能实现无锁线程安全的最大功臣Interlocked.CompareExchange方法,需要的朋友可以参考下
    2022-11-11
  • 关于C#中排序函数的总结

    关于C#中排序函数的总结

    下面小编就为大家带来一篇关于C#中排序函数的总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • C# MemoryStream类案例详解

    C# MemoryStream类案例详解

    这篇文章主要介绍了C# MemoryStream类案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C#如何通过QQ邮件发送验证码到指定邮箱

    C#如何通过QQ邮件发送验证码到指定邮箱

    在程序设计中发送验证码是常见的一个功能,用户在注册账号时或忘记密码后通常需要发送验证码到手机短信或邮箱来验证身份,这篇文章主要给大家介绍了关于C#如何通过QQ邮件发送验证码到指定邮箱的相关资料,需要的朋友可以参考下
    2024-01-01
  • C#中泛型举例List<T>与DataTable相互转换

    C#中泛型举例List<T>与DataTable相互转换

    这篇文章介绍了C#中泛型举例List<T>与DataTable相互转换的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#中String类常用方法汇总

    C#中String类常用方法汇总

    这篇文章主要介绍了C#中String类常用方法,较为详细的汇总了String类中的常用方法,对于深入掌握C#字符串操作有着很好的学习借鉴价值,需要的朋友可以参考下
    2014-11-11
  • 字符串转换成枚举类型的方法

    字符串转换成枚举类型的方法

    字符串可以向int, bool等类型转变,但是字符串是否可以向枚举转变呢?一起看下边的例子
    2014-01-01
  • C#探秘系列(三)——StackTrace,Trim

    C#探秘系列(三)——StackTrace,Trim

    这个系列我们看看C#中有哪些我们知道,但是又不知道怎么用,又或者懒得去了解的东西,比如这篇我们要介绍的StackTrace,Trim
    2014-05-05

最新评论