C# OpenVino Yolov8 Seg 分割

效果

项目

代码

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;

namespace OpenVino_Yolov8_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        String startupPath;
        string classer_path;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        String model_path;
        Core core;
        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);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            // 配置图片数据
            Mat image = new Mat(image_path);
            int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));

            float[] det_result_array = new float[8400 * 116];
            float[] proto_result_array = new float[32 * 160 * 160];
            float[] factors = new float[4];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);
            factors[2] = image.Rows;
            factors[3] = image.Cols;

            byte[] image_data = max_image.ImEncode(".bmp");
            //存储byte的长度
            ulong image_size = Convert.ToUInt64(image_data.Length);
            // 加载推理图片数据
            core.load_input_data("images", image_data, image_size, 1);

            dt1 = DateTime.Now;
            // 模型推理
            core.infer();
            dt2 = DateTime.Now;

            // 读取推理结果
            det_result_array = core.read_infer_result<float>("output0", 8400 * 116);
            proto_result_array = core.read_infer_result<float>("output1", 32 * 160 * 160);

            SegmentationResult result_pro= new SegmentationResult(classer_path, factors);
            Mat result_image = result_pro.draw_result(result_pro.process_result(det_result_array, proto_result_array), image.Clone());

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = startupPath + "\\yolov8n-seg.onnx";
            core = new Core(model_path, "CPU");
            classer_path = "yolov8-detect-lable.txt";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            core.delet();
        }
    }
}

完整Demo下载

相关推荐
GISer_Jing1 小时前
AI自动化工作流:智能驱动未来(升级研究生项目!!!)
人工智能·前端框架·自动化
草捏子1 小时前
Agent Skills:让 AI 一次学会、永远记住的能力扩展方案
人工智能
NocoBase1 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
后端小肥肠1 小时前
OpenClaw实战|从识图到公众号内容自动化,我跑通了完整链路
人工智能·aigc·agent
Elastic 中国社区官方博客1 小时前
快速 vs. 准确:衡量量化向量搜索的召回率
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
qq_381338501 小时前
【技术日报】2026-03-18 AI 领域重磅速递
大数据·人工智能
NocoBase2 小时前
开源项目管理工具选型指南(2026年最新)
人工智能·开源·无代码
feasibility.2 小时前
AI 爬虫高手养成:Openclaw+Scrapling 手动部署 + 采集策略(以Walmart 电商平台为例)
人工智能·爬虫·科技·机器人·agi·openclaw·scrapling
程序员老猫2 小时前
前端菜鸡狂喜!DeepSeek+Gemini,嘴炮出完整博客方案
人工智能
AI周红伟3 小时前
周红伟:《OpenClaw安全防控:OpenClaw++Skills智能体安全部署、实操和企业应用实操》
人工智能·阿里云·云计算·腾讯云·openclaw