简体   繁体   中英

Drawing Lines Using Kinect v2 c# wpf

I am trying to draw lines on canvas using my KINECT v2 right Handtip positions. I am getting my right hand tip positions from this line of code.

CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper.MapCameraPointToColorSpace(handtipPosition);

And this is my code snippet for drawing the lines, I have defined another point to feed X1 and Y1 coordinates to my line as so

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);

but when i use PreviousPoint for assigning coordinates to my X1 and Y1 parameters i get the error 'Use of possibly Unassigned field X' (Which i am guessing occurs cause PreviousPoint has no value at the beginning) and if i fix previousPoint values for X and Y to 0, on my canvas i draw lines from a fixed point (0,0) as only X2 and Y2 follow my hand positions.

Please help me rectify this situation, Appreciate any suggestions and help.

I'm assuming the code to draw the lines is in an event handler for the KINECT? If so, simply instantiate the PreviousPoint object in the global scope.

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);
  }
}

Obviously this is psuedo code, which is the best I can do without more of your actual code. This should keep you from getting the null object, and since the values aren't being overwritten in the event handler, the values should update properly for drawing the lines.

These articles may also help:

understanding-kinect-coordinate-mapping/

kinect-painting-c-wpf-sdk-kinect

It's still not clear to me what the exact issue is here. However, based on your comment on the other answer, it seems that my guess from my comment an hour ago might be on the mark. In that event, something like this should work for you:

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;                        
    }
}

The above declares a "nullable" instance of ColorSpacePoint , outside the event handler method. It is initialized to null by default. In the event handler, a line is added only if this value is not still null , ie an initial point has already been received. In either case, the current point replaces the current value of PreviousPoint , so that that value can be used the next time the event handler is called.

Note that I am assuming ColorSpacePoint is in fact a value type (ie a struct ). I infer this from the error message you show, which should only come from value types. But if it's a reference type (ie a class ), then you do not need to declare the field as a "nullable" type, as reference types are already nullable. In that case, you can declare the field as ColorSpacePoint PreviousPoint , and then use that field directly (eg line.X1 = PreviousPoint.X; ) instead of copying it to a local first.

(Actually, you don't strictly need to copy it to a local first anyway...you could just use eg PreviousPoint.Value.X , but because you are accessing multiple fields, I like to move it to a local variable for clarity and to reduce access to the nullable type).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM