/// <summary> /// Converts a color frame to a System.Media.ImageSource with its background removed. /// </summary> /// <param name="frame">A BackgroundRemovedColorFrame generated from a Kinect sensor.</param> /// <param name="format">The pixel format.</param> /// <returns>The specified frame in a System.media.ImageSource format.</returns> public static ImageSource ToBitmap(this BackgroundRemovedColorFrame frame, System.Windows.Media.PixelFormat format) { byte[] pixels = new byte[frame.PixelDataLength]; frame.CopyPixelDataTo(pixels); return(pixels.ToBitmap(frame.Width, frame.Height, format)); }
/// <summary> /// Update background removed color frame. /// </summary> /// <param name="frame">The input frame.</param> public void UpdateBackgroundRemovedColorFrame(BackgroundRemovedColorFrame frame) { if (frame == null) { throw new ArgumentNullException("frame"); } this.timestamp = frame.Timestamp; this.width = frame.Width; this.height = frame.Height; this.bufferLength = frame.PixelDataLength; this.trackedPlayerId = frame.TrackedPlayerId; this.averageDepth = frame.AverageDepth; if ((this.Buffer == null) || (this.Buffer.Length != this.bufferLength)) { this.Buffer = new byte[this.bufferLength]; } unsafe { fixed(byte *messageDataPtr = this.Buffer) { fixed(byte *frameDataPtr = frame.GetRawPixelData()) { byte *messageDataPixelPtr = messageDataPtr; byte *frameDataPixelPtr = frameDataPtr; byte *messageDataPixelPtrEnd = messageDataPixelPtr + this.bufferLength; while (messageDataPixelPtr != messageDataPixelPtrEnd) { // Convert from BGRA to RGBA format *(messageDataPixelPtr++) = *(frameDataPixelPtr + 2); *(messageDataPixelPtr++) = *(frameDataPixelPtr + 1); *(messageDataPixelPtr++) = *frameDataPixelPtr; *(messageDataPixelPtr++) = *(frameDataPixelPtr + 3); frameDataPixelPtr += BytesPerPixel; } } } } }
private void ResetGame() { savedBackground = null; GoodJob.Visibility = System.Windows.Visibility.Hidden; switchImage(); }
private void BackgroundRemovedFrameReadyHandler(object sender, BackgroundRemovedColorFrameReadyEventArgs e) { using (var backgroundRemovedFrame = e.OpenBackgroundRemovedColorFrame()) { if (backgroundRemovedFrame != null) { if (null == this.foregroundBitmap || this.foregroundBitmap.PixelWidth != backgroundRemovedFrame.Width || this.foregroundBitmap.PixelHeight != backgroundRemovedFrame.Height) { // WriteableBitmap bit = WriteableBitmap(Happy.Source); this.foregroundBitmap = new WriteableBitmap(backgroundRemovedFrame.Width, backgroundRemovedFrame.Height, 96.0, 96.0, PixelFormats.Bgra32, null); // Set the image we display to point to the bitmap where we'll put the image data this.MaskedColor.Source = this.foregroundBitmap; } if (original == 0) { original = image.Width; } if (stage == 0) { if (s != null && currentlyTrackedSkeletonId >= 0) { stage = 1; } } else if (stage == 1) { counter++; Matching.FontSize = 30; Matching.Text = "Initializing"; if (counter == 20) { stage = 2; counter = 0; } } else if (stage == 2) { counter++; Matching.FontSize = 60; Matching.Text = ((105 - counter) / 35 + 1).ToString(); if (counter == 105) { stage = 3; counter = 0; } } else if (stage == 3 || stage == 4) { if (stage == 3 && counter < 40) { counter++; Matching.FontSize = 60 - counter; Matching.Text = "Go"; if (counter == 40) { stage = 4; counter = 0; } } if (stage == 4) { image.Width = sizeIm; if (sizeIm < foregroundBitmap.PixelWidth + 100) { sizeIm += rate; var total = 0; if (s != null) { foreach (Joint joint in s.Joints) { ColorImagePoint point = ks.CoordinateMapper.MapSkeletonPointToColorPoint(joint.Position, ColorImageFormat.RgbResolution640x480Fps30); if (point.X > -1000000 && point.X < 1000000 && point.Y > -1000000 && point.Y < 1000000) { Joint j = joint; int pos = point.Y * stride + 4 * point.X; if (pos >= 0 && pos < sil.Length && sil[pos] == 255 && sil[pos + 1] == 255 && sil[pos + 2] == 255) { total += 1; } } } Matching.Text = "Matching: " + (total * 100 / s.Joints.Count).ToString() + "%"; } else { Matching.Text = "Matching: 0%"; } Matching.FontSize = 30; } else { stage = 5; savedBackground = backgroundRemovedFrame; savedForeground = foregroundBitmap; // UpdateImage("Images//Bent.png"); // image.Width = sizeIm; // image.UpdateLayout(); } } } else if (stage == 5) { counter++; GoodJob.Visibility = System.Windows.Visibility.Visible; if (image == Dog) { GoodJob.Text = "Woof! Bark!"; } else { GoodJob.Text = "Good Job"; } if (counter == 100) { stage = 6; counter = 0; GoodJob.Visibility = System.Windows.Visibility.Hidden; } // Happy.Visibility = System.Windows.Visibility.Hidden; // HappyEnd.Visibility = System.Windows.Visibility.Visible; // HappyEnd.Width = Happy.Width; // this.MaskedColor.Source = wb; // backgroundRemovedFrame = br; /* foreach (Joint joint in jc) { ColorImagePoint point = ks.CoordinateMapper.MapSkeletonPointToColorPoint(joint.Position, ColorImageFormat.RgbResolution640x480Fps30); if (point.X > -1000000 && point.X < 1000000 && point.Y > -1000000 && point.Y < 1000000) { Joint j = joint; int pos = point.Y * stride + 4 * point.X; if (pos >= 0 && pos < sil.Length && sil[pos] == 255 && sil[pos + 1] == 255 && sil[pos + 2] == 255) { // MaskedColor. } } }*/ } else if (stage == 6) { sizeIm -= 4; if (sizeIm > startingSize) { image.Width = sizeIm; } else { stage = 1; counter = 0; rate += 1; ResetGame(); } } if (savedBackground == null) { // Write the pixel data into our bitmap this.foregroundBitmap.WritePixels( new Int32Rect(0, 0, this.foregroundBitmap.PixelWidth, this.foregroundBitmap.PixelHeight), backgroundRemovedFrame.GetRawPixelData(), this.foregroundBitmap.PixelWidth * sizeof(int), 0); } else { // Write the pixel data into our bitmap /* this.foregroundBitmap.WritePixels( new Int32Rect(0, 0, this.savedForeground.PixelWidth, this.savedForeground.PixelHeight), savedBackground.GetRawPixelData(), this.savedForeground.PixelWidth * sizeof(int), 0);*/ } } } }
/// <summary> /// Converts a color frame to a System.Media.ImageSource with its background removed. /// </summary> /// <param name="frame">A BackgroundRemovedColorFrame generated from a Kinect sensor.</param> /// <returns>The specified frame in a System.media.ImageSource format.</returns> public static ImageSource ToBitmap(this BackgroundRemovedColorFrame frame) { return(frame.ToBitmap(PixelFormats.Bgra32)); }