使用 Kinect v2 c# wpf 绘制线条
Drawing Lines Using Kinect v2 c# wpf
我正在尝试使用我的 KINECT v2 右手端位置在 canvas 上画线。我从这行代码中得到了我的右手尖端位置。
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper.MapCameraPointToColorSpace(handtipPosition);
这是我绘制线条的代码片段,我已经定义了另一个点来将 X1 和 Y1 坐标馈送到我的线条中
ColorSpacePoint PreviousPoint;
line.X1 = PreviousPoint.X; // ERROR 'Use of possibly Unassigned field X'
line.Y1 = PreviousPoint.Y; // ERROR 'Use of possibly Unassigned field Y'
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
PreviousPoint = handtipPoint;
canvas.Children.Add(line);
但是当我使用 PreviousPoint 为我的 X1 和 Y1 参数分配坐标时,我得到错误 'Use of possibly Unassigned field X'(我猜这是因为 PreviousPoint 在开始时没有值)并且如果我修复 previousPoint 值X 和 Y 到 0,在我的 canvas 上,我从固定点 (0,0) 画线,因为只有 X2 和 Y2 跟随我的手的位置。
请帮助我纠正这种情况,感谢任何建议和帮助。
我假设绘制线条的代码在 KINECT 的事件处理程序中?如果是这样,只需在全局范围内实例化 PreviousPoint 对象。
public class whatever {
private ColorSpacePoint PreviousPoint;
// set the initial values, so you don't get a uninstantiated object error.
PreviousPoint.X = 0;
PreviousPoint.Y = 0;
Private Void EventHandlerForMovingKinect(args e) {
// If there is some sort of event handler that fires each time your move the kinect or whatever, we can put the rest of the code here.
line.Y1 = PreviousPoint.Y;
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
PreviousPoint = handtipPoint;
canvas.Children.Add(line);
}
}
显然这是伪代码,这是我在没有更多实际代码的情况下所能做的最好的。这应该可以防止您获得空对象,并且由于事件处理程序中的值没有被覆盖,因此值应该正确更新以绘制线条。
这些文章也可能有帮助:
understanding-kinect-coordinate-mapping/
kinect-painting-c-wpf-sdk-kinect
我仍然不清楚这里的确切问题是什么。但是,根据您对另一个答案的评论,我一小时前的评论中的猜测似乎是正确的。在那种情况下,像这样的东西应该对你有用:
class Form1 : Form // making assumption your code is in a Form
{
ColorSpacePoint? PreviousPoint;
// I have no idea what the actual event handler is supposed to look like.
// This is just a wild guess based on the little bit of code you posted.
void KinectEventHandler(object sender, KinectEventArgs handtip)
{
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper
.MapCameraPointToColorSpace(handtipPosition);
if (PreviousPoint != null)
{
ColorSpacePoint previousPointValue = (ColorSpacePoint)PreviousPoint;
line.X1 = previousPointValue.X;
line.Y1 = previousPointValue.Y;
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
canvas.Children.Add(line);
}
PreviousPoint = handtipPoint;
}
}
上面在事件处理程序方法之外声明了 ColorSpacePoint
的 "nullable" 实例。默认初始化为null
。在事件处理程序中,添加一行 仅当此值不仍为 null 时,即已收到初始点。在任何一种情况下,当前点都会替换 PreviousPoint
的当前值,以便下次调用事件处理程序时可以使用该值。
请注意,我假设 ColorSpacePoint
实际上是一个值类型(即 struct
)。我从您显示的错误消息中推断出这一点,它应该只来自值类型。但如果它是引用类型(即 class
),则不需要将字段声明为 "nullable" 类型,因为引用类型已经可以为 null。在这种情况下,您可以将字段声明为 ColorSpacePoint PreviousPoint
,然后直接使用该字段(例如 line.X1 = PreviousPoint.X;
),而不是先将其复制到本地。
(实际上,您并不一定要先将其复制到本地...您可以只使用例如 PreviousPoint.Value.X
,但是因为您正在访问多个字段,所以我喜欢将其移动到为了清晰起见并减少对可空类型的访问,一个局部变量)。
我正在尝试使用我的 KINECT v2 右手端位置在 canvas 上画线。我从这行代码中得到了我的右手尖端位置。
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper.MapCameraPointToColorSpace(handtipPosition);
这是我绘制线条的代码片段,我已经定义了另一个点来将 X1 和 Y1 坐标馈送到我的线条中
ColorSpacePoint PreviousPoint;
line.X1 = PreviousPoint.X; // ERROR 'Use of possibly Unassigned field X'
line.Y1 = PreviousPoint.Y; // ERROR 'Use of possibly Unassigned field Y'
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
PreviousPoint = handtipPoint;
canvas.Children.Add(line);
但是当我使用 PreviousPoint 为我的 X1 和 Y1 参数分配坐标时,我得到错误 'Use of possibly Unassigned field X'(我猜这是因为 PreviousPoint 在开始时没有值)并且如果我修复 previousPoint 值X 和 Y 到 0,在我的 canvas 上,我从固定点 (0,0) 画线,因为只有 X2 和 Y2 跟随我的手的位置。
请帮助我纠正这种情况,感谢任何建议和帮助。
我假设绘制线条的代码在 KINECT 的事件处理程序中?如果是这样,只需在全局范围内实例化 PreviousPoint 对象。
public class whatever {
private ColorSpacePoint PreviousPoint;
// set the initial values, so you don't get a uninstantiated object error.
PreviousPoint.X = 0;
PreviousPoint.Y = 0;
Private Void EventHandlerForMovingKinect(args e) {
// If there is some sort of event handler that fires each time your move the kinect or whatever, we can put the rest of the code here.
line.Y1 = PreviousPoint.Y;
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
PreviousPoint = handtipPoint;
canvas.Children.Add(line);
}
}
显然这是伪代码,这是我在没有更多实际代码的情况下所能做的最好的。这应该可以防止您获得空对象,并且由于事件处理程序中的值没有被覆盖,因此值应该正确更新以绘制线条。
这些文章也可能有帮助:
understanding-kinect-coordinate-mapping/
kinect-painting-c-wpf-sdk-kinect
我仍然不清楚这里的确切问题是什么。但是,根据您对另一个答案的评论,我一小时前的评论中的猜测似乎是正确的。在那种情况下,像这样的东西应该对你有用:
class Form1 : Form // making assumption your code is in a Form
{
ColorSpacePoint? PreviousPoint;
// I have no idea what the actual event handler is supposed to look like.
// This is just a wild guess based on the little bit of code you posted.
void KinectEventHandler(object sender, KinectEventArgs handtip)
{
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper
.MapCameraPointToColorSpace(handtipPosition);
if (PreviousPoint != null)
{
ColorSpacePoint previousPointValue = (ColorSpacePoint)PreviousPoint;
line.X1 = previousPointValue.X;
line.Y1 = previousPointValue.Y;
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
canvas.Children.Add(line);
}
PreviousPoint = handtipPoint;
}
}
上面在事件处理程序方法之外声明了 ColorSpacePoint
的 "nullable" 实例。默认初始化为null
。在事件处理程序中,添加一行 仅当此值不仍为 null 时,即已收到初始点。在任何一种情况下,当前点都会替换 PreviousPoint
的当前值,以便下次调用事件处理程序时可以使用该值。
请注意,我假设 ColorSpacePoint
实际上是一个值类型(即 struct
)。我从您显示的错误消息中推断出这一点,它应该只来自值类型。但如果它是引用类型(即 class
),则不需要将字段声明为 "nullable" 类型,因为引用类型已经可以为 null。在这种情况下,您可以将字段声明为 ColorSpacePoint PreviousPoint
,然后直接使用该字段(例如 line.X1 = PreviousPoint.X;
),而不是先将其复制到本地。
(实际上,您并不一定要先将其复制到本地...您可以只使用例如 PreviousPoint.Value.X
,但是因为您正在访问多个字段,所以我喜欢将其移动到为了清晰起见并减少对可空类型的访问,一个局部变量)。