C# Onnx segment-anything 分割万物 一键抠图,C Onnx实现一键抠图,万物皆可分割的功能

马肤

温馨提示:这篇文章已超过424天没有更新,请注意相关的内容是否还可用!

摘要:C#利用Onnx技术实现图像分割,通过"segment-anything 分割万物"的功能,实现一键抠图。该技术能够轻松应对各种图像分割需求,无论是复杂的背景还是精细的细节,都能准确识别并分割。这一创新技术为图像编辑和处理带来便捷,提升效率,推动图像分割领域的发展。

目录

介绍

效果

模型信息

sam_vit_b_decoder.onnx

sam_vit_b_encoder.onnx

项目

代码

下载


C# Onnx segment-anything 分割万物 一键抠图

介绍

github地址:https://github.com/facebookresearch/segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model. 

C# Onnx segment-anything 分割万物 一键抠图,C Onnx实现一键抠图,万物皆可分割的功能 第1张

效果

C# Onnx segment-anything 分割万物

C# Onnx segment-anything 分割万物 一键抠图,C Onnx实现一键抠图,万物皆可分割的功能 第2张

模型信息

sam_vit_b_decoder.onnx

Model Properties

-------------------------

---------------------------------------------------------------

Inputs

-------------------------

name:image_embeddings

tensor:Float[1, 256, 64, 64]

name:point_coords

tensor:Float[1, -1, 2]

name:point_labels

tensor:Float[1, -1]

name:mask_input

tensor:Float[1, 1, 256, 256]

name:has_mask_input

tensor:Float[1]

name:orig_im_size

tensor:Float[2]

---------------------------------------------------------------

Outputs

-------------------------

name:masks

tensor:Float[-1, -1, -1, -1]

name:iou_predictions

tensor:Float[-1, 4]

name:low_res_masks

tensor:Float[-1, -1, -1, -1]

---------------------------------------------------------------

sam_vit_b_encoder.onnx

Model Properties

-------------------------

---------------------------------------------------------------

Inputs

-------------------------

name:image

tensor:Float[1, 3, 1024, 1024]

---------------------------------------------------------------

Outputs

-------------------------

name:image_embeddings

tensor:Float[1, 256, 64, 64]

---------------------------------------------------------------

项目

C# Onnx segment-anything 分割万物 一键抠图,C Onnx实现一键抠图,万物皆可分割的功能 第3张

代码

using OpenCvSharp;

using OpenCvSharp.Extensions;

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Imaging;

using System.Reflection;

using System.Windows.Forms;

namespace Onnx_Demo

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

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

        string image_path = "";

        DateTime dt1 = DateTime.Now;

        DateTime dt2 = DateTime.Now;

        Mat image;

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = fileFilter;

            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;

            image_path = ofd.FileName;

            pictureBox1.Image = new Bitmap(image_path);

            textBox1.Text = "";

            image = new Mat(image_path);

            pictureBox2.Image = null;

            pictureBox3.Image = null;

            sam.SetImage(image_path);

            image = new Mat(image_path);

            pictureBox1.Image = new Bitmap(image_path);

        }

        private void button2_Click(object sender, EventArgs e)

        {

            pictureBox3.Image = null;

            button2.Enabled = false;

            Application.DoEvents();

            List masks_list = sam.GetPointMask(roi);

            if (masks_list.Count>0)

            {

                int MaxInde = 0;

                float maxiou_pred = masks_list[0].iou_pred;

                for (int i = 0; i

                {

                    float temp = masks_list[i].iou_pred;

                    if (temp > maxiou_pred)

                    {

                        MaxInde = i;

                    }

                }

                pictureBox3.Image = BitmapConverter.ToBitmap(masks_list[MaxInde].mask);

            }

            button2.Enabled = true;

        }

        SamInferenceSession sam;

        private void Form1_Load(object sender, EventArgs e)

        {

            string encoderPath = "model/sam_vit_b_encoder.onnx";

            string decoderPath = "model/sam_vit_b_decoder.onnx";

            sam = new SamInferenceSession(encoderPath, decoderPath);

            sam.Initialize();

            image_path = "test_img/test.jpg";

            sam.SetImage(image_path);

            image = new Mat(image_path);

            pictureBox1.Image = new Bitmap(image_path);

            

        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)

        {

            Common.ShowNormalImg(pictureBox1.Image);

        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)

        {

            Common.ShowNormalImg(pictureBox2.Image);

        }

        SaveFileDialog sdf = new SaveFileDialog();

        private void button3_Click(object sender, EventArgs e)

        {

            if (pictureBox2.Image == null)

            {

                return;

            }

            Bitmap output = new Bitmap(pictureBox2.Image);

            sdf.Title = "保存";

            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";

            if (sdf.ShowDialog() == DialogResult.OK)

            {

                switch (sdf.FilterIndex)

                {

                    case 1:

                        {

                            output.Save(sdf.FileName, ImageFormat.Jpeg);

                            break;

                        }

                    case 2:

                        {

                            output.Save(sdf.FileName, ImageFormat.Png);

                            break;

                        }

                    case 3:

                        {

                            output.Save(sdf.FileName, ImageFormat.Bmp);

                            break;

                        }

                    case 4:

                        {

                            output.Save(sdf.FileName, ImageFormat.Emf);

                            break;

                        }

                    case 5:

                        {

                            output.Save(sdf.FileName, ImageFormat.Exif);

                            break;

                        }

                    case 6:

                        {

                            output.Save(sdf.FileName, ImageFormat.Gif);

                            break;

                        }

                    case 7:

                        {

                            output.Save(sdf.FileName, ImageFormat.Icon);

                            break;

                        }

                    case 8:

                        {

                            output.Save(sdf.FileName, ImageFormat.Tiff);

                            break;

                        }

                    case 9:

                        {

                            output.Save(sdf.FileName, ImageFormat.Wmf);

                            break;

                        }

                }

                MessageBox.Show("保存成功,位置:" + sdf.FileName);

            }

        }

        bool m_mouseDown = false;

        bool m_mouseMove = false;

        System.Drawing.Point startPoint = new System.Drawing.Point();

        System.Drawing.Point endPoint = new System.Drawing.Point();

        Mat currentFrame = new Mat();

        Rect roi = new Rect();

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

            if (pictureBox1.Image == null)

                return;

            if (!m_mouseDown) return;

            m_mouseMove = true;

            endPoint = e.Location;

            pictureBox1.Invalidate();

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)

        {

            if (!m_mouseDown || !m_mouseMove)

                return;

            Graphics g = e.Graphics;

            Pen p = new Pen(Color.Blue, 2);

            Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));

            g.DrawRectangle(p, rect);

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

        {

            if (!m_mouseDown || !m_mouseMove)

                return;

            m_mouseDown = false;

            m_mouseMove = false;

            System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);

            System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);

            if (image_startPoint.X

                image_startPoint.X = 0;

            if (image_startPoint.Y

                image_startPoint.Y = 0;

            if (image_endPoint.X

                image_endPoint.X = 0;

            if (image_endPoint.Y

                image_endPoint.Y = 0;

            if (image_startPoint.X > image.Cols)

                image_startPoint.X = image.Cols;

            if (image_startPoint.Y > image.Rows)

                image_startPoint.Y = image.Rows;

            if (image_endPoint.X > image.Cols)

                image_endPoint.X = image.Cols;

            if (image_endPoint.Y > image.Rows)

                image_endPoint.Y = image.Rows;

            label1.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);

            int w = (image_endPoint.X - image_startPoint.X);

            int h = (image_endPoint.Y - image_startPoint.Y);

            if (w > 10 && h > 10)

            {

                roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);

                //Console.WriteLine(String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y));

                //test

                //OpenCvSharp.Point pointinfo = new OpenCvSharp.Point(910, 641);

                //roi = new Rect(pointinfo.X - 160, pointinfo.Y - 430, 380, 940);

                Mat roi_mat = image[roi];

                pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);

            }

            //pictureBox1.Invalidate();

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            if (pictureBox1.Image == null)

                return;

            m_mouseDown = true;

            startPoint = e.Location;

        }

        private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)

        {

            System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);

            Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);

            int zoomedWidth = pictureBox.Width;

            int zoomedHeight = pictureBox.Height;

            int imageWidth = pictureBox1.Image.Width;

            int imageHeight = pictureBox1.Image.Height;

            double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);

            double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);

            int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;

            int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;

            int zoomedX = point.X - black_left_width;

            int zoomedY = point.Y - black_top_height;

            System.Drawing.Point outPoint = new System.Drawing.Point();

            outPoint.X = (int)((double)zoomedX / zoomRatex);

            outPoint.Y = (int)((double)zoomedY / zoomRatey);

            return outPoint;

        }

    }

}

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Windows.Forms;
namespace Onnx_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        Mat image;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
            pictureBox3.Image = null;
            sam.SetImage(image_path);
            image = new Mat(image_path);
            pictureBox1.Image = new Bitmap(image_path);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox3.Image = null;
            button2.Enabled = false;
            Application.DoEvents();
            List masks_list = sam.GetPointMask(roi);
            if (masks_list.Count>0)
            {
                int MaxInde = 0;
                float maxiou_pred = masks_list[0].iou_pred;
                for (int i = 0; i  maxiou_pred)
                    {
                        MaxInde = i;
                    }
                }
                pictureBox3.Image = BitmapConverter.ToBitmap(masks_list[MaxInde].mask);
            }
            button2.Enabled = true;
        }
        SamInferenceSession sam;
        private void Form1_Load(object sender, EventArgs e)
        {
            string encoderPath = "model/sam_vit_b_encoder.onnx";
            string decoderPath = "model/sam_vit_b_decoder.onnx";
            sam = new SamInferenceSession(encoderPath, decoderPath);
            sam.Initialize();
            image_path = "test_img/test.jpg";
            sam.SetImage(image_path);
            image = new Mat(image_path);
            pictureBox1.Image = new Bitmap(image_path);
            
        }
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }
        SaveFileDialog sdf = new SaveFileDialog();
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }
                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
        bool m_mouseDown = false;
        bool m_mouseMove = false;
        System.Drawing.Point startPoint = new System.Drawing.Point();
        System.Drawing.Point endPoint = new System.Drawing.Point();
        Mat currentFrame = new Mat();
        Rect roi = new Rect();
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            if (!m_mouseDown) return;
            m_mouseMove = true;
            endPoint = e.Location;
            pictureBox1.Invalidate();
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 2);
            Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
            g.DrawRectangle(p, rect);
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            m_mouseDown = false;
            m_mouseMove = false;
            System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
            System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
            if (image_startPoint.X  image.Cols)
                image_startPoint.X = image.Cols;
            if (image_startPoint.Y > image.Rows)
                image_startPoint.Y = image.Rows;
            if (image_endPoint.X > image.Cols)
                image_endPoint.X = image.Cols;
            if (image_endPoint.Y > image.Rows)
                image_endPoint.Y = image.Rows;
            label1.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
            int w = (image_endPoint.X - image_startPoint.X);
            int h = (image_endPoint.Y - image_startPoint.Y);
            if (w > 10 && h > 10)
            {
                roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);
                //Console.WriteLine(String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y));
                //test
                //OpenCvSharp.Point pointinfo = new OpenCvSharp.Point(910, 641);
                //roi = new Rect(pointinfo.X - 160, pointinfo.Y - 430, 380, 940);
                Mat roi_mat = image[roi];
                pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
            }
            //pictureBox1.Invalidate();
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            m_mouseDown = true;
            startPoint = e.Location;
        }
        private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
        {
            System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);
            int zoomedWidth = pictureBox.Width;
            int zoomedHeight = pictureBox.Height;
            int imageWidth = pictureBox1.Image.Width;
            int imageHeight = pictureBox1.Image.Height;
            double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
            double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
            int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
            int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;
            int zoomedX = point.X - black_left_width;
            int zoomedY = point.Y - black_top_height;
            System.Drawing.Point outPoint = new System.Drawing.Point();
            outPoint.X = (int)((double)zoomedX / zoomRatex);
            outPoint.Y = (int)((double)zoomedY / zoomRatey);
            return outPoint;
        }
    }
}

下载

源码下载

 


0
收藏0
文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。

相关阅读

  • 【研发日记】Matlab/Simulink自动生成代码(二)——五种选择结构实现方法,Matlab/Simulink自动生成代码的五种选择结构实现方法(二),Matlab/Simulink自动生成代码的五种选择结构实现方法详解(二)
  • 超级好用的C++实用库之跨平台实用方法,跨平台实用方法的C++实用库超好用指南,C++跨平台实用库使用指南,超好用实用方法集合,C++跨平台实用库超好用指南,方法与技巧集合
  • 【动态规划】斐波那契数列模型(C++),斐波那契数列模型(C++实现与动态规划解析),斐波那契数列模型解析与C++实现(动态规划)
  • 【C++】,string类底层的模拟实现,C++中string类的模拟底层实现探究
  • uniapp 小程序实现微信授权登录(前端和后端),Uniapp小程序实现微信授权登录全流程(前端后端全攻略),Uniapp小程序微信授权登录全流程攻略,前端后端全指南
  • Vue脚手架的安装(保姆级教程),Vue脚手架保姆级安装教程,Vue脚手架保姆级安装指南,Vue脚手架保姆级安装指南,从零开始教你如何安装Vue脚手架
  • 如何在树莓派 Raspberry Pi中本地部署一个web站点并实现无公网IP远程访问,树莓派上本地部署Web站点及无公网IP远程访问指南,树莓派部署Web站点及无公网IP远程访问指南,本地部署与远程访问实践,树莓派部署Web站点及无公网IP远程访问实践指南,树莓派部署Web站点及无公网IP远程访问实践指南,本地部署与远程访问详解,树莓派部署Web站点及无公网IP远程访问实践详解,本地部署与远程访问指南,树莓派部署Web站点及无公网IP远程访问实践详解,本地部署与远程访问指南。
  • vue2技术栈实现AI问答机器人功能(流式与非流式两种接口方法),Vue2技术栈实现AI问答机器人功能,流式与非流式接口方法探究,Vue2技术栈实现AI问答机器人功能,流式与非流式接口方法详解
  • 发表评论

    快捷回复:表情:
    评论列表 (暂无评论,0人围观)

    还没有评论,来说两句吧...

    目录[+]

    取消
    微信二维码
    微信二维码
    支付宝二维码