예제 #1
0
        private void CameraZm_NewVideoSample(object sender, WPFMediaKit.DirectShow.MediaPlayers.VideoSampleArgs e)
        {
            Dispatcher.Invoke(new Action(delegate()
            {
                System.Drawing.Bitmap oNewFrame = e.VideoFrame;

                AForge.Imaging.Filters.FiltersSequence seq = new AForge.Imaging.Filters.FiltersSequence();
                seq.Add(new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721));
                seq.Add(new AForge.Imaging.Filters.Threshold(Threshold));
                seq.Add(new AForge.Imaging.Filters.GrayscaleToRGB());
                System.Drawing.Bitmap oFilterBitmap = seq.Apply(oNewFrame);

                var bitmapData = oFilterBitmap.LockBits(
                    new System.Drawing.Rectangle(0, 0, oFilterBitmap.Width, oFilterBitmap.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly, oFilterBitmap.PixelFormat);
                byte[] destArr = new byte[bitmapData.Stride * bitmapData.Height];
                System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, destArr, 0,
                                                            bitmapData.Stride * bitmapData.Height);
                oFilterBitmap.UnlockBits(bitmapData);


                var bitmapSource = BitmapSource.Create(
                    bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr24, null,
                    bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
                this.ImgMainZm.Source = bitmapSource;

                try
                {
                    this.ARRgbRaster.wrapBuffer(destArr);
                    int detectedMkrs = this.ARDetectMarker.detectMarkerLite(this.ARRgbRaster, Threshold);
                    this.CvMainZm.Children.Clear();
                    if (detectedMkrs > 0)
                    {
                        for (int i = 0; i < detectedMkrs; i++)
                        {
                            this.ShowMarkerRect(ARDetectMarker.getCorners(i));
                        }
                    }
                }
                catch
                {
                }
            }));
        }
예제 #2
0
        void CaptureListener.OnBuffer(CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)
        {
            int w = i_sender.video_width;
            int h = i_sender.video_height;
            int s = w * (i_sender.video_bit_count / 8);

            AForge.Imaging.Filters.FiltersSequence seq = new AForge.Imaging.Filters.FiltersSequence();
            seq.Add(new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721));
            seq.Add(new AForge.Imaging.Filters.Threshold(127));
            seq.Add(new AForge.Imaging.Filters.GrayscaleToRGB());

            AForge.Imaging.UnmanagedImage srcImg = new AForge.Imaging.UnmanagedImage(i_buffer, w, h, s,
                                                                                     System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            AForge.Imaging.UnmanagedImage outputImg = seq.Apply(srcImg);

            byte[] destArr = new byte[outputImg.Stride * outputImg.Height];
            System.Runtime.InteropServices.Marshal.Copy(outputImg.ImageData, destArr, 0,
                                                        outputImg.Stride * outputImg.Height);
            this.m_raster.wrapBuffer(destArr);

            try
            {
                //this.m_ar.

                int detectedMkrs = this.m_ar.detectMarkerLite(this.m_raster, m_threshold);
                NyARDoublePoint2d[]        points   = null;
                List <NyARDoublePoint2d[]> ltPoints = new List <NyARDoublePoint2d[]>();
                if (detectedMkrs > 0)
                {
                    points = m_ar.getCorners(0);

                    ltPoints.Add(points);
                    for (int i = 1; i < detectedMkrs; i++)
                    {
                        NyARDoublePoint2d[] oMarkerPoints = m_ar.getCorners(i);
                        ltPoints.Add(oMarkerPoints);
                    }
                }

                Dispatcher.BeginInvoke(new Action(delegate()
                {
                    try
                    {
                        TransformedBitmap b = new TransformedBitmap();
                        b.BeginInit();
                        b.Source = BitmapSource.Create(w, h, 96.0, 96.0, PixelFormats.Bgr32, BitmapPalettes.WebPalette,
                                                       i_buffer, i_buffer_len, s);
                        b.SetValue(TransformedBitmap.TransformProperty, new ScaleTransform(-1, -1));
                        b.EndInit();
                        image1.SetValue(Image.SourceProperty, b);

                        if (points != null && points.Length == 4)
                        {
                            recognizedTag.Points = new PointCollection(new Point[]
                            {
                                new Point(cameraResX - points[0].x, cameraResY - points[0].y),
                                new Point(cameraResX - points[1].x, cameraResY - points[1].y),
                                new Point(cameraResX - points[2].x, cameraResY - points[2].y),
                                new Point(cameraResX - points[3].x, cameraResY - points[3].y)
                            });
                            recognizedTag.Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            recognizedTag.Visibility = System.Windows.Visibility.Hidden;
                        }

                        this.CvMainZm.Children.Clear();
                        for (int i = 1; i < ltPoints.Count; i++)
                        {
                            NyARDoublePoint2d[] oMarkerPoints = ltPoints[i];
                            Polygon oPolygon = new Polygon()
                            {
                                SnapsToDevicePixels = true,
                                Fill    = new SolidColorBrush(Colors.Violet),
                                Opacity = 0.8,
                                Stroke  = new SolidColorBrush(Colors.Red)
                            };

                            oPolygon.Points = new PointCollection(new Point[]
                            {
                                new Point(cameraResX - oMarkerPoints[0].x, cameraResY - oMarkerPoints[0].y),
                                new Point(cameraResX - oMarkerPoints[1].x, cameraResY - oMarkerPoints[1].y),
                                new Point(cameraResX - oMarkerPoints[2].x, cameraResY - oMarkerPoints[2].y),
                                new Point(cameraResX - oMarkerPoints[3].x, cameraResY - oMarkerPoints[3].y)
                            });
                            this.CvMainZm.Children.Add(oPolygon);
                        }
                    }
                    catch
                    {
                    }
                }), null);
            }
            catch
            {
            }
        }
        private void CameraZm_NewVideoSample(object sender, WPFMediaKit.DirectShow.MediaPlayers.VideoSampleArgs e)
        {
            Dispatcher.Invoke(new Action(delegate()
            {
                System.Drawing.Bitmap oNewFrame = e.VideoFrame;

                AForge.Imaging.Filters.FiltersSequence seq = new AForge.Imaging.Filters.FiltersSequence();
                seq.Add(new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721));
                seq.Add(new AForge.Imaging.Filters.Threshold(127));
                seq.Add(new AForge.Imaging.Filters.GrayscaleToRGB());
                System.Drawing.Bitmap oFilterBitmap = seq.Apply(oNewFrame);

                var bitmapData = oFilterBitmap.LockBits(
                    new System.Drawing.Rectangle(0, 0, oFilterBitmap.Width, oFilterBitmap.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly, oFilterBitmap.PixelFormat);

                byte[] destArr = new byte[bitmapData.Stride * bitmapData.Height];

                System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, destArr, 0,
                                                            bitmapData.Stride * bitmapData.Height);

                var bitmapSource = BitmapSource.Create(
                    bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr24, null,
                    bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);

                oFilterBitmap.UnlockBits(bitmapData);

                this.m_raster.wrapBuffer(destArr);

                try
                {
                    int detectedMkrs                    = this.m_ar.detectMarkerLite(this.m_raster, m_threshold);
                    NyARDoublePoint2d[] points          = null;
                    List <NyARDoublePoint2d[]> ltPoints = new List <NyARDoublePoint2d[]>();
                    if (detectedMkrs > 0)
                    {
                        points = m_ar.getCorners(0);

                        ltPoints.Add(points);
                        for (int i = 0; i < detectedMkrs; i++)
                        {
                            NyARDoublePoint2d[] oMarkerPoints = m_ar.getCorners(i);
                            ltPoints.Add(oMarkerPoints);
                        }
                    }
                    Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        try
                        {
                            this.ImgMainZm.Source = bitmapSource;

                            this.CvMainZm.Children.Clear();
                            for (int i = 0; i < ltPoints.Count; i++)
                            {
                                NyARDoublePoint2d[] oMarkerPoints = ltPoints[i];
                                Polygon oPolygon = new Polygon()
                                {
                                    SnapsToDevicePixels = true,
                                    Fill    = new SolidColorBrush(Colors.Violet),
                                    Opacity = 0.8,
                                    Stroke  = new SolidColorBrush(Colors.Red)
                                };

                                oPolygon.Points = new PointCollection(new Point[]
                                {
                                    new Point(oMarkerPoints[0].x, cameraResY - oMarkerPoints[0].y),
                                    new Point(oMarkerPoints[1].x, cameraResY - oMarkerPoints[1].y),
                                    new Point(oMarkerPoints[2].x, cameraResY - oMarkerPoints[2].y),
                                    new Point(oMarkerPoints[3].x, cameraResY - oMarkerPoints[3].y)
                                });
                                this.CvMainZm.Children.Add(oPolygon);
                            }
                        }
                        catch
                        { }
                    }), null);
                }
                catch
                { }
            }));
        }
        void CaptureListener.OnBuffer(CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)
        {
            // calculate size of the frame bitmap
            int w = i_sender.video_width;
            int h = i_sender.video_height;
            int s = w * (i_sender.video_bit_count / 8); // stride


            AForge.Imaging.Filters.FiltersSequence seq = new AForge.Imaging.Filters.FiltersSequence();
            seq.Add(new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721));
            seq.Add(new AForge.Imaging.Filters.Threshold(127));
            seq.Add(new AForge.Imaging.Filters.GrayscaleToRGB());
            AForge.Imaging.UnmanagedImage srcImg    = new AForge.Imaging.UnmanagedImage(i_buffer, w, h, s, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            AForge.Imaging.UnmanagedImage outputImg = seq.Apply(srcImg);
            byte[] destArr = new byte[outputImg.Stride * outputImg.Height];
            System.Runtime.InteropServices.Marshal.Copy(outputImg.ImageData, destArr, 0, outputImg.Stride * outputImg.Height);
            this.m_raster.wrapBuffer(destArr);

            try
            {
                int        detectedMkrs = this.m_ar.detectMarkerLite(this.m_raster, m_threshold);
                NyARSquare square       = null;
                if (detectedMkrs > 0)
                {
                    NyARTransMatResult  transMat = new NyARTransMatResult();
                    NyARDoublePoint2d[] points   = m_ar.getCorners(0); // RichF added this method
                    square          = new NyARSquare();
                    square.sqvertex = points;
                }


                Dispatcher.BeginInvoke(new Action(delegate()
                {
                    TransformedBitmap b = new TransformedBitmap();
                    b.BeginInit();
                    b.Source = BitmapSource.Create(w, h, dpiX, dpiY, PixelFormats.Bgr32, BitmapPalettes.WebPalette, i_buffer, i_buffer_len, s);
                    b.SetValue(TransformedBitmap.TransformProperty, new ScaleTransform(-1, -1));
                    b.EndInit();
                    image1.SetValue(Image.SourceProperty, b);

                    if (square != null)
                    {
                        recognizedTag.Points = new PointCollection(new Point[] {
                            new Point(cameraResX - square.sqvertex[0].x, cameraResY - square.sqvertex[0].y),
                            new Point(cameraResX - square.sqvertex[1].x, cameraResY - square.sqvertex[1].y),
                            new Point(cameraResX - square.sqvertex[2].x, cameraResY - square.sqvertex[2].y),
                            new Point(cameraResX - square.sqvertex[3].x, cameraResY - square.sqvertex[3].y)
                        });

                        recognizedTag.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        recognizedTag.Visibility = System.Windows.Visibility.Hidden;
                    }
                }), null);
            }
            catch
            {
            }
        }