Advertisement
Robomatics

Kinect Depth

Sep 24th, 2012
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.07 KB | None | 0 0
  1. Imports Microsoft.Kinect
  2.  
  3. Public Class Form1
  4.  
  5.     Dim kinz As KinectSensor
  6.     Dim depthz As DepthImageFrame
  7.     Dim picdepth As Bitmap = New Bitmap(640, 480, Imaging.PixelFormat.Format32bppRgb)
  8.  
  9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.  
  11.         For Each potentialSensor In KinectSensor.KinectSensors
  12.             If potentialSensor.Status = KinectStatus.Connected Then
  13.                 kinz = potentialSensor
  14.                 Exit For
  15.             End If
  16.         Next potentialSensor
  17.  
  18.         kinz.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30)
  19.         AddHandler kinz.DepthFrameReady, AddressOf depthready
  20.         kinz.Start()
  21.  
  22.     End Sub
  23.  
  24.     Private Sub depthready(ByVal sender As Object, ByVal e As DepthImageFrameReadyEventArgs)
  25.  
  26.         depthz = e.OpenDepthImageFrame
  27.  
  28.     End Sub
  29.  
  30.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  31.  
  32.         depthmethod()
  33.         PictureBox1.Image = picdepth
  34.  
  35.     End Sub
  36.     'Color for depth
  37.     Public Sub depthmethod()
  38.  
  39.         Dim pixz(kinz.DepthStream.FramePixelDataLength - 1) As Short
  40.         Dim pixmax As Integer = 32760
  41.  
  42.         If depthz IsNot Nothing Then
  43.  
  44.             depthz.CopyPixelDataTo(pixz)
  45.  
  46.             Dim rect As New Rectangle(0, 0, picdepth.Width, picdepth.Height)
  47.             Dim bmpData As System.Drawing.Imaging.BitmapData = picdepth.LockBits(rect, _
  48.                 Drawing.Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppRgb)
  49.             Dim ptr As IntPtr = bmpData.Scan0
  50.             Dim bytes As Integer = bmpData.Stride * picdepth.Height
  51.             Dim rgbValues(bytes - 1) As Byte
  52.             System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
  53.  
  54.             Dim secondcounter As Integer = 0
  55.             Dim tempred As Integer
  56.             Dim tempblue As Integer
  57.             Dim tempgreen As Integer
  58.             Dim tempalpha As Integer
  59.             Dim range As Integer
  60.  
  61.             While secondcounter < rgbValues.Length
  62.                 tempblue = rgbValues(secondcounter)
  63.                 tempgreen = rgbValues(secondcounter + 1)
  64.                 tempred = rgbValues(secondcounter + 2)
  65.                 tempalpha = rgbValues(secondcounter + 3)
  66.                 tempalpha = 255
  67.  
  68.                 If pixz(secondcounter / 4) <> -8 Then
  69.                     tempgreen = 255 - ((pixz(secondcounter / 4) / pixmax) * 255)
  70.                 Else
  71.                     tempgreen = 0
  72.                 End If
  73.  
  74.  
  75.                 tempred = 0
  76.                 tempblue = 0
  77.  
  78.                 rgbValues(secondcounter) = tempblue
  79.                 rgbValues(secondcounter + 1) = tempgreen
  80.                 rgbValues(secondcounter + 2) = tempred
  81.                 rgbValues(secondcounter + 3) = tempalpha
  82.  
  83.                 secondcounter = secondcounter + 4
  84.             End While
  85.  
  86.             System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)
  87.  
  88.             picdepth.UnlockBits(bmpData)
  89.  
  90.         End If
  91.  
  92.     End Sub
  93. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement