[英]How to track only one person/body with Kinect v2 in C#
我想追蹤第一個人,並且只使用這個人的身體。
因此,基本上,當一個人被跟蹤,並且有人在他身后行走或與這個人看着時,如果他們移動了,則該kinect不能識別任何其他人。
我正在使用C#中的SDK 2.0“ Body Basics-WPF”形式的示例代碼。 我的目標是僅由一個人識別幾個關節(成功完成)。 我找到了一個如何在Kinect v1上實現它的線程,但對於Kinect v2則沒有。 這里的代碼:
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (dataReceived)
{
using (DrawingContext dc = this.drawingGroup.Open())
{
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
int penIndex = 0;
foreach (Body body in this.bodies)
{
Pen drawPen = this.bodyColors[penIndex++];
if (body.IsTracked)
{
this.DrawClippedEdges(body, dc);
IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
foreach (JointType jointType in joints.Keys)
{
if (jointType == JointType.ShoulderRight || jointType == JointType.ElbowRight || jointType == JointType.WristRight || jointType == JointType.HandRight ||
jointType == JointType.ShoulderLeft || jointType == JointType.ElbowLeft || jointType == JointType.WristLeft || jointType == JointType.HandLeft)
{
CameraSpacePoint position = joints[jointType].Position;
if (position.Z < 0)
{
position.Z = InferredZPositionClamp;
}
DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
}
}
this.DrawBody(joints, jointPoints, dc, drawPen);
}
}
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
}
}
}
安靜一陣子,但我通過自己的功能來跟蹤一個人來解決此問題。 如果被跟蹤的人離開了“身體陣列”中的下一個頂部,則獲取“被跟蹤的人”。
private ulong currTrackingId = 0;
private Body GetActiveBody()
{
if (currTrackingId <= 0)
{
foreach (Body body in this.bodies)
{
if (body.IsTracked)
{
currTrackingId = body.TrackingId;
return body;
}
}
return null;
}
else
{
foreach (Body body in this.bodies)
{
if (body.IsTracked && body.TrackingId == currTrackingId)
{
return body;
}
}
}
currTrackingId = 0;
return GetActiveBody();
}
我使用的是較舊的Kinect,但仍然沒有新的Kinect,因此這可能行不行:如果bodyFrame.BodyCount會給您面對Kinect的人數,然后將其替換為1,這樣您的代碼將變為:
this.bodies = new Body [1];
我認為通常kinect會選擇最接近的人。 試試看,告訴我你得到了什么。
無需使用foreach來迭代bodys數組,而只需使用列表中的第一個body。
更換:
foreach (Body body in this.bodies)
{
if (body.IsTracked)
{
...
附:
var body = this.bodies[0];
if (body.tracked)
{
...
更新:
我只是使用Kinect Studio進行了嘗試,但我發現即使只有一個跟蹤主體,也不能保證它位於數組的第一個插槽中。 在我的應用程序中,現在我正在使用LINQ來獲取IsTracked == true的第一個正文。 對於生產准備好的代碼,我沒有很好的答案。 如果我解決了這個問題,我將更新這篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.