当前位置: 首页 > article >正文

C# OpenCvSharp DNN UNet 推理

目录

效果

模型

项目

代码

下载


效果

模型

Inputs
-------------------------
name:data
tensor:Float[1, 3, 256, 256]
---------------------------------------------------------------

Outputs
-------------------------
name:predict
tensor:Float[1, 2, 256, 256]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_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;

        string modelpath;

        int inpHeight;
        int inpWidth;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_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;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string modelTxt = "model/unet.prototxt";
            string modelBin = "model/unet.caffemodel";

            inpHeight = 256;
            inpWidth = 256;

            opencv_net = CvDnn.ReadNetFromCaffe(modelTxt, modelBin);

            image_path = "test_img/person.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;
            Application.DoEvents();

            Mat src = new Mat(image_path);

            int max_image_length = src.Cols > src.Rows ? src.Cols : src.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, src.Cols, src.Rows);
            src.CopyTo(new Mat(max_image, roi));

            Mat resize_image = max_image.Resize(new OpenCvSharp.Size(256, 256));

            BN_image = CvDnn.BlobFromImage(resize_image, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(127.5, 127.5, 127.5), true, false);

            //float* ptr = (float*)BN_image.Data;
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(ptr[i]);
            //}

            opencv_net.SetInput(BN_image, "data");


            dt1 = DateTime.Now;

            Mat detection = opencv_net.Forward("predict");


            //float* ptr2 = (float*)detection.Data;
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(ptr2[i]);
            //}

            dt2 = DateTime.Now;

            //得到的输出是一个四维的mat格式数据,大小为[1,2, 256, 256]
            //首先将他reshape,设置成一通道,512行,256列,其中前256行与后256行是互补关系,对应位置相加都为1
            //前256行为背景的概率,后256行为人像的概率
            Mat newMat = detection.Reshape(1, 512);
            //获取人像概率矩阵
            newMat = newMat.RowRange(256, 512);

            Mat result = new Mat();
            newMat.ConvertTo(result, MatType.CV_8U, 255.0);

            Cv2.Threshold(result, result, 127, 255, ThresholdTypes.Binary);

            Mat result2 = Mat.Zeros(256, 256, MatType.CV_8UC3) * 255;

            resize_image.CopyTo(result2, result);

            Cv2.ImShow("黑白", result);
            Cv2.ImShow("扣取", result2);

            pictureBox2.Image = new Bitmap(result2.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);
        }
    }
}


/*
Inputs
-------------------------
name:data
tensor:Float[1, 3, 256, 256]
---------------------------------------------------------------

Outputs
-------------------------
name:predict
tensor:Float[1, 2, 256, 256]
---------------------------------------------------------------
 */

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_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;

        string modelpath;

        int inpHeight;
        int inpWidth;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_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;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string modelTxt = "model/unet.prototxt";
            string modelBin = "model/unet.caffemodel";

            inpHeight = 256;
            inpWidth = 256;

            opencv_net = CvDnn.ReadNetFromCaffe(modelTxt, modelBin);

            image_path = "test_img/person.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;
            Application.DoEvents();

            Mat src = new Mat(image_path);

            int max_image_length = src.Cols > src.Rows ? src.Cols : src.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, src.Cols, src.Rows);
            src.CopyTo(new Mat(max_image, roi));

            Mat resize_image = max_image.Resize(new OpenCvSharp.Size(256, 256));

            BN_image = CvDnn.BlobFromImage(resize_image, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(127.5, 127.5, 127.5), true, false);

            //float* ptr = (float*)BN_image.Data;
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(ptr[i]);
            //}

            opencv_net.SetInput(BN_image, "data");


            dt1 = DateTime.Now;

            Mat detection = opencv_net.Forward("predict");


            //float* ptr2 = (float*)detection.Data;
            //for (int i = 0; i < 10; i++)
            //{
            //    Console.WriteLine(ptr2[i]);
            //}

            dt2 = DateTime.Now;

            //得到的输出是一个四维的mat格式数据,大小为[1,2, 256, 256]
            //首先将他reshape,设置成一通道,512行,256列,其中前256行与后256行是互补关系,对应位置相加都为1
            //前256行为背景的概率,后256行为人像的概率
            Mat newMat = detection.Reshape(1, 512);
            //获取人像概率矩阵
            newMat = newMat.RowRange(256, 512);

            Mat result = new Mat();
            newMat.ConvertTo(result, MatType.CV_8U, 255.0);

            Cv2.Threshold(result, result, 127, 255, ThresholdTypes.Binary);

            Mat result2 = Mat.Zeros(256, 256, MatType.CV_8UC3) * 255;

            resize_image.CopyTo(result2, result);

            Cv2.ImShow("黑白", result);
            Cv2.ImShow("扣取", result2);

            pictureBox2.Image = new Bitmap(result2.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);
        }
    }
}


/*
Inputs
-------------------------
name:data
tensor:Float[1, 3, 256, 256]
---------------------------------------------------------------

Outputs
-------------------------
name:predict
tensor:Float[1, 2, 256, 256]
---------------------------------------------------------------
 */

下载

源码下载


http://www.kler.cn/a/371470.html

相关文章:

  • 批量剪辑视频软件源码搭建全解析,支持OEM
  • 华为手机卸载系统应用的方法
  • 软件测试学习笔记丨Selenium学习笔记:css定位
  • LeetCode15:三数之和
  • Kafka认证时Successfully logged in真的认证成功了?
  • Ubuntu中VSCode以sudo开始GDB调试C程序方法
  • 华为手机系统应用瘦身
  • 了解桌面机床用于学校教学培训应用-桌面级CNC机床
  • Debug日程工作经验总结日程常用
  • 五指cms安装
  • ubuntu20.04系统安装
  • 使用Python和OpenCV实现火焰检测
  • c++ assert
  • [机器学习]集成学习
  • Docker架构
  • [论文阅读]SimCSE: Simple Contrastive Learning of Sentence Embeddings
  • Topaz Video AI for Mac 视频无损放大软件安装教程【保姆级,操作简单轻松上手】
  • 《ToDesk 云电脑、易腾云、青椒云移动端体验实测:让手机秒变超级电脑》
  • 鸿蒙NEXT应用上架与分发
  • 沧穹科技室内音频“北斗”定位技术亮相第三届北斗规模应用国际峰会
  • 面试题整理1
  • Flume采集Kafka数据到Hive
  • 【力扣 + 牛客 | SQL题 | 每日4题】牛客SQL热题210,213,212,219
  • PART 1 数据挖掘概论 — 数据挖掘方法论
  • H.264视频压缩与MP4/MKV封装格式的详尽指南
  • 【C++】入门C++