このエントリはKINECT SDK Advent Calendar : ATNDの12月18日分です。
kaorun55氏の「KINECT SDK Beta2 でユーザーデータを扱う( C# + WPF )」 を参考にVBに書き換えました。
まずはXAMLソースから
--------------------------------------------------------------------------------
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="521" Width="661">
<Grid>
<Image Height="480" Name="Image1" Width="640" />
</Grid>
</Window>
--------------------------------------------------------------------------------
今までのサンプルと同様です。
メインソースです。
--------------------------------------------------------------------------------
Imports Microsoft.Research.Kinect.Nui
Imports System.Threading
Imports System.Windows.Threading
Class MainWindow
Inherits Window
Private readerThread As Thread
Private shuldRun As Boolean
Private kinect As Runtime
Private usercolor() As Color = {Color.FromRgb(0, 0, 0), Colors.Red, Colors.Green, Colors.Blue, Colors.Yellow, Colors.Magenta, Colors.Pink}
Public Sub New()
' この呼び出しはデザイナーで必要です。
InitializeComponent()
' InitializeComponent() 呼び出しの後で初期化を追加します。
End Sub
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
If Runtime.Kinects.Count > 0 Then
''KINECT初期化
kinect = Runtime.Kinects(0)
kinect.Initialize(RuntimeOptions.UseColor Or RuntimeOptions.UseDepthAndPlayerIndex)
kinect.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color)
kinect.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex)
shuldRun = True
''スレッド作成
readerThread = New Thread(New ThreadStart(AddressOf RenderThread))
''スレッド開始
readerThread.Start()
End If
End Sub
Sub RenderThread()
''Imageコントロールに書き込むスレッド
While (shuldRun)
''映像データの取り込み
kinect = Runtime.Kinects(0)
''タイムアウトは100ms
Dim video As ImageFrame = kinect.VideoStream.GetNextFrame(100)
Dim depth As ImageFrame = kinect.DepthStream.GetNextFrame(100)
For y = 0 To depth.Image.Height - 1
For x = 0 To depth.Image.Width - 1
Dim index As Integer = (x + (y * depth.Image.Width)) * 2
Dim byte0 As Byte = depth.Image.Bits(index)
Dim byte1 As Byte = depth.Image.Bits(index + 1)
Dim playerIndex As Integer = byte0 And &H7
Dim distance As Integer = byte1 << 5 Or byte0 >> 3
If playerIndex <> 0 Then
Dim videoX As Integer = 0
Dim videoY As Integer = 0
''深度データからカラーデータに座標変換
kinect.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(ImageResolution.Resolution640x480, New ImageViewArea(), x, y, 0, videoX, videoY)
Dim videoIndex As Integer = (videoX + (videoY * video.Image.Width)) * video.Image.BytesPerPixel
videoIndex = Math.Min(videoIndex, video.Image.Bits.Length - video.Image.BytesPerPixel)
video.Image.Bits(videoIndex) = usercolor(playerIndex).R
video.Image.Bits(videoIndex + 1) = usercolor(playerIndex).G
video.Image.Bits(videoIndex + 2) = usercolor(playerIndex).B
End If
Next
Next
''メインスレッド経由でイメージの書込み
Me.Dispatcher.BeginInvoke(DispatcherPriority.Background, New Action(
Sub()
Image1.Source = BitmapImage.Create(video.Image.Width, video.Image.Height, 96, 96, PixelFormats.Bgr32, Nothing, video.Image.Bits, video.Image.Width * video.Image.BytesPerPixel)
End Sub))
End While
End Sub
Private Sub Window_Unloaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Unloaded
shuldRun = False
End Sub
End Class
--------------------------------------------------------------------------------
7人まで認識するはずですが、試していません。
0 件のコメント:
コメントを投稿