使用AForge对Kinect视频进行C#图像处理 [英] C# image processing on Kinect video using AForge

查看:261
本文介绍了使用AForge对Kinect视频进行C#图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal :
use Kinect video to do shape recognition (large rectangle on the picture), draw rectangle on the picture to highlights the results and display.

我使用的技术:


  • C#代码,

  • AForge,更具体地说是形状检查器

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

神奇应该如何运作:


  1. 每次帧准备就绪时,我将帧数据作为字节数组获取并将其转换为位图以允许我分析它

  2. 应用形状识别算法

  3. 渲染结果...

我的问题:

到目前为止整个过程都有效,但是当我尝试将结果呈现在WPF Image它非常落后......(每10秒1帧)......

My problem :
The whole process works so far but when I try to render the result in a WPF Image it lags terribly... (1 frame every 10 sec)...

我的代码:

// 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项目......)。我感谢任何帮助!

I can provide full code (MV C# 2010 project...). I appreciate any help!

谢谢。

推荐答案

嗯,来自你上面的评论看起来像AForge代码让事情变得缓慢。从我能想到的,你有以下选择

Hmmm, from your comment above it looks like the AForge code is making things run slowly. From what I can think of, you have the following options


  1. (重新)写blob处理以使用OpenCL(在CPU或GPU上)加快速度

  2. 使用更快的图像处理库,如 EmguCV ?可能还有更多

  3. 如果不需要实时处理,请缓冲输入帧并尽可能快地处理它们,AForge.NET可以在多个后台线程中尝试吸收尽可能多的延迟你可以。

  1. (Re)write the blob processing to use OpenCL (on CPU or GPU) to speed things up
  2. Use a faster image processing library like EmguCV? There may be many more
  3. If real-time processing is not required, buffer the input frames and process them as fast as AForge.NET can in multiple background threads to try and absorb as much latency as you can.

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

And perhaps more I'm missing here - but these should get you on the right track.

这篇关于使用AForge对Kinect视频进行C#图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆