Example #1
0
        private static Bitmap ColorImageFrameToBitmap(ColorImageFrame colorFrame)
        {
            byte[] pixelBuffer = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixelBuffer);

            Bitmap bitmapFrame = new Bitmap(colorFrame.Width, colorFrame.Height, PixelFormat.Format32bppRgb);
            BitmapData bitmapData = bitmapFrame.LockBits(new Rectangle(0, 0, colorFrame.Width, colorFrame.Height), ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);

            IntPtr intPointer = bitmapData.Scan0;
            Marshal.Copy(pixelBuffer, 0, intPointer, colorFrame.PixelDataLength);

            bitmapFrame.UnlockBits(bitmapData);
            colorFrame.Dispose();

            return bitmapFrame;
        }
Example #2
0
        /// <summary>
        /// Sets the desired picture box image.  Can do so for up to 4 images.
        /// </summary>
        /// <param name="imageIndex">The index of the picture box image to set.</param>
        /// <param name="frame">The Kinect's color image frame to use.</param>
        void setImage(int imageIndex, ColorImageFrame frame)
        {
            // Check that the frame is not empty.
            if (frame != null)
            {
                // Create an array of bytes to hold the data.
                byte[] pixelData = new byte[frame.PixelDataLength];
                // Copy the pixel data to the array.
                frame.CopyPixelDataTo(pixelData);

                // Create a bmp to hold the actual image.
                Bitmap bmp = new Bitmap(frame.Width, frame.Height, PixelFormat.Format32bppRgb);
                // Create the bitmap data to hold the data for the image.
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
                // Create the pointer to the bmp data.
                IntPtr ptr = bmpData.Scan0;
                // Then copy the actual data to the bmp.
                System.Runtime.InteropServices.Marshal.Copy(pixelData, 0, ptr, frame.PixelDataLength);
                // Unlock the bmp data bits so they can be used.
                bmp.UnlockBits(bmpData);
                // Switch through the image index.
                switch (imageIndex)
                {
                    case 0:
                        // Set the color image for the 1st picture box to the bmp image.
                        this.kinectPB1.Image = bmp;
                        break;
                    case 1:
                        // Set the color image for the 2nd picture box to the bmp image.
                        this.kinectPB2.Image = bmp;
                        break;
                    case 2:
                        // Set the color image for the 3rd picture box to the bmp image.
                        this.kinectPB3.Image = bmp;
                        break;
                    case 3:
                        // Set the color image for the 4th picture box to the bmp image.
                        this.kinectPB4.Image = bmp;
                        break;
                }

                // Dispose of the frame.
                frame.Dispose();
            }
        }
        /// <summary>
        /// Sets the data of ColorVideo.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="colorImageFrame"></param>
        public void KinectColorFrameReady(object sender, ColorImageFrameReadyEventArgs colorImageFrame)
        {
            //Get raw image
            ColorVideoFrame = colorImageFrame.OpenColorImageFrame();

            if (ColorVideoFrame != null)
            {
                //Create array for pixel data and copy it from the image frame
                PixelData = new Byte[ColorVideoFrame.PixelDataLength];
                ColorVideoFrame.CopyPixelDataTo(PixelData);

                //Convert RGBA to BGRA, Kinect and XNA uses different color-formats.
                BgraPixelData = new Byte[ColorVideoFrame.PixelDataLength];
                for (int i = 0; i < PixelData.Length; i += 4)
                {
                    BgraPixelData[i] = PixelData[i + 2];
                    BgraPixelData[i + 1] = PixelData[i + 1];
                    BgraPixelData[i + 2] = PixelData[i];
                    BgraPixelData[i + 3] = (Byte)255; //The video comes with 0 alpha so it is transparent
                }

                // Create a texture and assign the realigned pixels
                ColorVideo = new Texture2D(Graphics.GraphicsDevice, ColorVideoFrame.Width, ColorVideoFrame.Height);
                ColorVideo.SetData(BgraPixelData);
                ColorVideoFrame.Dispose();
            }
        }
Example #4
0
        private void processFrame(ColorImageFrame colorFrame)
        {
            //using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
              {
                  if (colorFrame != null)
                  {
                      bool colorFormat = this.rgbImageFormat != colorFrame.Format;

                      if (colorFormat)
                      {
                          width = colorFrame.Width;
                          height = colorFrame.Height;

                          this.colorpixelData = new byte[colorFrame.PixelDataLength];
                          this.colorFrameRGB = new byte[colorFrame.Width * colorFrame.Height * Bgr32BytesPerPixel];

                          this.processedBitmap = new WriteableBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null);

                          //this.procImage.Source = this.processedBitmap; // do now or later?

                          //Console.WriteLine("Frame written");
                      }

                      colorFrame.CopyPixelDataTo(this.colorpixelData);

                      // Output raw image
                      this.outputColorBitmap = new WriteableBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null);
                      this.outputColorBitmap.WritePixels(
                          (new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height)),
                          colorpixelData,
                          colorFrame.Width * Bgr32BytesPerPixel,
                          0);
                      this.raw_image.Source = this.outputColorBitmap;

                      // PROCESS THE DATA //
                      colorpixelData = convertToHSL(colorpixelData);
                      frameProcessor(colorpixelData);
                      //processedcolorpixelData = colorpixelData;

                      // Output processed image
                      this.processedBitmap.WritePixels(
                          new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
                          processedcolorpixelData,
                          colorFrame.Width * Bgr32BytesPerPixel,
                          0);

                      this.rgbImageFormat = colorFrame.Format;

                      this.procImage.Source = this.processedBitmap;
                      //Console.WriteLine("Frame written");

                      colorFrame.Dispose();
                  }

              }
        }
        private ImageSource getHeadshot(Skeleton sk, ColorImageFrame colorFrame)
        {
            Joint head = sk.Joints[JointType.Head];
            // Magic numbers, head position seems to scale with depth, these values are calibrated for a specific depth
            Joint scaledHead = head.ScaleTo(640, 480, 1.25f, 0.9f);

            int x = (int)scaledHead.Position.X;
            int y = (int)scaledHead.Position.Y;
            if (colorFrame == null)
            {
                return null;
            }

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

            int stride = colorFrame.Width * 4;

            BitmapSource frame = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
            if (x - 32 >= 0 && y - 16 >= 0 && x - 32 < 640-65 && y - 16 < 480-65)
            {
                x = x - 32;
                y = y - 16;
                CroppedBitmap pic = new CroppedBitmap(frame, new Int32Rect(x, y, 65, 65));
                colorFrame.Dispose();
                return pic;
            }
            colorFrame.Dispose();
            return null;
        }