使用 AForge 对 Kinect 视频进行 C# 图像处理
本文关键字:图像处理 视频 AForge Kinect 使用 | 更新日期: 2023-09-27 18:32:19
我的目标是:
使用Kinect视频进行形状识别(图片上的大矩形),在图片上绘制矩形以突出显示结果并显示。
我使用的技术:
- C#代码,
- AForge,更具体地说是它的形状检查器
http://www.aforgenet.com/articles/shape_checker/
魔法应该如何运作:
- 每次帧准备就绪时,我都会将帧数据作为字节数组获取,并将其转换为位图以允许我对其进行分析
- 应用形状识别算法
- 渲染结果...
我的问题 :
到目前为止,整个过程有效,但是当我尝试在 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 代码似乎让事情运行缓慢。据我所知,您有以下选择
- (重新)编写 blob 处理以使用 OpenCL(在 CPU 或 GPU 上)来加快速度
- 使用像EmguCV这样更快的图像处理库?可能还有更多
- 如果不需要实时处理,请缓冲输入帧,并在多个后台线程中尽可能快地处理 AForge.NET 以尝试吸收尽可能多的延迟。
也许我在这里错过了更多 - 但这些应该让你走上正确的轨道。