2013/07/09

C#.Net Kinect 改變上下傾角

Kinect有內建一個馬達可以用來改變上下的傾角,Kinect可以轉動的傾角大概±27°
可以透過KinectSensor的ElevationAngle去取得到傾角角度,也可以透過該屬性去改變傾角角度
但官方有建議不要太頻繁的去轉動,有可能會造成馬達損毀

如果有想要左右移動的開發者可以自行安裝馬達,利用arduino或8051等晶片去控制


XAML:
<Window x:Class="KinectAdjustTiltAngle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="646" Loaded="Window_Loaded" Unloaded="Window_Unloaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="530*" />
            <ColumnDefinition Width="94*" />
        </Grid.ColumnDefinitions>
        <Image Name="image"  Height="640" Width="480" Margin="12,20,38,0"></Image>
        <Slider Height="629" Margin="513,20,81,0" Name="sliderKinectAngle" VerticalAlignment="Top" Minimum="-27" Maximum="27" Orientation="Vertical" Grid.ColumnSpan="2" PreviewMouseUp="sliderKinectAngle_PreviewMouseUp" />
        <Label Name="labelCurrentAngle"  Content="目前角度" Height="28" HorizontalAlignment="Left" Margin="19,323,0,0"  VerticalAlignment="Top" Grid.Column="1" />
    </Grid>
</Window>





Code:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using System.Threading;

namespace KinectAdjustTiltAngle
{
    /// 
    /// MainWindow.xaml 的互動邏輯
    /// 
    public partial class MainWindow : Window
    {
        private KinectSensor sensor = KinectSensor.KinectSensors[0];
        private byte[] dataByte;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!sensor.IsRunning)
            {
                sensor.ColorFrameReady += VideoFrameReady;
                sensor.ColorStream.Enable();
                sensor.Start();
            }
            labelCurrentAngle.Content = @"目前角度" +
                ((int)sensor.ElevationAngle).ToString();
            sliderKinectAngle.Value = (int)sensor.ElevationAngle;
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            if (sensor.IsRunning)
            {
                sensor.Stop();
            }
        }

        private void VideoFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            bool hasData = false;

            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                {
                }
                else
                {
                    dataByte = new byte[frame.PixelDataLength];
                    frame.CopyPixelDataTo(dataByte);
                    hasData = true;
                }
            }

            if (hasData)
            {
                BitmapSource source = BitmapSource.Create(640, 480, 96, 96,
                    PixelFormats.Bgr32, null, dataByte, 640 * 4);
                image.Source = source;
            }
        }

        private void sliderKinectAngle_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (sensor != null && sensor.IsRunning)
            {
                sensor.ElevationAngle = (int)sliderKinectAngle.Value;
                labelCurrentAngle.Content = @"目前角度" + ((int)sliderKinectAngle.Value).ToString();
            }
            Thread.Sleep(new TimeSpan(hours: 0, minutes: 0, seconds: 1));
        }

    }
}


轉動前

轉動後




參考資料:
http://tw-hkt.blogspot.tw/2012/03/kinect_10.html