ColorImageFrame不能用于kinect v2(VS2013 C#kinectSDK V2)

时间:2015-03-07 05:01:53

标签: c# opencv kinect emgucv

(RgbResolution640x480Fps30)有问题

      public partial class TrainWPF : Window
{

    KinectSensor kinectSensor = null;
    ColorImageFormat imageFormat = ColorImageFormat.RgbResolution640x480Fps30;       

ColorImageFrame最初可以使用kinect v1,但不能在kinect v2中使用如何在KINECT v2中使用ColorImageFrame ///// stop& ColorFrameReady& Dispose无法使用

            private void DeActivateSensor()
    {
        if (kinectSensor != null)
        {
            kinectSensor.Stop();
            kinectSensor.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(sensor_ColorFrameReady);
            kinectSensor.Dispose();
        }
    }

    private void SetupSensorVideoInput()
    {
        if (kinectSensor != null)
        {
            imageFormat = (ColorImageFormat)cmbDisplayMode.SelectedItem;
            kinectSensor.ColorStream.Enable(imageFormat);

            kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(sensor_ColorFrameReady);
            kinectSensor.Start();
        }
    }        
    void sensor_ColorFrameReady(object sender, ColorFrameArrivedEventArgs e)
    {
        using (ColorImageFrame image = e.OpenColorImageFrame())
        {
            if (image == null)
                return;

            Image<Bgr, byte> currentImage = EmguImageExtensions.ToOpenCVImage<Bgr, byte>(image);
            Image<Gray, byte> grayFrame = currentImage.Convert<Gray, byte>();

            System.Drawing.Rectangle[] facesDetected = Face.DetectMultiScale(grayFrame, 1.2, 10, new System.Drawing.Size(50, 50), System.Drawing.Size.Empty);

            for (int i = 0; i < facesDetected.Length; i++)// (Rectangle face_found in facesDetected)
            {
                //This will focus in on the face from the haar results its not perfect but it will remove a majoriy
                //of the background noise

                facesDetected[i].X += (int)(facesDetected[i].Height * 0.15);
                facesDetected[i].Y += (int)(facesDetected[i].Width * 0.22);
                facesDetected[i].Height -= (int)(facesDetected[i].Height * 0.3);
                facesDetected[i].Width -= (int)(facesDetected[i].Width * 0.35);

                result = currentImage.Copy(facesDetected[i]).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                result._EqualizeHist();
                face_PICBX.Source = result.ToBitmapSource();
            }

            if (colorBytes == null ||
                colorBytes.Length != image.PixelDataLength)
            {
                colorBytes = new byte[image.PixelDataLength];
            }

            image.CopyPixelDataTo(colorBytes);

            BitmapSource source = BitmapSource.Create(image.Width,
                image.Height,
                96,
                96,
                PixelFormats.Bgr32,
                null,
                colorBytes,
                image.Width * image.BytesPerPixel);
            picVideoDisplay.Source = source; 


        }
    }
    private 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;
    }

0 个答案:

没有答案