【问题标题】:Conversion between OpenCVSharp Coordinates and System.Drawing CoordinatesOpenCVSharp坐标和System.Drawing坐标之间的转换
【发布时间】:2019-10-08 01:32:12
【问题描述】:

我有点卡在这个:

我正在尝试将由两个 OpenCvSharp.Points 组成的矩形转换为 System.Drawing.Rectangle。

我在 OpenCvSharp 中发现了一个有趣的区域,我想将此区域保存为新位图以显示在 PictureBox 或其他东西中。

public static Mat Detect(string filename)
        {
            var cfg = "4000.cfg";
            var model = "4000.weights"; //YOLOv2 544x544
            var threshold = 0.3;

            var mat = Cv2.ImRead(filename);
            var w = mat.Width;
            var h = mat.Height;
            var blob = CvDnn.BlobFromImage(mat, 1 / 255.0, new OpenCvSharp.Size(416, 416), new Scalar(), true, false);
            var net = CvDnn.ReadNetFromDarknet(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, cfg), Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, model));
            net.SetInput(blob, "data");

            Stopwatch sw = new Stopwatch();
            sw.Start();
            var prob = net.Forward(); // Process NeuralNet
            sw.Stop();
            Console.WriteLine($"Runtime:{sw.ElapsedMilliseconds} ms");


            const int prefix = 5;   //skip 0~4

            for (int i = 0; i < prob.Rows; i++)
            {
                var confidence = prob.At<float>(i, 4);
                if (confidence > threshold)
                {
                    //get classes probability
                    Cv2.MinMaxLoc(prob.Row[i].ColRange(prefix, prob.Cols), out _, out OpenCvSharp.Point max);
                    var classes = max.X;
                    var probability = prob.At<float>(i, classes + prefix);

                    if (probability > threshold) //more accuracy
                    {
                        //get center and width/height
                        var centerX = prob.At<float>(i, 0) * w;
                        var centerY = prob.At<float>(i, 1) * h;
                        var width = prob.At<float>(i, 2) * w;
                        var height = prob.At<float>(i, 3) * h;
                        //label formating
                        var label = $"{Labels[classes]} {probability * 100:0.00}%";
                        Console.WriteLine($"confidence {confidence * 100:0.00}% {label}");
                        var x1 = (centerX - width / 2) < 0 ? 0 : centerX - width / 2; //avoid left side over edge
                        //draw result
                        mat.Rectangle(new OpenCvSharp.Point(x1, centerY - height / 2), new OpenCvSharp.Point(centerX + width / 2, centerY + height / 2), Colors[classes], 2);
                        var textSize = Cv2.GetTextSize(label, HersheyFonts.HersheyTriplex, 0.5, 1, out var baseline);
                        Cv2.Rectangle(mat, new Rect(new OpenCvSharp.Point(x1, centerY - height / 2 - textSize.Height - baseline),
                                new OpenCvSharp.Size(textSize.Width, textSize.Height + baseline)), Colors[classes], Cv2.FILLED);
                        Cv2.PutText(mat, label, new OpenCvSharp.Point(x1, centerY - height / 2 - baseline), HersheyFonts.HersheyTriplex, 0.5, Scalar.Black);
                    }
                }
            }
            return mat;
        }

基本上是希望这个 OpenCvSharp Rect 作为单个位图绘制到垫子中以显示在图片框中

这是绘制矩形的线 mat.Rectangle(new OpenCvSharp.Point(x1, centerY - height / 2), new OpenCvSharp.Point(centerX + width / 2, centerY + height / 2), Colors[classes], 2);

谁能帮我解决这个问题? 我很抱歉我的英语不好!

【问题讨论】:

    标签: c# opencv opencvsharp


    【解决方案1】:
    var centerX = prob.At<float>(i, 0) * w;
    var centerY = prob.At<float>(i, 1) * h;
    var width = prob.At<float>(i, 2) * w;
    var height = prob.At<float>(i, 3) * h;
    var top  =(centerY - height / 2) < 0 ? 0 : centerY - height  / 2;
    var left =(centerX - width / 2) < 0 ? 0 : centerX - width / 2;
    //just put x,y,w,h in the bounding rect
    //make sure x,y,w,h will not crop the outside of the exising image
    var targetbox  = Cv2.BoundingRect(left,top,width,height);
    
    var output_mat = new Mat(input_image, targetbox); //Crop the image
    
    Bitmap output_mat_in_bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(output_mat);
    

    output_mat_in_bitmap 是您想要的位图。我觉得

    【讨论】:

    • 嗨,这看起来很有趣。我会试一试,如果有效,我会将其标记为答案。谢谢
    【解决方案2】:

    如果可以选择简单地转换整个 Mat 对象 - OpenCvSharp (https://github.com/shimat/opencvsharp) 包括许多实用程序和扩展 - 特别是允许从 Mat 直接转换为 Bitmap 的工具和扩展 看这个例子https://github.com/shimat/opencvsharp/wiki/Converting-Image

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多