午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

C# OpenCvSharp DNN UNet 推理

 吳敬銳 2024-11-17

效果

圖片

模型

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

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

項(xiàng)目

圖片

代碼

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), truefalse);
 
            //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格式數(shù)據(jù),大小為[1,2, 256, 256]
            //首先將他reshape,設(shè)置成一通道,512行,256列,其中前256行與后256行是互補(bǔ)關(guān)系,對應(yīng)位置相加都為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]
---------------------------------------------------------------
 */

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多