hi! i have to do a program with kinect for my senior project and i'm new in doing programs with visual studio.. i follow the instructions of the "Kinect for Windows SDK Programming Guide" but the explanation in one of the example applications of the book is insufficient.. to be more specific i have an issue on the way for applying color effects on video streaming such as red / green / blue, gray scale and inverting the color the book explains only the basic iteration on pixels like this one for gray scale
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
var data = Math.Max(Math.Max(this.pixelData[i], this.pixelData[i + 1]), this.pixelData[i + 2]);
this.pixelData[i] = data;
this.pixelData[i + 1] = data;
this.pixelData[i + 2] = data;
}
How do i change the color of the pixels adjusting this suggestion? here is a part of my code:
MainWindow.cs
public partial class MainWindow
{
KinectSensor sensor;
Byte[] pixelData;
MainWindowViewModel viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new MainWindowViewModel();
this.DataContext = this.viewModel;
}
protected void Window_loaded(object sender, RoutedEventArgs e)
{
if (KinectSensor.KinectSensors.Count > 0)
{
this.sensor = KinectSensor.KinectSensors.FirstOrDefault(sensorItem => sensorItem.Status == KinectStatus.Connected);
this.StartSensor();
if (!this.sensor.ColorStream.IsEnabled)
{
this.sensor.ColorStream.Enable();
}
this.sensor.ColorFrameReady += sensor_ColorFrameReady;
this.Settings_change();
}
else
{
MessageBox.Show("No device is connected with system!");
this.Close();
}
}
void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame imageFrame = e.OpenColorImageFrame())
{
if (imageFrame == null)
{
return;
}
else
{
this.pixelData = new byte[imageFrame.PixelDataLength];
imageFrame.CopyPixelDataTo(this.pixelData);
int stride = imageFrame.Width * imageFrame.BytesPerPixel;
this.VideoControl.Source = BitmapSource.Create(imageFrame.Width, imageFrame.Height, 96, 96,
PixelFormats.Bgr32, null, pixelData, stride);
if(this.viewModel.IsEffectsEnabled == true)
{
if (this.viewModel.CurrentColorEffect == KinectCam1.MainWindowViewModel.Effects.Red)
{
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
this.pixelData[i] = 0;
this.pixelData[i + 1] = 0;
}
}
if (this.viewModel.CurrentColorEffect == KinectCam1.MainWindowViewModel.Effects.Blue)
{
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
this.pixelData[i + 1] = 0;
this.pixelData[i + 2] = 0;
}
}
if (this.viewModel.CurrentColorEffect == KinectCam1.MainWindowViewModel.Effects.Green)
{
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
this.pixelData[i] = 0;
this.pixelData[i + 2] = 0;
}
}
}
if(this.viewModel.IsGrayScaleEnabled==true)
{
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
var data = Math.Max(Math.Max(this.pixelData[i], this.pixelData[i +
1]), this.pixelData[i + 2]);
this.pixelData[i] = data;
this.pixelData[i + 1] = data;
this.pixelData[i + 2] = data;
}
}
if (this.viewModel.IsInvertColorEffectsEnabled==true)
{
for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
{
this.pixelData[i] = (byte)~this.pixelData[i];
this.pixelData[i + 1] = (byte)~this.pixelData[i + 1];
this.pixelData[i + 2] = (byte)~this.pixelData[i + 2];
}
}
}
}
}
private void ColorEffectsSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.viewModel.IsEffectsEnabled = true;
this.ChangeColorEffectFormat();
}
private void ChangeColorEffectFormat()
{
if (this.sensor.IsRunning)
{
this.viewModel.CurrentColorEffect = (KinectCam1.MainWindowViewModel.Effects)this.ColorEffectsSelection.SelectedItem;
}
}
private int GetCurrentFrameNumber(ColorImageFrame ImageFrame)
{
return ImageFrame.FrameNumber;
}
private void ChangeColorImageFormat()
{
if (this.sensor.IsRunning)
{
this.viewModel.CurrentImageFormat = (ColorImageFormat)this.ColorImageFormatSelection.SelectedItem;
this.sensor.ColorStream.Enable(this.viewModel.CurrentImageFormat == ColorImageFormat.Undefined ?
ColorImageFormat.RgbResolution640x480Fps30 : this.viewModel.CurrentImageFormat);
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.ChangeColorImageFormat();
}
viweModel class
class MainWindowViewModel : INotifyPropertyChanged
{
public enum Effects
{
none=0,
Red,
Green,
Blue,
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnNotifyPropertyChange(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<Effects> colorEffectsvalue;
public ObservableCollection<Effects> ColorEffects
{
get
{
colorEffectsvalue = new ObservableCollection<Effects>();
foreach (Effects colorEffect in Enum.GetValues(typeof(Effects)))
{
colorEffectsvalue.Add(colorEffect);
}
return colorEffectsvalue;
}
}
private Effects CurrentColorEffectValue;
public Effects CurrentColorEffect
{
get
{
return this.CurrentColorEffectValue;
}
set
{
if (CurrentColorEffectValue != value)
{
CurrentColorEffectValue = value;
this.OnNotifyPropertyChange("CurrentColorEffect");
}
}
}
private ObservableCollection<ColorImageFormat> colorImageFormatvalue;
public ObservableCollection<ColorImageFormat> ColorImageFormats
{
get
{
colorImageFormatvalue = new ObservableCollection<ColorImageFormat>();
foreach (ColorImageFormat colorImageFormat in Enum.GetValues(typeof(ColorImageFormat)))
{
colorImageFormatvalue.Add(colorImageFormat);
}
return colorImageFormatvalue;
}
}
private ColorImageFormat CurrentImageFormatValue;
public ColorImageFormat CurrentImageFormat
{
get
{
return this.CurrentImageFormatValue;
}
set
{
if (CurrentImageFormatValue != value)
{
CurrentImageFormatValue = value;
this.OnNotifyPropertyChange("CurrentImageFormat");
}
}
}
xaml file
<CheckBox HorizontalAlignment="Left" Margin="220,70,0,0" VerticalAlignment="Top"
IsChecked="{Binding IsInvertColorEffectsEnabled}" />
<CheckBox HorizontalAlignment="Left" Margin="220,95,0,0" VerticalAlignment="Top"
IsChecked="{Binding IsFrameRateEnabled}"/>
<CheckBox HorizontalAlignment="Left" Margin="220,125,0,0" VerticalAlignment="Top"
IsChecked="{Binding IsFrameNumberEnabled}"/>
<CheckBox HorizontalAlignment="Left" Margin="220,153,0,0" VerticalAlignment="Top"
IsChecked="{Binding IsAutoCaptureEnabled}"/>
<CheckBox HorizontalAlignment="Left" Margin="220,200,0,0" VerticalAlignment="Top"
IsChecked="{Binding IsGrayScaleEnabled}"/>
<Slider TickPlacement="BottomRight" IsSnapToTickEnabled="True" Minimum="-27" Maximum="27"
SmallChange="5" LargeChange="5" ValueChanged="Slider_ValueChanged" Margin="216,172,237,33"/>
<ComboBox Name="ColorImageFormatSelection" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="236"
Margin="220,0,0,0" ItemsSource="{Binding ColorImageFormats}" SelectionChanged="ComboBox_SelectionChanged"/>
<ComboBox Name="ColorEffectsSelection" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="90"
Margin="220,35,0,0" ItemsSource="{Binding ColorEffects}" />