`
cloudtech
  • 浏览: 4617437 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Kinect 开发系列之 获取捕捉点图像的横纵坐标并用来做图像剪切

 
阅读更多

以下言论和代码仅供参考,因为是自己对kinect的理解,会有错漏。

kinect的简单动作捕捉是通过其RGB摄像头(3 个摄像头之一)捕捉的。目前beta2版本里至少可以追踪21个关节点(如头,左肩膀,肩膀中心,有肩膀,脊椎,左右肘,手腕,脚踝,左右中臀)。可以在这里找得到Microsoft.Research.Kinect.Nui.JointID

Kinect有3个摄像头,除了上面说到的那个,还有分别作3D深度识别和感知器。注意3D深度识别,用下面的代码你能大概理解深度识别的意义和用法。如下:

using Microsoft.Research.Kinect.Nui;

public void GetCoordinates(Joint joint, out float x, out float y)
{
skeletonEngine.SkeletonToDepthImage(joint.Position, out x, out y);

x = (float)(x * rootCanvas.ActualWidth);
y = (float)(y * rootCanvas.ActualHeight);
}

Tip: SkeletonEngine is a type/object of Microsoft.Research.Kinect.Nui namespace.

注意这个方法 "SkeletonToDepthImage" ,传入一个关节(或称为捕捉点),会返回其x,y坐标值。

这个坐标值做什么用呢?

可以根据其值用来计算你要截取从图像X,Y坐标位置处开始的图像。 代码示例如下:

public BitmapSource GetFrame()
{
ImageFrame imgFrame;

try
{
imgFrame = kinectRuntime.VideoStream.GetNextFrame(3000);
}
catch (Exception)
{
return null;
}

PlanarImage Image = imgFrame.Image;

if (this.lastHeadPoint != null)
{
float Xcenter = 0;
float Ycenter = 0;

skeletonDisplayManager.GetCoordinates((Joint)this.lastHeadPoint, out Xcenter, out Ycenter);

// calculate the Rectangle coordinates in whom to search for a match
int width = 288;
int height = 400;
int X = (int)Xcenter - width / 2;
int Y = (int)Ycenter - height / 2;

// double check that we are not croping outside the image
X = X < 0 ? 0 : X;
Y = Y < 0 ? 0 : Y;
X = X + width >= 1280 ? 1280 - width : X;
Y = Y + height >= 1024 ? 1024 - height : Y;

BitmapSource ColorBitmap = BitmapSource.Create(image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, Image.Bits, Image.Width * Image.BytesPerPixel);

// do the crop
CroppedBitmap cb = new CroppedBitmap(ColorBitmap, new Int32Rect(x, y, width, height));
ColorBitmap = cb;

returnColorBitmap;
}

}

X,Y坐标跟图像细细相关,比如这个方法:

ColorBitmap = BitmapSource.Create(Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, Image.Bits, Image.Width * Image.BytesPerPixel);

咋看起来参数有些复杂,其实只需关注2点:即图像本身和你要定位的X,Y坐标。

图像对象ImageFrame来自于Kinect自己的SDK:Microsoft.Research.Kinect.Nui.

.NET的命名空间System.Windows.Media.Imaging.BitmapSource和CroppedBitmap会被经常引用在Kinect开发中。就像上面的方法BitmapSource.Create需要BitmapSource来创建一个图形。

还有一个kinect的核心对象Runtime,通过它可以获得很多对象,比如camera和image,video对象。

to be continued.....
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics