Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
C#
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())
        {
            // Check if the incoming frame is not null
            if (imageFrame == null)
            {
                return;
            }
            else
            {
                // Get the pixel data in byte array
                this.pixelData = new byte[imageFrame.PixelDataLength];
                // Copy the pixel data
                imageFrame.CopyPixelDataTo(this.pixelData);
                // Calculate the stride
                int stride = imageFrame.Width * imageFrame.BytesPerPixel;
                // assign the bitmap image source into image control
                this.VideoControl.Source = BitmapSource.Create(imageFrame.Width, imageFrame.Height, 96, 96,
                PixelFormats.Bgr32, null, pixelData, stride);

                //----------------------------checkBox Frame Number-----------------------------------
                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; //Blue
                            this.pixelData[i + 1] = 0; //Green
                        }
                    }
                    if (this.viewModel.CurrentColorEffect == KinectCam1.MainWindowViewModel.Effects.Blue)
                    {
                        for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
                        {
                            this.pixelData[i + 1] = 0; //Green
                            this.pixelData[i + 2] = 0; //Red
                        }
                    }
                    if (this.viewModel.CurrentColorEffect == KinectCam1.MainWindowViewModel.Effects.Green)
                    {
                        for (int i = 0; i < this.pixelData.Length; i += imageFrame.BytesPerPixel)
                        {
                            this.pixelData[i] = 0; //Blue
                            this.pixelData[i + 2] = 0; //Red
                        }
                    }
                }
                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];
                    }
                }
            }
        }
    }

    //----------------------------Effects-----------------------------------
    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;
        }
    }

    //----------------------------Frame Number-----------------------------------
    private int GetCurrentFrameNumber(ColorImageFrame ImageFrame)
    {
        return ImageFrame.FrameNumber;
    }

    //----------------------------Video Format-----------------------------------
    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
C#
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
HTML
<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}" />
Posted
Updated 11-Aug-14 7:28am
v2
Comments
gggustafson 11-Aug-14 15:28pm    
You are overwhelming your readers with too much code. Limit the amount you provide to just that which supports your question.
ahadji_547 24-Sep-14 13:42pm    
ok.. thank you for your reply.. i add all these parts because i thought that will help you to understand where i have to put the code for color effects.. however i found the solution and i put it as soon as possible..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900