3

我的目标:
使用 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 项目...)。我很感激任何帮助!

谢谢。

4

1 回答 1

0

嗯,从您上面的评论看来,AForge 代码使事情运行缓慢。据我所知,您有以下选择

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

也许我在这里错过了更多 - 但这些应该让你走上正轨。

于 2012-05-01T17:26:57.017 回答