1

I'm trying to capture RBG video from Kinect.

I have wrote this code in C# but i don't know how to save frame in a avi/mpeg file. In the first method I set the sensor for rgb video, in the second method I receive the frame from kinect sensor.

void RecordVideo()
{
    foreach (var kinectCollegata in KinectSensor.KinectSensors)
    {
        if (kinectCollegata.Status == KinectStatus.Connected)
        {
            this.sensor = kinectCollegata;
            break;
        }
    }

    if (this.sensor != null)
    {
        this.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;

        this.sensor.Start();
        var parameters = new TransformSmoothParameters
        {
            Smoothing = 0.3f,
            Correction = 0.0f,
            Prediction = 0.0f,
            JitterRadius = 1.0f,
            MaxDeviationRadius = 0.5f
        };

        this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);

        this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
        sensor.Start();
    }
}

private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    try
    {
        Skeleton[] skeletons = new Skeleton[0];
        SkeletonFrame skeletonFrame;
        using (skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }
    }
    catch (Exception exc)
    {
    }
}

How I can modify my code for writing video file on the disc?


Thaks for your reply, i have make this.

1)

this.sensor.ColorFrameReady += this.ColorImageReady;

2)

private void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e)
{
   using (ColorImageFrame imageFrame = e.OpenColorImageFrame())
   {
      if (imageFrame != null)
      {
         Bitmap image = ImageToBitmap(imageFrame);
         int width = 320;
         int height = 240;
         // create instance of video writer
         VideoFileWriter writer = new VideoFileWriter();
         // create new video file
         writer.Open("test.avi", width, height, 25, VideoCodec.MPEG4);
         // create a bitmap to save into the video file
         for (int i = 0; i < 1000; i++)
         {
            image.SetPixel(i % width, i % height, System.Drawing.Color.Red);
            writer.WriteVideoFrame(image);
         }
         writer.Close();
         }
     }
  }

        Bitmap ImageToBitmap(ColorImageFrame Image)
        {
            byte[] pixeldata = new byte[Image.PixelDataLength];
            Image.CopyPixelDataTo(pixeldata);
            Bitmap bmap = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            BitmapData bmapdata = bmap.LockBits(
            new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
            ImageLockMode.WriteOnly,
                bmap.PixelFormat);
            IntPtr ptr = bmapdata.Scan0;
            Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
            bmap.UnlockBits(bmapdata);
            return bmap;
        }

But when i run my application a have this exception:

System.IO.FileNotFoundException non รจ stata gestita
  _HResult=-2147024770
  _message=Impossibile caricare il file o l'assembly 'AForge.Video.FFMPEG.dll' o una delle relative dipendenze. Impossibile trovare il modulo specificato.
  HResult=-2147024770
  IsTransient=false
  Message=Impossibile caricare il file o l'assembly 'AForge.Video.FFMPEG.dll' o una delle relative dipendenze. Impossibile trovare il modulo specificato.
  Source=Disfagia
  FileName=AForge.Video.FFMPEG.dll
  FusionLog=""
  StackTrace:
       in Disfagia.MainWindow.ColorImageReady(Object sender, ColorImageFrameReadyEventArgs e)
       in Microsoft.Kinect.ContextEventHandler`1.SendOrPostDelegate(Object state)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.DispatcherOperation.InvokeImpl()
       in System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Windows.Threading.DispatcherOperation.Invoke()
       in System.Windows.Threading.Dispatcher.ProcessQueue()
       in System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       in MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       in MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       in System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       in System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       in System.Windows.Threading.Dispatcher.Run()
       in System.Windows.Application.RunDispatcher(Object ignore)
       in System.Windows.Application.RunInternal(Window window)
       in System.Windows.Application.Run(Window window)
       in System.Windows.Application.Run()
       in Disfagia.App.Main() in c:\Users\michele.castriotta\Documents\Visual Studio 2013\Projects\Disfagia\Disfagia\obj\Debug\App.g.cs:riga 0
       in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
bircastri
  • 153
  • 4
  • 13

1 Answers1

1

Try the following:

  • Subscribe the ColorFrameReady event from the sensor.
  • In the event handler convert the color image from the kinect to a Bitmap. See here.
  • Download the AForge library and add a reference to the "AForge.Video.FFMPEG" assembly.
  • Use the VideoFileWriter class to save the frames to a video file.
Community
  • 1
  • 1
Thomas Hetzer
  • 1,537
  • 13
  • 24