C# OpenCvSharp DNN HybridNets 同时处理车辆检测、可驾驶区域分割、车道线分割

效果

C# OpenCvSharp DNN HybridNets 同时处理车辆检测、可驾驶区域分割、车道线分割_第1张图片

项目

C# OpenCvSharp DNN HybridNets 同时处理车辆检测、可驾驶区域分割、车道线分割_第2张图片

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
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;

        float confThreshold;
        float nmsThreshold;
        string modelpath;
        string anchorpath;

        int inpHeight;
        int inpWidth;

        float[] mean = { 0.485f, 0.456f, 0.406f };
        float[] std = { 0.229f, 0.224f, 0.225f };

        List det_class_names = new List() { "car" };
        List seg_class_names = new List() { "Background", "Lane", "Line" };
        List class_colors = new List { new Vec3b(0, 0, 0), new Vec3b(0, 255, 0), new Vec3b(255, 0, 0) };

        int det_num_class = 1;
        int seg_numclass = 3;

        float[] anchors;

        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)
        {

            confThreshold = 0.3f;
            nmsThreshold = 0.5f;
            modelpath = "model/hybridnets_256x384.onnx";
            anchorpath = "model/anchors_73656.bin";

            inpHeight = 256;
            inpWidth = 384;

            opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

            FileStream fileStream = new FileStream(anchorpath, FileMode.Open);
            //读二进制文件类
            BinaryReader br = new BinaryReader(fileStream, Encoding.UTF8);
            int len = 73656;
            anchors = new float[len];

            byte[] byteTemp;
            float fTemp;
            for (int i = 0; i < len; i++)
            {
                byteTemp = br.ReadBytes(4);
                fTemp = BitConverter.ToSingle(byteTemp, 0);
                anchors[i] = fTemp;
            }
            br.Close();

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

            image = new Mat(image_path);

            int newh = 0, neww = 0, padh = 0, padw = 0;
            Mat resize_img = Common.ResizeImage(image, inpHeight, inpWidth, ref newh, ref neww, ref padh, ref padw);

            float ratioh = (float)image.Rows / newh;
            float ratiow = (float)image.Cols / neww;

            Mat normalize = Common.Normalize(resize_img, mean, std);

            dt1 = DateTime.Now;

            BN_image = CvDnn.BlobFromImage(normalize);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            opencv_net.Forward(outs, outBlobNames);

            dt2 = DateTime.Now;

            float* classification = (float*)outs[0].Data;
            float* box_regression = (float*)outs[1].Data;
            float* seg = (float*)outs[2].Data;

            List boxes = new List();
            List confidences = new List();
            List classIds = new List();

            int num_proposal = outs[1].Size(1);  //输入的是单张图, 第0维batchsize忽略

            for (int n = 0; n < num_proposal; n++)
            {
                float conf = classification[n];

                if (conf > confThreshold)
                {
                    int row_ind = n * 4;
                    float x_centers = box_regression[row_ind + 1] * anchors[row_ind + 2] + anchors[row_ind];
                    float y_centers = box_regression[row_ind] * anchors[row_ind + 3] + anchors[row_ind + 1];
                    float w = (float)(Math.Exp(box_regression[row_ind + 3]) * anchors[row_ind + 2]);
                    float h = (float)(Math.Exp(box_regression[row_ind + 2]) * anchors[row_ind + 3]);

                    float xmin = (float)((x_centers - w * 0.5 - padw) * ratiow);
                    float ymin = (float)((y_centers - h * 0.5 - padh) * ratioh);
                    w *= ratiow;
                    h *= ratioh;
                    Rect box = new Rect((int)xmin, (int)ymin, (int)w, (int)h);
                    boxes.Add(box);
                    confidences.Add(conf);
                    classIds.Add(0);
                }

            }

            int[] indices;
            CvDnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, out indices);

            result_image = image.Clone();

            for (int ii = 0; ii < indices.Length; ++ii)
            {
                int idx = indices[ii];
                Rect box = boxes[idx];
                Cv2.Rectangle(result_image, new OpenCvSharp.Point(box.X, box.Y), new OpenCvSharp.Point(box.X + box.Width, box.Y + box.Height), new Scalar(0, 0, 255), 2);
                string label = det_class_names[classIds[idx]] + ":" + confidences[idx].ToString("0.00");
                Cv2.PutText(result_image, label, new OpenCvSharp.Point(box.X, box.Y - 5), HersheyFonts.HersheySimplex, 0.75, new Scalar(0, 0, 255), 1);
            }

            int area = inpHeight * inpWidth;
            int i = 0, j = 0, c = 0;
            for (i = 0; i < result_image.Rows; i++)
            {
                for (j = 0; j < result_image.Cols; j++)
                {
                    int x = (int)((j / ratiow) + padw);  ///从原图映射回到输出特征图
                    int y = (int)((i / ratioh) + padh);
                    int max_id = -1;
                    float max_conf = -10000;
                    for (c = 0; c < seg_numclass; c++)
                    {
                        float seg_conf = seg[c * area + y * inpWidth + x];
                        if (seg_conf > max_conf)
                        {
                            max_id = c;
                            max_conf = seg_conf;
                        }
                    }
                    if (max_id > 0)
                    {
                        result_image.Set(i, j, class_colors[max_id]);
                    }
                }
            }

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

下载

源码下载

你可能感兴趣的:(OpenCV,Onnx,AI,dnn,人工智能,神经网络,HybridNets,可驾驶区域分割,车道线分割)