繁体   English   中英

使用AForge对Kinect视频进行C#图像处理

[英]C# image processing on Kinect video using AForge

我的目标 :
使用Kinect视频进行形状识别(图片上的大矩形),在图片上绘制矩形以突出显示结果和显示。

我使用的技术:

  • C#代码,
  • AForge,更具体地说是它的形状检查器

http://www.aforgenet.com/articles/shape_checker/

魔术应该如何运作:

  1. 每次帧准备就绪时,我将帧数据作为字节数组并将其转换为位图以允许我分析它
  2. 应用形状识别算法
  3. 渲染结果......

我的问题 :
到目前为止整个过程都有效,但当我尝试在WPF图像中渲染结果时,它非常滞后......(每10秒1帧)......

我的代码:

// AllFramesReady is called every time a frame is ready to use...
private void AllFramesReady(object sender, AllFramesReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame == null)
            {
                return;
            }

            _Pixels = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(_Pixels);

            // Analyze the image

            int stride = colorFrame.Width * 4;
            System.Drawing.Size size = new System.Drawing.Size(colorFrame.Width, colorFrame.Height);
            // get the bitmap from bytes
            Bitmap btmap = BytesToBmp(_Pixels, size);
            //analyze the data...
            btmap = _shapeReco.AnalyzeImage(btmap);

            // copy the new data back to pixels
            _Pixels = BmpToBytes(btmap);

            // rendering the analyzed image
            imageAnalyzed.Source =
                BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                96, 96, PixelFormats.Bgr32, null, _Pixels, stride);
        }
    }


//
// HERE IS MY SHAPE RECOGNIZER THAT IMPLEMENTS THE SHAPE RECOGNITION ALGORITHM
//

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

using AForge;
using AForge.Imaging;
using AForge.Math.Geometry;


namespace KinectSetupDev
{
    class MyShapeRecognizer
    {

        private static String TAG = "MyShapeRecognizer";

        /***************************************************************************
         *                                VARIABLES                                *
         ***************************************************************************/

        private SimpleShapeChecker _ShapeChecker;
        private Bitmap _Image; // the image to analyze
        private Blob[] _Blobs;
        private BlobCounter _BlobCounter;

        /***************************************************************************
         *                              CONSTRUCTOR                                *
         ***************************************************************************/

        public MyShapeRecognizer()
        {
            Debug.Log(TAG, "MyShapeRecognizer");

            _ShapeChecker = new SimpleShapeChecker();
            _Image = new Bitmap(300, 400);
            _Blobs = null;
            _BlobCounter = null;
        }

        /***************************************************************************
         *                                METHODS                                  *
         ***************************************************************************/

        public Bitmap AnalyzeImage(Bitmap image)
        {
            Debug.Log(TAG, "AnalyzeImage");

            this._Image = image;
            this.LocatingObjects();
            this.AnalyzeObjects();

            return _Image;
        }

        private void LocatingObjects()
        {
            Debug.Log(TAG, "LocatingObjects");

            // lock image
            BitmapData bitmapData = _Image.LockBits(
                new Rectangle(0, 0, _Image.Width, _Image.Height),
                ImageLockMode.ReadOnly, _Image.PixelFormat);

            //locating objects
            _BlobCounter = new BlobCounter();

            _BlobCounter.FilterBlobs = true;
            _BlobCounter.MinHeight = 5;
            _BlobCounter.MinWidth = 5;

            _BlobCounter.ProcessImage(bitmapData);
            _Blobs = _BlobCounter.GetObjectsInformation();

            // unlock image
            _Image.UnlockBits(bitmapData);
        }

        private void AnalyzeObjects()
        {
            Debug.Log(TAG, "AnalyzeObjects");

            Graphics g = Graphics.FromImage(_Image);

            [DRAW RECT OR CIRCLE ON GRAPHICS]

            g.Dispose();
        }

        // Conver list of AForge.NET's points to array of .NET points
                                        private System.Drawing.Point[] ToPointsArray(List<IntPoint> points)
    {
        System.Drawing.Point[] array = new System.Drawing.Point[points.Count];

        for (int i = 0, n = points.Count; i < n; i++)
        {
            array[i] = new System.Drawing.Point(points[i].X, points[i].Y);
        }

        return array;
    }

    }
}

我可以提供完整的代码(MV C#2010项目......)。 我感谢任何帮助!

谢谢。

嗯,从上面的评论来看,AForge代码看起来很慢。 从我能想到的,你有以下选择

  1. (重新)编写blob处理以使用OpenCL(在CPU或GPU上)来加快速度
  2. 使用像EmguCV这样的更快的图像处理库? 可能还有更多
  3. 如果不需要实时处理,请缓冲输入帧并以尽可能快的速度处理它们,AForge.NET可以在多个后台线程中尝试尽可能多地吸收延迟。

也许我在这里失踪的更多 - 但这些应该让你走上正轨。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM