private void ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { ColorImageFrame frame = e.OpenColorImageFrame(); if (frame != null) { if (frame.Format != this.oldformat) { int bpp = frame.Format == ColorImageFormat.RgbResolution640x480Fps30 ? 4 : 2; this.colorimage = new byte[640 * 480 * bpp]; this.oldformat = frame.Format; this.DisposeTextures(); } this.FInvalidate = true; this.frameindex = frame.FrameNumber; lock (m_lock) { frame.CopyPixelDataTo(this.colorimage); } frame.Dispose(); } }
void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { depthframe = e.OpenDepthImageFrame(); colorframe = e.OpenColorImageFrame(); if (depthframe != null && colorframe != null) { depthpixelData = new short[depthframe.PixelDataLength]; depthframe.CopyPixelDataTo(depthpixelData); _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0); colorpixelData = new byte[colorframe.PixelDataLength]; colorframe.CopyPixelDataTo(colorpixelData); if (depthpixelData != null) { RangeFilter(); } _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0); depthframe.Dispose(); colorframe.Dispose(); } }
//[MethodImpl(MethodImplOptions.Synchronized)] private void OnColorFrameReady(object sender, ColorImageFrameReadyEventArgs eventArgs) { ColorImageFrame frame = eventArgs.OpenColorImageFrame(); if (frame != null) { try { if (colorPixelData.Length != frame.PixelDataLength) { colorPixelData = new byte[frame.PixelDataLength]; } frame.CopyPixelDataTo(colorPixelData); unsafe { fixed(byte *p = colorPixelData) { foreach (ColorFrameListener listener in new List <ColorFrameListener>(colorFrameListeners)) { listener.onColorFrameReady((IntPtr)p, frame.Width, frame.Height, ColorFormat.YUV); } } } } finally { if (frame != null) { frame.Dispose(); } } } }
void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { #region 基本彩色影像與深度影像處理 depthframe = e.OpenDepthImageFrame(); colorframe = e.OpenColorImageFrame(); if (depthframe == null || colorframe == null) { return; } depthpixelData = new short[depthframe.PixelDataLength]; depthframe.CopyPixelDataTo(depthpixelData); _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0); depthPixel = new DepthImagePixel[depthframe.PixelDataLength]; depthframe.CopyDepthImagePixelDataTo(depthPixel); colorpixelData = new byte[colorframe.PixelDataLength]; colorframe.CopyPixelDataTo(colorpixelData); #endregion if (depthpixelData != null) { PlayerFilter(); Alarm(); } _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0); depthframe.Dispose(); colorframe.Dispose(); }
void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { depthframe = e.OpenDepthImageFrame(); colorframe = e.OpenColorImageFrame(); if (depthframe == null || colorframe == null) { return; } depthpixelData = new short[depthframe.PixelDataLength]; depthframe.CopyPixelDataTo(depthpixelData); _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0); depthPixel = new DepthImagePixel[depthframe.PixelDataLength]; depthframe.CopyDepthImagePixelDataTo(depthPixel); colorpixelData = new byte[colorframe.PixelDataLength]; colorframe.CopyPixelDataTo(colorpixelData); if (depthpixelData != null) { UserBorderCaculation(); } _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0); depthframe.Dispose(); colorframe.Dispose(); }
/// <summary> /// Handles the Kinect AllFramesReady event /// </summary> private void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorFrame = null; DepthImageFrame depthFrame = null; Skeleton[] skeletonData; try { colorFrame = e.OpenColorImageFrame(); depthFrame = e.OpenDepthImageFrame(); using (var skeletonFrame = e.OpenSkeletonFrame()) { if (colorFrame == null || depthFrame == null || skeletonFrame == null) { return; } skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletonData); } // Find a skeleton to track. // First see if our old one is good. // When a skeleton is in PositionOnly tracking state, don't pick a new one // as it may become fully tracked again. Skeleton skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingId == this.trackedSkeletonId && s.TrackingState != SkeletonTrackingState.NotTracked); if (skeletonOfInterest == null) { // Old one wasn't around. Find any skeleton that is being tracked and use it. skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked); if (skeletonOfInterest != null) { this.trackedSkeletonId = skeletonOfInterest.TrackingId; } } if (this.FrameDataUpdated != null) { this.FrameDataUpdated(this, new FrameData(colorFrame, depthFrame, skeletonOfInterest)); } } finally { if (colorFrame != null) { colorFrame.Dispose(); } if (depthFrame != null) { depthFrame.Dispose(); } } }
static void ImageFrameReady(Object sender, ColorImageFrame imageFrame) { //Console.WriteLine("Foobar"); if (imageFrame != null) { Bitmap bmap = ImageToBitmap(imageFrame, true); form.GetPictureBox1().Image = bmap; imageFrame.Dispose(); } }
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(); } } }
void FrameReady(object sender, ColorImageFrameReadyEventArgs e) { ColorImageFrame imageFrame = e.OpenColorImageFrame(); if (imageFrame != null) { Bitmap bmap = ImageToBitmap(imageFrame); pictureBox1.Image = bmap; imageFrame.Dispose(); } }
protected virtual void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { colorFrame.Dispose(); desktopFrame.Dispose(); } disposed = true; }
static void ColorImageFrameReady(object sender, ColorImageFrameReadyEventArgs e) { while (imageSyncBlocked) { ; } imageSyncBlocked = true; ColorImageFrame temp = e.OpenColorImageFrame(); vid = frameToImage(temp); imageSyncBlocked = false; temp.Dispose(); }
//Converts image to bitmap BitmapSource ImageToBitmap(ColorImageFrame Image) { if (Image != null) { Image.CopyPixelDataTo(this.colorPixels); BitmapSource bmap = BitmapSource.Create(Image.Width, Image.Height, 10, 10, PixelFormats.Bgr32, null, this.colorPixels, Image.Width * Image.BytesPerPixel); Image.Dispose(); return(bmap); } else { return(null); } }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; bool openColorFrame = (IsTrackingFace || IsWritingColorStream); bool openDepthFrame = (IsTrackingFace || IsWritingDepthStream); bool openSkeletonFrame = (IsTrackingFace || IsTrackingSkeletons); try { if (openColorFrame) { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); } if (openDepthFrame) { depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); } if (openSkeletonFrame) { skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); } ProcessFrame(colorImageFrame); ProcessFrame(depthImageFrame); ProcessFrame(skeletonFrame); int skeletonFrameNumber = (skeletonFrame != null ? skeletonFrame.FrameNumber : -1); ProcessData(skeletonFrameNumber); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
/// <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> /// Event handler for Kinect sensor's OnAllFramesReady event /// </summary> private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; //DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); //depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || /*depthImageFrame == null || */ skeletonFrame == null) { return; } HandleColorImage(colorImageFrame); //HandleDepthImage(depthImageFrame); HandleSkeleton(skeletonFrame); DrawOutput(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } /* * if (depthImageFrame != null) * { * depthImageFrame.Dispose(); * } */ if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
private void ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { ColorImageFrame frame = e.OpenColorImageFrame(); if (frame != null) { this.FInvalidate = true; //this.frameindex = frame.FrameNumber; lock (m_colorlock) { frame.CopyPixelDataTo(this.colorimage); //Marshal.Copy(frame..Image.Bits, 0, this.colorimage, 640 * 480 * 4); } this.FInvalidate = true; this.frameindex = frame.FrameNumber; frame.Dispose(); } }
public void OnPhotoReady(object sender, ColorImageFrameReadyEventArgs args) { ColorImageFrame frame = args.OpenColorImageFrame(); if (frame == null) { return; } byte[] colorPixels = new byte[_kinectOutput.kinectSensor.ColorStream.FramePixelDataLength]; frame.CopyPixelDataTo(colorPixels); frame.Dispose(); Application.Current.Dispatcher.Invoke(() => { _colorBitmap.WritePixels( new Int32Rect(0, 0, this._colorBitmap.PixelWidth, this._colorBitmap.PixelHeight), colorPixels, _colorBitmap.PixelWidth * sizeof(int), 0); }); }
void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { #region 基本影像處理 depthframe = e.OpenDepthImageFrame(); colorframe = e.OpenColorImageFrame(); if (depthframe == null || colorframe == null) { return; } depthpixelData = new short[depthframe.PixelDataLength]; depthframe.CopyPixelDataTo(depthpixelData); depthPixel = new DepthImagePixel[depthframe.PixelDataLength]; depthframe.CopyDepthImagePixelDataTo(depthPixel); _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0); colorpixelData = new byte[colorframe.PixelDataLength]; colorframe.CopyPixelDataTo(colorpixelData); #endregion #region 座標轉換與過濾 colorpoints = new ColorImagePoint[depthpixelData.Length]; kinect.CoordinateMapper.MapDepthFrameToColorFrame( depthframe.Format, depthPixel, colorframe.Format, colorpoints); if (depthpixelData != null) { RangeFilter(); } #endregion _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0); depthframe.Dispose(); colorframe.Dispose(); }
void KinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { ColorImageFrame colorImageFrame = null; colorImageFrame = e.OpenColorImageFrame(); if (colorImageFrame == null) { return; } colorImageFrame.CopyPixelDataTo(this.colorPixels); this.colorBitmap.WritePixels( new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight), this.colorPixels, this.colorBitmap.PixelWidth * sizeof(int), 0); if (colorImageFrame != null) { colorImageFrame.Dispose(); } }
private void Sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { ColorImageFrame frame = e.OpenColorImageFrame(); if (frame != null) { CurrentFrame++; byte[] pixelData = new byte[frame.PixelDataLength]; frame.CopyPixelDataTo(pixelData); if (isWorking == false) { isWorking = true; t = new Thread(() => { colorSet(pixelData); }); t.Start(); } //Modify Text lbl_status.Text = "Data : Color Data Received"; frame.Dispose(); } else { //If frame is null do nothing... //Modify Text lbl_status.Text = "Data : No Data Received"; } }
//Event Kinect New Frame void AllFramesReady(object sender, AllFramesReadyEventArgs e) { int index = Array.IndexOf(this.sensors, (KinectSensor)sender); KinectSensor sensor = (KinectSensor)sender; //Console.WriteLine("Sensor" + index + " fired."); // Grab frames from stream DepthImageFrame imageDepthFrame = e.OpenDepthImageFrame(); ColorImageFrame imageColorFrame = e.OpenColorImageFrame(); SkeletonFrame imageSkeletonFrame = e.OpenSkeletonFrame(); if (imageDepthFrame != null && (!colorOn || imageColorFrame != null) && imageSkeletonFrame != null && Semaphore.glControlLoaded) { CoordinateMapper mapper = new CoordinateMapper(sensor); SkeletonPoint[] skeletonPoints = new SkeletonPoint[imageDepthFrame.PixelDataLength]; DepthImagePixel[] depthPixels = new DepthImagePixel[imageDepthFrame.PixelDataLength]; // Copy the pixel data from the image to a temporary array imageDepthFrame.CopyDepthImagePixelDataTo(depthPixels); // Map Depth data to Skeleton points. // skeletonPoints is being changed as a result of this function call mapper.MapDepthFrameToSkeletonFrame(DEPTH_IMAGE_FORMAT, depthPixels, skeletonPoints); short[,] vertexData; if (colorOn) { //Allocate array for color data pixelColorData = new byte[sensor.ColorStream.FramePixelDataLength]; imageColorFrame.CopyPixelDataTo(pixelColorData); // Adjust coordinates of skeleton points according to the colour format // skeletonPoints is being changed as a result of this function call mapper.MapColorFrameToSkeletonFrame(COLOR_IMAGE_FORMAT, DEPTH_IMAGE_FORMAT, depthPixels, skeletonPoints); vertexData = new short[(frameHeight * frameWidth), 6]; } else { vertexData = new short[(frameHeight * frameWidth), 3]; } // Convert SkeletonPoints data into short[][] // [x, y, z, Blue, Green, Red] int i = 0; for (int row = 0; row < frameHeight * frameWidth; row++) { vertexData[row, 0] = (short)(skeletonPoints[row].X * 1000); //Store for X vertexData[row, 1] = (short)(skeletonPoints[row].Y * 1000); //Store for Y vertexData[row, 2] = (short)(skeletonPoints[row].Z * 1000); //Store for Z if (colorOn) { vertexData[row, 3] = (short)pixelColorData[i + 2]; vertexData[row, 4] = (short)pixelColorData[i + 1]; vertexData[row, 5] = (short)pixelColorData[i]; i += 4; } } // Pass data to write to file if (Semaphore.readyForPCD)//change to use semaphore { Semaphore.passPCD(vertexData, index); } // Dispose frames for memory imageDepthFrame.Dispose(); if (colorOn) { imageColorFrame.Dispose(); } imageSkeletonFrame.Dispose(); } }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for image format changes. The FaceTracker doesn't // deal with that so we need to reset. if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // Get the skeleton information if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); // Update the list of trackers and the trackers with the current frame information foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
private void KinectSensorOnAllFramesReady(object sender, AllFramesReadyEventArgs e) { bool formatChanged = false; ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); if (colorImageFrame != null) { if (_currentKinectFormat.ColorImageFormat != colorImageFrame.Format) { formatChanged = true; _currentKinectFormat.ColorImageFormat = colorImageFrame.Format; } } if (depthImageFrame != null) { if (_currentKinectFormat.DepthImageFormat != depthImageFrame.Format) { formatChanged = true; _currentKinectFormat.DepthImageFormat = depthImageFrame.Format; var depthWidth = depthImageFrame.Width; var depthHeight = depthImageFrame.Height; FaceTrackingVM.DepthWidth = depthWidth; FaceTrackingVM.DepthHeight = depthHeight; this._depthImageData = new byte[depthImageFrame.PixelDataLength * 4]; this._modDepthImageData = new byte[depthImageFrame.PixelDataLength * 4]; this._mappedColorImageData = new byte[depthImageFrame.PixelDataLength * 4]; this._depthImageWritableBitmap = new WriteableBitmap( depthWidth, depthHeight, 96, 96, PixelFormats.Bgr32, null); this._modDepthImageWritableBitmap = new WriteableBitmap( depthWidth, depthHeight, 96, 96, PixelFormats.Bgr32, null); _colorImageWritableBitmap = new WriteableBitmap( depthWidth, depthHeight, 96, 96, PixelFormats.Bgr32, null); ColorImage = _colorImageWritableBitmap; DepthImage = _depthImageWritableBitmap; FusionInputImage = _modDepthImageWritableBitmap; } } skeletonFrame = e.OpenSkeletonFrame(); if (skeletonFrame != null) { if (_currentKinectFormat.NumSkeletons != skeletonFrame.SkeletonArrayLength) { _currentKinectFormat.NumSkeletons = skeletonFrame.SkeletonArrayLength; formatChanged = true; } } if (formatChanged) { _kinectFrameWorkItemPool.Format = _currentKinectFormat; } if (colorImageFrame != null && depthImageFrame != null && skeletonFrame != null) { var workItem = _kinectFrameWorkItemPool.Pop(); workItem.FrameNumber = depthImageFrame.FrameNumber; colorImageFrame.CopyPixelDataTo(workItem.ColorPixels); depthImageFrame.CopyDepthImagePixelDataTo(workItem.DepthImagePixels); skeletonFrame.CopySkeletonDataTo(workItem.Skeletons); var mapper = KinectSensor.CoordinateMapper; mapper.MapColorFrameToDepthFrame(workItem.Format.ColorImageFormat, workItem.Format.DepthImageFormat, workItem.DepthImagePixels, workItem.ColorMappedToDepthPoints); if (_kinectWorkQueue != null) { _kinectWorkQueue.AddWork(workItem); } } } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for image format changes. The FaceTracker doesn't // deal with that so we need to reset. if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // Get the skeleton information if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); // Update the list of trackers and the trackers with the current frame information var i = 0; foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } var position = skeleton.Position; if (position.X != 0 && position.Y != 0 && position.Z != 0) { Console.WriteLine($"Face {i}: X {position.X}, Y {position.Y}, Z {position.Z}"); string text = position.X + "@x " + position.Y + "@y " + position.Z + "@z"; System.IO.File.WriteAllText(@"C:\Users\Corbin Pixels\Desktop\kinect\FaceTrackingBasics-WPF\coords.txt", text); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } i += 1; } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
public void Dispose() { colorFrame.Dispose(); desktopFrame.Dispose(); }
void KinectFaceNode_AllFrameReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; if (face == null) { face = new FaceTracker(this.runtime.Runtime); } colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); skeletonFrame = e.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.runtime.Runtime, ColorImageFormat.RgbResolution640x480Fps30, colorImage, DepthImageFormat.Resolution320x240Fps30, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); colorImageFrame.Dispose(); depthImageFrame.Dispose(); skeletonFrame.Dispose(); this.FInvalidate = true; }
private void AllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for changes in any of the data this function is receiving // and reset things appropriately. if (this.depthImageFormat != depthImageFrame.Format) { this.DestroyFaceTracker(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.DestroyFaceTracker(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; this.colorImageWritableBitmap = null; this.ColorImage.Source = null; this.theMaterial.Brush = null; } if (this.skeletonData != null && this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = null; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.colorImageWritableBitmap == null) { this.colorImageWritableBitmap = new WriteableBitmap( colorImageFrame.Width, colorImageFrame.Height, 96, 96, PixelFormats.Bgr32, null); this.ColorImage.Source = this.colorImageWritableBitmap; this.theMaterial.Brush = new ImageBrush(this.colorImageWritableBitmap) { ViewportUnits = BrushMappingMode.Absolute }; } if (this.skeletonData == null) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } // Copy data received in this event to our buffers. colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); this.colorImageWritableBitmap.WritePixels( new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height), this.colorImage, colorImageFrame.Width * Bgr32BytesPerPixel, 0); // Find a skeleton to track. // First see if our old one is good. // When a skeleton is in PositionOnly tracking state, don't pick a new one // as it may become fully tracked again. Skeleton skeletonOfInterest = this.skeletonData.FirstOrDefault( skeleton => skeleton.TrackingId == this.trackingId && skeleton.TrackingState != SkeletonTrackingState.NotTracked); if (skeletonOfInterest == null) { // Old one wasn't around. Find any skeleton that is being tracked and use it. skeletonOfInterest = this.skeletonData.FirstOrDefault( skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked); if (skeletonOfInterest != null) { // This may be a different person so reset the tracker which // could have tuned itself to the previous person. if (this.faceTracker != null) { this.faceTracker.ResetTracking(); } this.trackingId = skeletonOfInterest.TrackingId; } } bool displayFaceMesh = false; if (skeletonOfInterest != null && skeletonOfInterest.TrackingState == SkeletonTrackingState.Tracked) { if (this.faceTracker == null) { try { this.faceTracker = new FaceTracker(this.Kinect); } catch (InvalidOperationException) { // During some shutdown scenarios the FaceTracker // is unable to be instantiated. Catch that exception // and don't track a face. Debug.WriteLine("AllFramesReady - creating a new FaceTracker threw an InvalidOperationException"); this.faceTracker = null; } } if (this.faceTracker != null) { FaceTrackFrame faceTrackFrame = this.faceTracker.Track( this.colorImageFormat, this.colorImage, this.depthImageFormat, this.depthImage, skeletonOfInterest); if (faceTrackFrame.TrackSuccessful) { this.UpdateMesh(faceTrackFrame); // Only display the face mesh if there was a successful track. displayFaceMesh = true; } } } else { this.trackingId = -1; } this.viewport3d.Visibility = displayFaceMesh ? Visibility.Visible : Visibility.Hidden; } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
private void OnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; try { colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame(); depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame(); skeletonFrame = allFramesReadyEventArgs.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { return; } // Check for image format changes. The FaceTracker doesn't // deal with that so we need to reset. if (this.depthImageFormat != depthImageFrame.Format) { this.ResetFaceTracking(); this.depthImage = null; this.depthImageFormat = depthImageFrame.Format; } if (this.colorImageFormat != colorImageFrame.Format) { this.ResetFaceTracking(); this.colorImage = null; this.colorImageFormat = colorImageFrame.Format; } // Create any buffers to store copies of the data we work with if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } // Get the skeleton information if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); // Update the list of trackers and the trackers with the current frame information int num = 0; Skeleton sk1 = null, sk2 = null; foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked) { if (num == 0) { sk1 = skeleton; } else { sk2 = skeleton; } num++; } } if (sk1 != null) { if (sk2 == null) { if (!this.trackedSkeletons.ContainsKey(sk1.TrackingId)) { this.trackedSkeletons.Add(sk1.TrackingId, new SkeletonFaceTracker()); Gesture.images.Add(sk1.TrackingId, 1); } } else { if (!this.trackedSkeletons.ContainsKey(sk1.TrackingId)) { if (!this.trackedSkeletons.ContainsKey(sk2.TrackingId))//都无 { this.trackedSkeletons.Add(sk1.TrackingId, new SkeletonFaceTracker()); this.trackedSkeletons.Add(sk2.TrackingId, new SkeletonFaceTracker()); Gesture.images.Add(sk1.TrackingId, 1); Gesture.images.Add(sk2.TrackingId, 2); } else//1无2有 { this.trackedSkeletons.Add(sk1.TrackingId, new SkeletonFaceTracker()); if (Gesture.images[sk2.TrackingId] == 1) { Gesture.images.Add(sk1.TrackingId, 2); } else { Gesture.images.Add(sk1.TrackingId, 1); } } } else { if (!this.trackedSkeletons.ContainsKey(sk2.TrackingId))//1有2无 { this.trackedSkeletons.Add(sk2.TrackingId, new SkeletonFaceTracker()); if (Gesture.images[sk1.TrackingId] == 1) { Gesture.images.Add(sk2.TrackingId, 2); } else { Gesture.images.Add(sk2.TrackingId, 1); } } } } } SkeletonFaceTracker skeletonFaceTracker; if (sk1 != null) { if (this.trackedSkeletons.TryGetValue(sk1.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, sk1); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } if (sk2 != null) { if (this.trackedSkeletons.TryGetValue(sk2.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.Kinect, colorImageFormat, colorImage, depthImageFormat, depthImage, sk2); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); this.InvalidateVisual(); } finally { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } } }
void FramesReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame VFrame = e.OpenColorImageFrame(); if (VFrame == null) { return; } byte[] pixelS = new byte[VFrame.PixelDataLength]; Bitmap bmap = ImageToBitmap(VFrame); SkeletonFrame SFrame = e.OpenSkeletonFrame(); if (SFrame == null) { return; } Graphics g = Graphics.FromImage(bmap); Skeleton[] Skeletons = new Skeleton[SFrame.SkeletonArrayLength]; SFrame.CopySkeletonDataTo(Skeletons); foreach (Skeleton S in Skeletons) { //List<JointInfo> jointInfos = new List<JointInfo>(); if (S.TrackingState == SkeletonTrackingState.Tracked) { foreach (JointType j in Enum.GetValues(typeof(JointType))) { JointInfo.Update(S.Joints[j]); //jointInfos.Add(new JointInfo(S.Joints[j])); } for (int i = 0; i < 20; i++) { if (JointInfo.allJoints[i] != null) { jointLabels[i].Text = JointInfo.allJoints[i].ToString(); } else { jointLabels[i].Text = " "; } } foreach (JointInfo j in JointInfo.allJoints) { if (j != null) { MarkAtxy(j.position, Brushes.Blue, g); } } //body DrawBone(JointType.Head, JointType.ShoulderCenter, S, g); DrawBone(JointType.ShoulderCenter, JointType.Spine, S, g); DrawBone(JointType.Spine, JointType.HipCenter, S, g); //left leg DrawBone(JointType.HipCenter, JointType.HipLeft, S, g); DrawBone(JointType.HipLeft, JointType.KneeLeft, S, g); DrawBone(JointType.KneeLeft, JointType.AnkleLeft, S, g); DrawBone(JointType.AnkleLeft, JointType.FootLeft, S, g); //Right Leg DrawBone(JointType.HipCenter, JointType.HipRight, S, g); DrawBone(JointType.HipRight, JointType.KneeRight, S, g); DrawBone(JointType.KneeRight, JointType.AnkleRight, S, g); DrawBone(JointType.AnkleRight, JointType.FootRight, S, g); //Left Arm DrawBone(JointType.ShoulderCenter, JointType.ShoulderLeft, S, g); DrawBone(JointType.ShoulderLeft, JointType.ElbowLeft, S, g); DrawBone(JointType.ElbowLeft, JointType.WristLeft, S, g); DrawBone(JointType.WristLeft, JointType.HandLeft, S, g); //Right Arm DrawBone(JointType.ShoulderCenter, JointType.ShoulderRight, S, g); DrawBone(JointType.ShoulderRight, JointType.ElbowRight, S, g); DrawBone(JointType.ElbowRight, JointType.WristRight, S, g); DrawBone(JointType.WristRight, JointType.HandRight, S, g); } } pictureBox1.Image = bmap; SFrame.Dispose(); VFrame.Dispose(); }
void KinectFaceNode_AllFrameReady(object sender, AllFramesReadyEventArgs e) { ColorImageFrame colorImageFrame = null; DepthImageFrame depthImageFrame = null; SkeletonFrame skeletonFrame = null; colorImageFrame = e.OpenColorImageFrame(); depthImageFrame = e.OpenDepthImageFrame(); skeletonFrame = e.OpenSkeletonFrame(); if (colorImageFrame == null || depthImageFrame == null || skeletonFrame == null) { if (colorImageFrame != null) { colorImageFrame.Dispose(); } if (depthImageFrame != null) { depthImageFrame.Dispose(); } if (skeletonFrame != null) { skeletonFrame.Dispose(); } return; } if (first) { first = false; this.olddepth = depthImageFrame.Format; } else { if (this.olddepth != depthImageFrame.Format) { //Need a reset if (this.depthImage != null) { this.depthImage = null; } foreach (SkeletonFaceTracker sft in this.trackedSkeletons.Values) { sft.Dispose(); } this.trackedSkeletons.Clear(); this.olddepth = depthImageFrame.Format; } } if (this.depthImage == null) { this.depthImage = new short[depthImageFrame.PixelDataLength]; } if (this.colorImage == null) { this.colorImage = new byte[colorImageFrame.PixelDataLength]; } if (this.skeletonData == null || this.skeletonData.Length != skeletonFrame.SkeletonArrayLength) { this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength]; } colorImageFrame.CopyPixelDataTo(this.colorImage); depthImageFrame.CopyPixelDataTo(this.depthImage); skeletonFrame.CopySkeletonDataTo(this.skeletonData); foreach (Skeleton skeleton in this.skeletonData) { if (skeleton.TrackingState == SkeletonTrackingState.Tracked || skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { // We want keep a record of any skeleton, tracked or untracked. if (!this.trackedSkeletons.ContainsKey(skeleton.TrackingId)) { this.trackedSkeletons.Add(skeleton.TrackingId, new SkeletonFaceTracker()); } // Give each tracker the upated frame. SkeletonFaceTracker skeletonFaceTracker; if (this.trackedSkeletons.TryGetValue(skeleton.TrackingId, out skeletonFaceTracker)) { skeletonFaceTracker.OnFrameReady(this.runtime.Runtime, colorImageFrame.Format, colorImage, depthImageFrame.Format, depthImage, skeleton); skeletonFaceTracker.LastTrackedFrame = skeletonFrame.FrameNumber; } } } this.RemoveOldTrackers(skeletonFrame.FrameNumber); colorImageFrame.Dispose(); depthImageFrame.Dispose(); skeletonFrame.Dispose(); this.FInvalidate = true; }