欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# Onnx實(shí)現(xiàn)輕量實(shí)時(shí)的M-LSD直線檢測(cè)

 更新時(shí)間:2023年11月15日 10:19:16   作者:天天代碼碼天天  
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合Onnx實(shí)現(xiàn)輕量實(shí)時(shí)的M-LSD直線檢測(cè),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

介紹

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]
---------------------------------------------------------------

項(xiàng)目

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)
        {
 
            // 創(chuàng)建輸入容器
            input_ontainer = new List<NamedOnnxValue>();
 
            // 創(chuàng)建輸出會(huì)話
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 設(shè)置為CPU上運(yùn)行
 
            // 創(chuàng)建推理模型類,讀取本地模型文件
            model_path = "model/model_512x512_large.onnx";
 
            inpWidth = 512;
            inpHeight = 512;
            onnx_session = new InferenceSession(model_path, options);
 
            // 創(chuàng)建輸入容器
            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 = "檢測(cè)中,請(qǐng)稍等……";
            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 放入一個(gè)輸入?yún)?shù)的容器,并指定名稱
            input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input_image_with_alpha:0", input_tensor));
 
            dt1 = DateTime.Now;
            //運(yùn)行 Inference 并獲取結(jié)果
            result_infer = onnx_session.Run(input_ontainer);
            dt2 = DateTime.Now;
 
            //將輸出結(jié)果轉(zhuǎn)為DisposableNamedOnnxValue數(shù)組
            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 = "推理耗時(shí):" + (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);
        }
    }
}

其他

結(jié)合透視變換可實(shí)現(xiàn)圖像校正,圖像校正參考

C#使用OpenCvSharp實(shí)現(xiàn)圖像校正

C#使用OpenCvSharp實(shí)現(xiàn)透視變換

以上就是C# Onnx實(shí)現(xiàn)輕量實(shí)時(shí)的M-LSD直線檢測(cè)的詳細(xì)內(nèi)容,更多關(guān)于C#直線檢測(cè)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法

    C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法,文中詳細(xì)描述了C#生成互不相同隨機(jī)數(shù)的各個(gè)步驟及所用到的函數(shù),非常具有借鑒價(jià)值,需要的朋友可以參考下
    2014-09-09
  • C#使用LINQ查詢操作符實(shí)例代碼(一)

    C#使用LINQ查詢操作符實(shí)例代碼(一)

    這篇文章介紹了C#使用LINQ查詢操作符的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 詳解從ObjectPool到CAS指令

    詳解從ObjectPool到CAS指令

    這篇文章主要介紹了從ObjectPool到CAS指令?,本文主要是帶大家看了下ObjectPool的源碼,然后看了看ObjectPool能實(shí)現(xiàn)無鎖線程安全的最大功臣Interlocked.CompareExchange方法,需要的朋友可以參考下
    2022-11-11
  • 關(guān)于C#中排序函數(shù)的總結(jié)

    關(guān)于C#中排序函數(shù)的總結(jié)

    下面小編就為大家?guī)硪黄P(guān)于C#中排序函數(shù)的總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • C# MemoryStream類案例詳解

    C# MemoryStream類案例詳解

    這篇文章主要介紹了C# MemoryStream類案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#如何通過QQ郵件發(fā)送驗(yàn)證碼到指定郵箱

    C#如何通過QQ郵件發(fā)送驗(yàn)證碼到指定郵箱

    在程序設(shè)計(jì)中發(fā)送驗(yàn)證碼是常見的一個(gè)功能,用戶在注冊(cè)賬號(hào)時(shí)或忘記密碼后通常需要發(fā)送驗(yàn)證碼到手機(jī)短信或郵箱來驗(yàn)證身份,這篇文章主要給大家介紹了關(guān)于C#如何通過QQ郵件發(fā)送驗(yàn)證碼到指定郵箱的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換

    C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換

    這篇文章介紹了C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#中String類常用方法匯總

    C#中String類常用方法匯總

    這篇文章主要介紹了C#中String類常用方法,較為詳細(xì)的匯總了String類中的常用方法,對(duì)于深入掌握C#字符串操作有著很好的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • 字符串轉(zhuǎn)換成枚舉類型的方法

    字符串轉(zhuǎn)換成枚舉類型的方法

    字符串可以向int, bool等類型轉(zhuǎn)變,但是字符串是否可以向枚舉轉(zhuǎn)變呢?一起看下邊的例子
    2014-01-01
  • C#探秘系列(三)——StackTrace,Trim

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

    這個(gè)系列我們看看C#中有哪些我們知道,但是又不知道怎么用,又或者懶得去了解的東西,比如這篇我們要介紹的StackTrace,Trim
    2014-05-05

最新評(píng)論