コード例 #1
0
 public Mat Detect(Mat mat)
 {
     byte[] array = new byte[_width * _height * mat.ElemSize()];
     Marshal.Copy(mat.DataStart, array, 0, array.Length);
     using (Image <Bgr, byte> image1 = new Image <Bgr, byte>(_width, _height))
     {
         image1.Bytes = array;
         var frame = image1.Mat;
         int cols  = 640;
         int rows  = 480;
         _net.SetInput(DnnInvoke.BlobFromImage(frame, 1, new System.Drawing.Size(300, 300), default(MCvScalar), false, false));
         using (Emgu.CV.Mat matt = _net.Forward())
         {
             float[,,,] flt = (float[, , , ])matt.GetData();
             for (int x = 0; x < flt.GetLength(2); x++)
             {
                 if (flt[0, 0, x, 2] > _probability)
                 {
                     int X1 = Convert.ToInt32(flt[0, 0, x, 3] * cols);
                     int Y1 = Convert.ToInt32(flt[0, 0, x, 4] * rows);
                     int X2 = Convert.ToInt32(flt[0, 0, x, 5] * cols);
                     int Y2 = Convert.ToInt32(flt[0, 0, x, 6] * rows);
                     mat.Rectangle(new OpenCvSharp.Rect((int)X1, (int)Y1, (int)X2 - (int)X1, (int)Y2 - (int)Y1), Scalar.Red);
                 }
             }
         }
     }
     return(mat);
 }
コード例 #2
0
ファイル: HistSample.cs プロジェクト: JiphuTzu/opencvsharp
        public void Run()
        {
            Mat src = Cv2.ImRead(FilePath.Image.Lenna, ImreadModes.GrayScale);

            // Histogram view
            const int Width = 260, Height = 200;
            Mat render = new Mat(new Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));

            // Calculate histogram
            Mat hist = new Mat();
            int[] hdims = {256}; // Histogram size for each dimension
            Rangef[] ranges = { new Rangef(0,256), }; // min/max 
            Cv2.CalcHist(
                new Mat[]{src}, 
                new int[]{0}, 
                null,
                hist, 
                1, 
                hdims, 
                ranges);
  
            // Get the max value of histogram
            double minVal, maxVal;
            Cv2.MinMaxLoc(hist, out minVal, out maxVal);

            Scalar color = Scalar.All(100);
            // Scales and draws histogram
            hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
            for (int j = 0; j < hdims[0]; ++j)
            {
                int binW = (int)((double)Width / hdims[0]);
                render.Rectangle(
                    new Point(j * binW, render.Rows),
                    new Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
                    color, 
                    -1);
            }

            using (new Window("Image", WindowMode.AutoSize | WindowMode.FreeRatio, src))
            using (new Window("Histogram", WindowMode.AutoSize | WindowMode.FreeRatio, render))
            {
                Cv2.WaitKey();
            }
        }