C# OpenCvSharp DNN UNet 推理

时间:2024-11-02 11:55:13

目录

效果

模型

项目

代码

下载


效果

模型

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

下载

源码下载