Exemplo n.º 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);
 }
Exemplo n.º 2
0
        public void Run(cv.Mat src)
        {
            var array = new byte[src.Width * src.Height * src.ElemSize()];

            Marshal.Copy(src.Data, array, 0, array.Length);
            using (var image = Dlib.LoadImageData <byte>(array, (uint)src.Height, (uint)src.Width, (uint)(src.Width * src.ElemSize())))
            {
                Dlib.PyramidUp(image);
                rects = detector.Operator(image);
            }
        }
Exemplo n.º 3
0
        private Array2D <byte> ConvertMatToDlib2DArray(OpenCvSharp.Mat frame)
        {
            // https://github.com/takuya-takeuchi/DlibDotNet/issues/213
            Array2D <byte> gray  = null; // target Dlib array, it's grayscale, 1 byte per pixel
            int            size  = frame.Width * frame.Height * frame.ElemSize();
            var            array = new byte[size];

            Marshal.Copy(frame.Data, array, 0, size);
            gray = Dlib.LoadImageData <byte>(array, (uint)frame.Height, (uint)frame.Width, (uint)(frame.Width * frame.ElemSize()));

            return(gray);
        }
Exemplo n.º 4
0
 public static void Write(Mat image, WriteableBitmap bmp)
 {
     if (image.Rows != bmp.PixelHeight || image.Cols != bmp.PixelWidth)
     {
         Cv2.Resize(image, image, new OpenCvSharp.Size(bmp.PixelWidth, bmp.PixelHeight));
     }
     var pixels = GetPixels(image);
     var stride = image.Cols * image.ElemSize();
     bmp.WritePixels(
             new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight),
             pixels,
             stride,
             0);
 }
Exemplo n.º 5
0
        public void Run(cv.Mat src)
        {
            var array = new byte[src.Width * src.Height * src.ElemSize()];

            Marshal.Copy(src.Data, array, 0, array.Length);
            using (var image = Dlib.LoadImageData <byte>(array, (uint)src.Height, (uint)src.Width, (uint)(src.Width * src.ElemSize())))
                using (var blurredImg = new Array2D <byte>())
                    using (var horzGradient = new Array2D <short>())
                        using (var vertGradient = new Array2D <short>())
                        {
                            Dlib.GaussianBlur(image, blurredImg); // there appears to be no provision for a kernel size with the DlibDotNet interface...
                            // Now find the horizontal and vertical gradient images.
                            Dlib.SobelEdgeDetector(blurredImg, horzGradient, vertGradient);

                            // now we do the non-maximum edge suppression step so that our edges are nice and thin
                            Dlib.SuppressNonMaximumEdges(horzGradient, vertGradient, edgeImage);
                        }
        }
Exemplo n.º 6
0
        public void Run(cv.Mat src)
        {
            var array = new byte[src.Width * src.Height * src.ElemSize()];

            Marshal.Copy(src.Data, array, 0, array.Length);
            if (src.Channels() == 1)
            {
                using (var image = Dlib.LoadImageData <byte>(array, (uint)src.Height, (uint)src.Width, (uint)(src.Width * src.ElemSize())))
                {
                    Dlib.GaussianBlur(image, blurredGray);
                }
            }
            else
            {
                using (var image = Dlib.LoadImageData <BgrPixel>(array, (uint)src.Height, (uint)src.Width, (uint)(src.Width * src.ElemSize())))
                {
                    Dlib.GaussianBlur(image, blurredRGB);
                }
            }
        }
Exemplo n.º 7
0
 public static byte[] GetPixels(Mat image)
 {
     byte[] pixels = new byte[image.Width * image.ElemSize() * image.Height];
     Marshal.Copy(image.Data, pixels, 0, pixels.Length);
     return pixels;
 }