C# Onnx实现DIS高精度图像二类分割

 更新时间:2023年11月21日 09:05:33   作者:天天代码码天天  
这篇文章主要为大家详细介绍了C# Onnx实现DIS高精度图像二类分割的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

介绍

github地址:https://github.com/xuebinqin/DIS

This is the repo for our new project Highly Accurate Dichotomous Image Segmentation

对应的paper是ECCV2022的一篇文章《Highly Accurate Dichotomous Image Segmentation》, 跟BASNet和U2-Net都是出自同一个作者写的。 

效果

模型信息

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

Outputs
-------------------------
name:output
tensor:Float[1, 1, 480, 640]
---------------------------------------------------------------

项目

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;
 
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;
 
        int outHeight, outWidth;
 
        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;
 
        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/isnet_general_use_480x640.onnx";
 
            inpHeight = 480;
            inpWidth = 640;
 
            outHeight = 480;
            outWidth = 640;
 
            onnx_session = new InferenceSession(model_path, options);
 
            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();
 
            image_path = "test_img/bike.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(inpWidth, inpHeight));
 
            float[] input_tensor_data = new float[1 * 3 * 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*)(resize_image.Ptr(i).ToPointer()))[j * 3 + 2 - c];
                        input_tensor_data[c * inpHeight * inpWidth + i * inpWidth + j] = (float)(pix / 255.0 - 0.5);
                    }
                }
            }
 
            input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth });
 
            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));
 
            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_ontainer);
            dt2 = DateTime.Now;
 
            //将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();
 
            float[] pred = results_onnxvalue[0].AsTensor<float>().ToArray();
 
            Mat mask = new Mat(outHeight, outWidth, MatType.CV_32FC1, pred);
            double min_value, max_value;
            Cv2.MinMaxLoc(mask, out min_value, out max_value);
 
            mask = (mask - min_value) / (max_value - min_value);
 
            mask *= 255;
            mask.ConvertTo(mask, MatType.CV_8UC1);
 
            Cv2.Resize(mask, mask, new OpenCvSharp.Size(image.Cols, image.Rows));
 
            Mat result_image = mask.Clone();
 
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
 
            pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
 
            mask.Dispose();
            image.Dispose();
            resize_image.Dispose();
            result_image.Dispose();
        }
 
        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# Onnx实现DIS高精度图像二类分割的文章就介绍到这了,更多相关C# Onnx图像二类分割内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#把数字转换成大写金额的代码实例

    C#把数字转换成大写金额的代码实例

    这篇文章主要介绍了C#把数字转换成大写金额的代码实例,例如把200转换成“贰佰元”,需要的朋友可以参考下
    2014-05-05
  • c#使用IAsyncEnumerable实现流式分段传输

    c#使用IAsyncEnumerable实现流式分段传输

    这篇文章主要为大家详细介绍了c#如何使用IAsyncEnumerable实现流式分段传输,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • 详解WPF如何使用WriteableBitmap提升Image性能

    详解WPF如何使用WriteableBitmap提升Image性能

    这篇文章主要为大家详细介绍了WPF如何使用WriteableBitmap提升Image性能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • C#简单发送email的方法

    C#简单发送email的方法

    这篇文章主要介绍了C#简单发送email的方法,涉及C#发送Email的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • 基于WPF实现面包屑控件的示例代码

    基于WPF实现面包屑控件的示例代码

    这篇文章主要为大家详细介绍了如何基于WPF实现简单的面包屑控件,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2023-05-05
  • c#中LINQ的基本用法实例

    c#中LINQ的基本用法实例

    语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中引入的一项创新功能。下面这篇文章主要给大家介绍了关于c#中LINQ的基本用法,需要的朋友可以参考借鉴,下面来一起看看吧
    2019-01-01
  • Winform让DataGridView左侧显示图片

    Winform让DataGridView左侧显示图片

    本文主要介绍在如何让DataGridView左侧显示图片,这里主要讲解重写DataGridView的OnRowPostPaint方法,需要的朋友可以参考下。
    2016-05-05
  • C# 将程序添加开机启动的三种方式

    C# 将程序添加开机启动的三种方式

    本文主要介绍了C# 将程序添加开机启动的三种方式,主要包含开始菜单启动,注册表启动项和Windows 计划任务这三种方法,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • C# XmlDocument操作XML案例详解

    C# XmlDocument操作XML案例详解

    这篇文章主要介绍了C# XmlDocument操作XML案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C#和lua相互调用的方法教程

    C#和lua相互调用的方法教程

    lua是一种脚本语言,可以方便的移植到各种宿主语言中,并且可以支持热更新,在游戏开发中也能当做主要的语言来编写游戏的逻辑,所以这篇文章主要给大家介绍了关于C#和lua相互调用的方法教程,需要的朋友可以参考下。
    2017-11-11

最新评论