簡體   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