1

我需要用图像替换不是播放器的背景像素,我可以用 ColorFrameImage 成功替换用户像素,但是一旦我尝试用图像像素替换其余的深度位。. 我收到 XamlParseError 异常

更新: ims.PixelHeight 和 ims.PixelWidth 导致异常。.

我可能将位图图像转换为像素错误。.

有什么解决办法吗?

这是代码:

public partial class MainWindow : Window
{

    Byte[] pixels;

    Byte[] pix;

    Byte[] pixy;

    BitmapImage ims;

    public MainWindow()
    {
        InitializeComponent();


        ims = new BitmapImage();
        ims.BeginInit();
        ims.UriSource = new Uri(@"/autumn_scene.jpg", UriKind.RelativeOrAbsolute);

        //int height = ims.PixelHeight;
       // int width = ims.PixelWidth;
        //int nStride = (ims.PixelWidth * ims.Format.BitsPerPixel + 7) / 8;

      //  pixy = new byte[(ims.PixelHeight* 4+1)];

      //  MessageBox.Show("Pixel Height" + ims.PixelHeight);
        MessageBox.Show("I hate Exceptions");

        //System.Console.WriteLine("Pixel Height" + ims.PixelHeight);

   //     ims.CopyPixels(pixy, nStride, 0);

      //  ims.EndInit();

        }




    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
    }

    void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        KinectSensor oldSensor = (KinectSensor)e.OldValue;
        StopKinect(oldSensor);

        KinectSensor newSensor = (KinectSensor)e.NewValue;

        //NOTE: Add this check for a null sensor
        if (newSensor == null)
        {
            return;
        }


        newSensor.ColorStream.Enable();
        newSensor.DepthStream.Enable();
        newSensor.SkeletonStream.Enable();
        newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);

        try 
{
    newSensor.Start();
}
catch (System.IO.IOException)
{

 kinectSensorChooser1.AppConflictOccurred();
}
    }

    void newSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
    {

    using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (depthFrame == null)
            {
                return;
            }

            if (colorFrame == null)
            {
                return;
            }


            pixels = GenerateColoredBytes(depthFrame,colorFrame);

            //pix = new byte[colorFrame.PixelDataLength];
            //colorFrame.CopyPixelDataTo(pix);

            int stride2 = colorFrame.Width * 4;

            image1.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride2);

            //Number of bytes per row
            int stride = depthFrame.Width * 4;


           image2.Source =  BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
          //  image2.Source = ims;
        }


    }

   private byte[] GenerateColoredBytes(DepthImageFrame depthFrame, ColorImageFrame colorFrame)

    {



        //Getting raw depth data

        short[] rawDepthData = new short[depthFrame.PixelDataLength];
        depthFrame.CopyPixelDataTo(rawDepthData);



        pixels = new byte[depthFrame.Width * depthFrame.Height * 4];

        pix = new byte[colorFrame.Width * colorFrame.Height * 4];




        colorFrame.CopyPixelDataTo(pix);

     //  ColorImagePoint mapval = depthFrame.MapToColorImagePoint(depthFrame.Width,depthFrame.Height,
        //Getting & Setting the BGR values
        const int BlueIndex = 0;
        const int GreenIndex = 1;
        const int RedIndex = 2;
     //   const int alpha = 3;



       // int nStride = (ims.PixelWidth * ims.Format.BitsPerPixel + 7) / 8;

       //byte[] pixy = new byte[ims.PixelHeight * nStride];

       // ims.CopyPixels(pixy,nStride,0);


       for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) {
            //Player formula
            int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;

            //Depth / Distance formula
            int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;


            if (player > 0)
            {
                pixels[colorIndex + BlueIndex] = pix[colorIndex + BlueIndex];
                pixels[colorIndex + GreenIndex] = pix[colorIndex + GreenIndex];
                pixels[colorIndex + RedIndex] = pix[colorIndex + RedIndex];

              //  pixels[colorIndex + BlueIndex] = 
            }else{

                //if (colorIndex + RedIndex < pixy.Length)
                //{

                //    pixels[colorIndex + BlueIndex] = pixy[colorIndex + BlueIndex];
                //    pixels[colorIndex + GreenIndex] = pixy[colorIndex + GreenIndex];
                //    pixels[colorIndex + RedIndex] = pixy[colorIndex + RedIndex];
                //}
                //else
                {
                    pixels[colorIndex + BlueIndex] = 255;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 0;
                    //   pixels[colorIndex + alpha] = 0;
                }

            }
       }

            return pixels;
   }

void StopKinect(KinectSensor sensor){
        if (sensor != null)
        {
            //NOTE: Add this code
            if (sensor.IsRunning)
            {
                sensor.Stop();
                if (sensor.AudioSource != null)
                {
                    sensor.AudioSource.Stop();
                }
            }
        }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        StopKinect(kinectSensorChooser1.Kinect);
    }
}
4

0 回答 0