none
What is my mistake? i can't gather RGB and IR images. RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Kinect;
    using System.Xaml;
    using System.IO;
    
    namespace camerashot
    {
        /// <summary>
        /// Логика взаимодействия для MainWindow.xaml
        /// </summary>
         
        public partial class MainWindow : Window
        {
            int check = 0;
            int checkir = 0;
            int number = 1;
    
            KinectSensor sensor, sensor2;
    
    
            public MainWindow()
            {
         sensor = KinectSensor.KinectSensors[0]; 
         InitializeComponent();
    
         sensor.Start();
         if (check == 0)
         {
    
             sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
             
             sensor.ColorFrameReady += colorframeReady;
    
             sensor.ColorStream.Disable();
    
         }
         if (checkir == 0)
         {
    
          
             sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
             sensor.ColorFrameReady += irframeReady;
             sensor.ColorStream.Disable();
    
         }
    
            }
    
                /////////////////////////////////////////////
    
           public void irframeReady(object sender, ColorImageFrameReadyEventArgs e)
            {
                ColorImageFrame IR = e.OpenColorImageFrame();
                if (IR == null) { return; }
                byte[] IRpixels = new byte[IR.PixelDataLength];
                IR.CopyPixelDataTo(IRpixels);
                if (checkir == 0)
                {
                    int strideir = IR.Width * IR.BytesPerPixel;
                    BitmapEncoder irencoder= new PngBitmapEncoder();
                irencoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(
                    IR.Width, IR.Height, 96, 96, PixelFormats.Gray16, null, IRpixels, strideir)));
    
    
                string irpath = System.IO.Path.Combine(@"C:\my3dclouds\calibration\","b"+ number.ToString() + ".png");
                try
                {
                    using (FileStream fs = new FileStream(irpath, FileMode.Create))
                    {
                        irencoder.Save(fs);
                    }
                }
                catch (IOException ioe)
                {
                    Console.WriteLine(ioe.ToString());
                }
    
                checkir = 1;
               
    
            }
                
            }
    
          
            
            
            
            
            public void colorframeReady(object sender, ColorImageFrameReadyEventArgs e)
           {
    
               ColorImageFrame DIF = e.OpenColorImageFrame();
               if (DIF == null) { return; }
    
               byte[] pixels = new byte[DIF.PixelDataLength];
               DIF.CopyPixelDataTo(pixels);
    
               if (check == 0)
               {
                   int stride = DIF.Width * DIF.BytesPerPixel;
                   BitmapEncoder encoder = new PngBitmapEncoder();
                   encoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(
                       DIF.Width, DIF.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride)));
    
                   string path = System.IO.Path.Combine( @"C:\my3dclouds\calibration\", "a"+number.ToString() + ".png");
    
                   try
                   {
                       using (FileStream fs = new FileStream(path, FileMode.Create))
                       {
                           encoder.Save(fs);
                       }
                   }
                   catch (IOException ioe)
                   {
                       Console.WriteLine(ioe.ToString());
                   }
                   check = 1;
               }
           }
    
            public void shot_Click(object sender, RoutedEventArgs e)
            {
                
                number++;
                
                check = 0;
                
                sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                sensor.ColorFrameReady += colorframeReady;
                sensor.ColorStream.Disable();
    
             
                
    
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
    
    
    
                checkir = 0;
                sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
                sensor.ColorFrameReady += irframeReady;
                sensor.ColorStream.Disable();
               
    
            }
    
    
    
    
            ////////////////////////////////////////
                    }
                }
                
    

    Wednesday, April 15, 2015 11:17 AM

All replies

  • I had only a quick look on the code but it seems Kinect v1 code. For example, in v2 SDK there are no ColorStreams anymore. Kinect is not backwards compatible so you cannot run v1 code with v2 sensor.

    In case you are working with v1 sensor and SDK this is not the proper forum, you should post here.

    Anyway it seems you are disabling ColorStream just after enabling it and this won't work. Look at the v1 Toolkit samples to see how you should work with ColorStream.


    Wednesday, April 15, 2015 5:11 PM