Advertisement
Robomatics

Kinect Skeleton

Jan 5th, 2013
4,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.61 KB | None | 0 0
  1. Imports Microsoft.Kinect
  2.  
  3. Public Class Form1
  4.  
  5.     Dim kinz As KinectSensor
  6.     Dim imagez As ColorImageFrame
  7.     Dim skeletonz As SkeletonFrame
  8.     Dim piccolor As Bitmap = New Bitmap(640, 480, Imaging.PixelFormat.Format32bppRgb)
  9.     Dim gfx As Graphics = Graphics.FromImage(piccolor)
  10.  
  11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.         For Each potentialSensor In KinectSensor.KinectSensors
  13.             If potentialSensor.Status = KinectStatus.Connected Then
  14.                 kinz = potentialSensor
  15.                 Exit For
  16.             End If
  17.         Next potentialSensor
  18.  
  19.         kinz.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30)
  20.         kinz.SkeletonStream.Enable()
  21.         AddHandler kinz.ColorFrameReady, AddressOf colorready
  22.         AddHandler kinz.SkeletonFrameReady, AddressOf skeletonready
  23.         kinz.Start()
  24.  
  25.         kinz.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated
  26.         kinz.ElevationAngle = -10 '0 More Likely
  27.     End Sub
  28.  
  29.     'Events Captured
  30.     Private Sub colorready(ByVal sender As Object, ByVal e As ColorImageFrameReadyEventArgs)
  31.  
  32.         imagez = e.OpenColorImageFrame
  33.  
  34.     End Sub
  35.     Private Sub skeletonready(ByVal sender As Object, ByVal e As SkeletonFrameReadyEventArgs)
  36.  
  37.         skeletonz = e.OpenSkeletonFrame
  38.  
  39.     End Sub
  40.  
  41.     'Color Video
  42.     Public Sub colormethod()
  43.  
  44.         Dim pixz(kinz.ColorStream.FramePixelDataLength - 1) As Byte
  45.  
  46.         If imagez IsNot Nothing Then
  47.  
  48.             imagez.CopyPixelDataTo(pixz)
  49.  
  50.             Dim rect As New Rectangle(0, 0, piccolor.Width, piccolor.Height)
  51.             Dim bmpData As System.Drawing.Imaging.BitmapData = piccolor.LockBits(rect, _
  52.                 Drawing.Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppRgb)
  53.             Dim ptr As IntPtr = bmpData.Scan0
  54.             Dim bytes As Integer = bmpData.Stride * piccolor.Height
  55.             Dim rgbValues(bytes - 1) As Byte
  56.             System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
  57.  
  58.             Dim secondcounter As Integer
  59.             Dim tempred As Integer
  60.             Dim tempblue As Integer
  61.             Dim tempgreen As Integer
  62.             Dim tempalpha As Integer
  63.             Dim tempx As Integer
  64.             Dim tempy As Integer
  65.             secondcounter = 0
  66.  
  67.             While secondcounter < rgbValues.Length
  68.                 tempblue = rgbValues(secondcounter)
  69.                 tempgreen = rgbValues(secondcounter + 1)
  70.                 tempred = rgbValues(secondcounter + 2)
  71.                 tempalpha = rgbValues(secondcounter + 3)
  72.                 tempalpha = 255
  73.  
  74.                 tempy = ((secondcounter * 0.25) / 640)
  75.                 tempx = (secondcounter * 0.25) - (tempy * 640)
  76.                 If tempx < 0 Then
  77.                     tempx = tempx + 640
  78.                 End If
  79.  
  80.                 tempred = pixz(secondcounter + 2)
  81.                 tempgreen = pixz(secondcounter + 1)
  82.                 tempblue = pixz(secondcounter + 0)
  83.  
  84.                 rgbValues(secondcounter) = tempblue
  85.                 rgbValues(secondcounter + 1) = tempgreen
  86.                 rgbValues(secondcounter + 2) = tempred
  87.                 rgbValues(secondcounter + 3) = tempalpha
  88.  
  89.                 secondcounter = secondcounter + 4
  90.             End While
  91.  
  92.  
  93.             System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
  94.  
  95.             piccolor.UnlockBits(bmpData)
  96.  
  97.         End If
  98.  
  99.     End Sub
  100.  
  101.  
  102.     'Skeleton display
  103.     Public Sub skeletonmethod()
  104.  
  105.         Dim skeletons(-1) As Skeleton
  106.         If skeletonz IsNot Nothing Then
  107.             skeletons = New Skeleton(skeletonz.SkeletonArrayLength - 1) {}
  108.             skeletonz.CopySkeletonDataTo(skeletons)
  109.         End If
  110.  
  111.         Dim penz As Pen = New Pen(Brushes.LimeGreen, 3)
  112.  
  113.         If skeletons.Length <> 0 Then
  114.  
  115.             For Each skel As Skeleton In skeletons
  116.  
  117.                 'Right Arm
  118.                 Dim shoulderright As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.ShoulderRight).Position, DepthImageFormat.Resolution640x480Fps30)
  119.                 Dim elbowright As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.ElbowRight).Position, DepthImageFormat.Resolution640x480Fps30)
  120.                 Dim wristright As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.WristRight).Position, DepthImageFormat.Resolution640x480Fps30)
  121.                 Dim handright As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.HandRight).Position, DepthImageFormat.Resolution640x480Fps30)
  122.                 gfx.DrawLine(penz, New Point(shoulderright.X, shoulderright.Y), New Point(elbowright.X, elbowright.Y))
  123.                 gfx.DrawLine(penz, New Point(elbowright.X, elbowright.Y), New Point(wristright.X, wristright.Y))
  124.                 gfx.DrawLine(penz, New Point(wristright.X, wristright.Y), New Point(handright.X, handright.Y))
  125.                 'Left Arm
  126.                 Dim shoulderleft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.ShoulderLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  127.                 Dim elbowleft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.ElbowLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  128.                 Dim wristleft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.WristLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  129.                 Dim handleft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.HandLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  130.                 gfx.DrawLine(penz, New Point(shoulderleft.X, shoulderleft.Y), New Point(elbowleft.X, elbowleft.Y))
  131.                 gfx.DrawLine(penz, New Point(elbowleft.X, elbowleft.Y), New Point(wristleft.X, wristleft.Y))
  132.                 gfx.DrawLine(penz, New Point(wristleft.X, wristleft.Y), New Point(handleft.X, handleft.Y))
  133.                 'Right Leg
  134.                 Dim FootRight As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.FootRight).Position, DepthImageFormat.Resolution640x480Fps30)
  135.                 Dim AnkleRight As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.AnkleRight).Position, DepthImageFormat.Resolution640x480Fps30)
  136.                 Dim KneeRight As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.KneeRight).Position, DepthImageFormat.Resolution640x480Fps30)
  137.                 Dim HipRight As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.HipRight).Position, DepthImageFormat.Resolution640x480Fps30)
  138.                 gfx.DrawLine(penz, New Point(HipRight.X, HipRight.Y), New Point(KneeRight.X, KneeRight.Y))
  139.                 gfx.DrawLine(penz, New Point(KneeRight.X, KneeRight.Y), New Point(AnkleRight.X, AnkleRight.Y))
  140.                 gfx.DrawLine(penz, New Point(AnkleRight.X, AnkleRight.Y), New Point(FootRight.X, FootRight.Y))
  141.                 'Left Leg
  142.                 Dim FootLeft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.FootLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  143.                 Dim AnkleLeft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.AnkleLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  144.                 Dim KneeLeft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.KneeLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  145.                 Dim HipLeft As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.HipLeft).Position, DepthImageFormat.Resolution640x480Fps30)
  146.                 gfx.DrawLine(penz, New Point(HipLeft.X, HipLeft.Y), New Point(KneeLeft.X, KneeLeft.Y))
  147.                 gfx.DrawLine(penz, New Point(KneeLeft.X, KneeLeft.Y), New Point(AnkleLeft.X, AnkleLeft.Y))
  148.                 gfx.DrawLine(penz, New Point(AnkleLeft.X, AnkleLeft.Y), New Point(FootLeft.X, FootLeft.Y))
  149.                 'Body
  150.                 Dim head As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.Head).Position, DepthImageFormat.Resolution640x480Fps30)
  151.                 Dim shouldercenter As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.ShoulderCenter).Position, DepthImageFormat.Resolution640x480Fps30)
  152.                 Dim Spine As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.Spine).Position, DepthImageFormat.Resolution640x480Fps30)
  153.                 Dim HipCenter As DepthImagePoint = kinz.MapSkeletonPointToDepth(skel.Joints(JointType.HipCenter).Position, DepthImageFormat.Resolution640x480Fps30)
  154.                 gfx.DrawLine(penz, New Point(head.X, head.Y), New Point(shouldercenter.X, shouldercenter.Y))
  155.                 gfx.DrawLine(penz, New Point(shouldercenter.X, shouldercenter.Y), New Point(shoulderright.X, shoulderright.Y))
  156.                 gfx.DrawLine(penz, New Point(shouldercenter.X, shouldercenter.Y), New Point(shoulderleft.X, shoulderleft.Y))
  157.                 gfx.DrawLine(penz, New Point(shouldercenter.X, shouldercenter.Y), New Point(Spine.X, Spine.Y))
  158.                 gfx.DrawLine(penz, New Point(HipCenter.X, HipCenter.Y), New Point(Spine.X, Spine.Y))
  159.                 gfx.DrawLine(penz, New Point(HipCenter.X, HipCenter.Y), New Point(HipRight.X, HipRight.Y))
  160.                 gfx.DrawLine(penz, New Point(HipCenter.X, HipCenter.Y), New Point(HipLeft.X, HipLeft.Y))
  161.  
  162.             Next skel
  163.  
  164.         End If
  165.  
  166.     End Sub
  167.  
  168.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  169.         colormethod()
  170.         skeletonmethod()
  171.         PictureBox1.Image = piccolor
  172.     End Sub
  173.  
  174. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement