C# emgu dnn 调用语义分割模型

记录下备忘

            Net net = DnnInvoke.ReadNetFromDarknet(@"J:\model.cfg", @"J:\model.weights");
    

            net.SetPreferableBackend(Emgu.CV.Dnn.Backend.Cuda);
            net.SetPreferableTarget(Target.CudaFp16);

            Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
            openFile.Filter = "图片格式|*.jpg;*.png;*.bmp";
            if (openFile.ShowDialog() != true) return;
            Image img = new Image(openFile.FileName);

            var size = new System.Drawing.Size(((int)(img.Width / 32d + 16) * 32), ((int)(img.Height / 32d + 16) * 32));
            var blob = DnnInvoke.BlobFromImage(img, 1 / 255d, size);
            net.SetInput(blob);

            var mat = net.Forward();
            int rows = mat.SizeOfDimension[2];
            int cols = mat.SizeOfDimension[3];
            int chhn = mat.SizeOfDimension[1];
            float* p = (float*)mat.DataPointer;
            Image res = new Image(cols, rows);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    var v = p[i * cols + j] * 255 + 0.5;
                    v = v > 255 ? 255 : (v < 0 ? 0 : v);
                    res[i, j] = new Rgb(v, v, v);
                }
            }

你可能感兴趣的:(c#,dnn,开发语言,opencv)