Reputation: 9
I wanted to know how to draw a rectangle around skeleton using SDK v1.5
What I want to do is, when a gesture is recognized, a red rectangle should appear around the skeleton.
Any help would be appreciated.
Regards,
Ahmed
Upvotes: 0
Views: 1373
Reputation: 841
This code won't work right away, it aims to give the algorithm and methods to use.
SkeletonPoint maxX = joints[0].Position, minX = joints[0].Position;
SkeletonPoint maxY = joints[0].Position, minY = joints[0].Position;
for(joint in joints){
if(joint.Position.x>maxX) maxX = joint.Position;
if(joint.Position.x<minX) minX = joint.Position;
if(joint.Position.y>maxY) maxY = joint.Position;
if(joint.Position.y<minY) minY = joint.Position;
}
// Adding a margin, because the articulations are not always placed
// at the extremities. Can adjust to get a better look.
float margin = .10f // 10 cm margin.
maxX.X += margin;
maxY.Y += margin;
minX.X -= margin;
minY.Y -= margin;
// Now we need to convert it to the video stream space.
DepthImagePoint maxXd = depthImageFrame.MapFromSkeletonPoint(maxX);
ColorImagePoint maxXc = depthImageFrame.MapToColorImagePoint(maxXd);
// **
// Same for other coordinates.
// **
// The buffer b will contains the current image, in bitmap format
// (without header). You may use colorImageFrame.Format to get more
// details on its encoding.
byte[] b = new byte[PixelDataLength];
colorImageFrame.CopyPixelDataTo(b);
// Then transform the buffer b into whatever you want,
// and draw the rect [[maxXc,maxYc],[minXc,minYc]] over it
// before rendering.
This way you get the outermost joints, and not just the joints that are likely to be the outermost.
The way to transform the byte array is left to you, it depends on your rending method.
Upvotes: 4
Reputation: 4366
Assuming you are using the Kinect SDK, This is relatively easy. I am using a canvas and getting the coordinates of the head, feet, hip, and hands so I know what to draw the box around, then add a few more pixels to each side. I am using the algorithm:
|(heady + 25) - (feety - 25)| * |(righthandx + 25) - (lefthandx - 25)|
To get the box height. I am using || for absolute value in this case
Then once you place you canvas (and joints), you can get their coordinates using:
Canvas.GetTop(element / 2);
Canvas.GetLeft(element / 2); // /2 so that we get the center
Then you can use my algorithm to find how big to make the box, and since elements are placed with their top left corners, you can use place it using the coordinate of the heady + a bit for the Y, and the right hand x + a bit as the X. You can place it using
Canvas.SetTop(ycoord, box);
Canvas.SetLeft(xcoord, box);
I add on the bit so there is a bit of a space between objects and the box.
Hope this helps.
When I work out all of the code, I will post it
Upvotes: 1