/// <summary> /// 彩色图回调函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { //获取彩色坐标信息 FrameDescription colorFrameDescription = colorFrame.FrameDescription; //绘制彩色图 using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); //分析数据将新的彩色图数据写入位图 if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { count += 1; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } //on first image recieved if (count == 1) { { if (this.colorBitmap != null) { // create a png bitmap encoder which knows how to save a .png file BitmapEncoder encoder = new PngBitmapEncoder(); // create frame from the writable bitmap and add to encoder encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap)); string path = Path.Combine("test_images", "image1.jpg"); // write the new file to disk try { // FileStream is IDisposable using (FileStream fs = new FileStream(path, FileMode.Create)) { encoder.Save(fs); } } catch (IOException) { Debug.WriteLine("failed to send screencap"); } } }; } }
//重点 /// <summary> /// 处理从传感器接收到的颜色帧数据 /// </summary> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrameArrivedEventArgs:为事件传递参数,返回当前彩色图像帧 // ColorFrame:表示某一帧彩色图像帧 using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { // FrameDescription:表示来自Kinect传感器的图像帧的属性 FrameDescription colorFrameDescription = colorFrame.FrameDescription; // 运行Kinect实例访问内存 using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // 不知道什么意思,可能是对内存的读写控制 this.colorBitmap.Lock(); // 验证数据并将新的颜色框数据写入显示位图 if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { // 没找到,但我之前见过 colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); // 不知道什么意思,估计是画图 this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } // 不知道什么意思,可能是对内存的读写控制 this.colorBitmap.Unlock(); } } } }
void DepthFrameReader_FrameArrived(object sender, DepthFrameArrivedEventArgs e) { using (DepthFrame depthFrame = e.FrameReference.AcquireFrame()) { if (depthFrame != null) { if (mIs15Fps) { if (mDiscardFrame) { mDiscardFrame = false; return; } else { mDiscardFrame = true; } } using (KinectBuffer depthBuffer = depthFrame.LockImageBuffer()) { if (((mFrameDescription.Width * mFrameDescription.Height) == (depthBuffer.Size / mFrameDescription.BytesPerPixel))) { ProcessDepthFrameData(depthBuffer); mFeedPixels(mDepthPixels); } } } } }
private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e) { bool depthFrameProcessed = false; using (DepthFrame depthFrame = e.FrameReference.AcquireFrame()) { if (depthFrame != null) { using (KinectBuffer depthBuffer = depthFrame.LockImageBuffer()) { if ((depthFrameDescription.Width * depthFrameDescription.Height) == (depthBuffer.Size / depthFrameDescription.BytesPerPixel)) { ushort maxDepth = ushort.MaxValue; ProcessDepthFrameData(depthBuffer.UnderlyingBuffer, depthBuffer.Size, depthFrame.DepthMinReliableDistance, maxDepth); depthFrameProcessed = true; } } } } if (depthFrameProcessed) { depthBitmap.WritePixels( new Int32Rect(0, 0, depthBitmap.PixelWidth, depthBitmap.PixelHeight), depthPixels, depthBitmap.PixelWidth, 0); } }
private void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame frame = e.FrameReference.AcquireFrame()) { if (frame == null) { return; } using (KinectBuffer buffer = frame.LockRawImageBuffer()) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; Rectangle rect = new Rectangle(0, 0, width, height); uint size = (uint)(width * height * 4); if (bmp == null) { bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); } BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); frame.CopyConvertedFrameDataToIntPtr(bmpData.Scan0, size, ColorImageFormat.Bgra); bmp.UnlockBits(bmpData); } } if (bmp != null) { pictureBox1.Image = bmp; pictureBox1.Refresh(); } }
//private FrameDescription depthFrameDescription = null; internal DFrame(DepthFrame depthFrame) { this.frameDescriptor = depthFrame.FrameDescription; this.width = depthFrame.FrameDescription.Width; this.height = depthFrame.FrameDescription.Height; this.pixels = new ushort[width * height]; // this.depthFrameDescription = this.kinectSensor.DepthFrameSource.FrameDescription; this.WriteableBitmap = new WriteableBitmap( this.width, this.height, 96.0, 96.0, PixelFormats.Bgr32, null); this.WriteableBitmapForDoorNavigation = new WriteableBitmap(this.width, this.height, 96.0, 96.0, PixelFormats.Gray8, null); using (KinectBuffer depthBuffer = depthFrame.LockImageBuffer()) { // ushort maxDepth = ushort.MaxValue; this.ProcessDepthFrameData(depthFrame, depthBuffer.UnderlyingBuffer, depthBuffer.Size); } this.ProcessBitmap(); // creates bitmap, and bitmap source }
/// <summary> /// Handles the color frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { //use the current image frame in a memory safe manner using (ColorFrame frame = e.FrameReference.AcquireFrame()) { //sometimes this frame isn't available //defensive programming if (frame == null) { return; } using (KinectBuffer colorBuffer = frame.LockRawImageBuffer()) { //threadsafe lock on this data so it doesn't get modified pushupColorBitmap.Lock(); //let application to know where the image is being stored frame.CopyConvertedFrameDataToIntPtr(pushupColorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); //the number is width*height*bytes per pixel //redraw the screen in this area pushupColorBitmap.AddDirtyRect(new Int32Rect(0, 0, pushupColorBitmap.PixelWidth, pushupColorBitmap.PixelHeight)); //unlock pushupColorBitmap.Unlock(); } } }
public void PollMostRecentDepthFrame() { MultiSourceFrame multiFrame = _reader.AcquireLatestFrame(); if (multiFrame == null) { return; } using (DepthFrame frame = multiFrame.DepthFrameReference.AcquireFrame()) { if (frame == null) { return; // Could not find multi-frame or depth-frame } using (KinectBuffer buffer = frame.LockImageBuffer()) { if (DepthFrameDescription.Width * DepthFrameDescription.Height == buffer.Size / DepthFrameDescription.BytesPerPixel) { ProcessDepthFrameData( buffer.UnderlyingBuffer, buffer.Size, frame.DepthMinReliableDistance, ushort.MaxValue); } } } }
//színes képi adatok kezelése, ami a Kinect felől jön private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame elérhető using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); // adatok ellenőrzése és továbbítása a bittérképként a colorBitmap bufferbe if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
private void ProcessColorFrame(ColorFrame colorFrame) { if ((null == colorFrame)) { return; } FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.m_colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.m_colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.m_colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.m_colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.m_colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.m_colorBitmap.PixelWidth, this.m_colorBitmap.PixelHeight)); //this.m_colorBitmap.AddDirtyRect(new Int32Rect(m_drawingRegion.Left, m_drawingRegion.Top, m_drawingRegion.Right, m_drawingRegion.Bottom)); } this.m_colorBitmap.Unlock(); } }
/// <summary> /// Display color image on image control /// </summary> /// <param name="bitmap">color bitmap</param> /// <param name="image">color image control</param> /// <param name="args">frame args</param> private void RenderColorImage(ref WriteableBitmap bitmap, System.Windows.Controls.Image image, ColorFrameArrivedEventArgs args) { using (ColorFrame colorFrame = args.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } else { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { bitmap.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr( bitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); bitmap.Unlock(); image.Source = bitmap; } } } }
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorBitmap.Lock(); if ((colorFrameDescription.Width == colorBitmap.PixelWidth) && (colorFrameDescription.Height == colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); } colorBitmap.Unlock(); } } } }
/// <summary> /// Use color frame and store to a WritableBitmap /// </summary> private void CaptureOriginalColorFrame(ColorFrame e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.capturedBackground.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this.capturedBackground.PixelWidth) && (colorFrameDescription.Height == this.capturedBackground.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.capturedBackground.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.capturedBackground.AddDirtyRect(new Int32Rect(0, 0, this.capturedBackground.PixelWidth, this.capturedBackground.PixelHeight)); } this.capturedBackground.Unlock(); } } } }
private void HandleColorFrame(ColorFrame colorFrame) { if (colorFrame == null) { return; } FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.currentViewModel.VideoImageSource.Lock(); if ((colorFrameDescription.Width == this.currentViewModel.VideoImageSource.PixelWidth) && (colorFrameDescription.Height == this.currentViewModel.VideoImageSource.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this.currentViewModel.VideoImageSource.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this.currentViewModel.VideoImageSource.AddDirtyRect( new Int32Rect(0, 0, this.currentViewModel.VideoImageSource.PixelWidth, this.currentViewModel.VideoImageSource.PixelHeight)); } this.currentViewModel.VideoImageSource.Unlock(); } }
private void ColorReader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { if (this.processingColorFrame) { return; } this.processingColorFrame = true; bool colorFrameProcessed = false; // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { if (colorFrame.RawColorImageFormat == ColorImageFormat.Rgba) { colorFrame.CopyRawFrameDataToArray(this.colorPixels); } else { colorFrame.CopyConvertedFrameDataToArray(this.colorPixels, ColorImageFormat.Rgba); } colorFrameProcessed = true; } } } if (colorFrameProcessed) { this.colorFrameCallback(this.colorPixels); } this.processingColorFrame = false; }
// ColorFrameReader event handler void colorFrameReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame == null) { return; } // Locks raw pixel data for the captured frame using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorBitmap.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), ColorImageFormat.Bgra); // Defines the entire image as update space colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); colorBitmap.Unlock(); } } }
/// <summary> /// Processes the Depth Image. /// </summary> /// <param name="frame">The frame.</param> private void ProcessDepthImage(DepthFrame frame) { if (updateDepthTexture) { return; } if (frame != null) { FrameDescription frameDescription = frame.FrameDescription; // the fastest way to process the body index data is to directly access // the underlying buffer using (KinectBuffer buffer = frame.LockImageBuffer()) { // verify data and write the color data to the display bitmap if (((frameDescription.Width * frameDescription.Height) == (buffer.Size / frameDescription.BytesPerPixel)) && (frameDescription.Width == this.depthTexture.Width) && (frameDescription.Height == this.depthTexture.Height)) { this.ProcessGrayFrameData(buffer.UnderlyingBuffer, buffer.Size, this.depthMinReliableDistance, this.depthMaxReliableDistance, frameDescription.BytesPerPixel, this.depthData); this.updateDepthTexture = true; } } } }
void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // Get the current image frame in a memory-safe manner using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { // Defensive programming: Just in case the sensor frame is no longer valid, exit the function if (colorFrame == null) { return; } using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { // Put a thread-safe lock on this data so it doesn't get modified elsewhere colorBitmap.Lock(); // Let the application know where the image is being stored colorFrame.CopyConvertedFrameDataToIntPtr( colorBitmap.BackBuffer, (uint)(1920 * 1080 * 4), // Width * Height * BytesPerPixel ColorImageFormat.Bgra); // Let the application know that it needs to redraw the screen in this area (the whole image) colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); // Remove the thread-safe lock on this data colorBitmap.Unlock(); } } }
private bool aquireBodyDataAndBuffers( BodyFrame bodyFrame, DepthFrame depthFrame, BodyIndexFrame bodyIndexFrame, out Body body, out KinectBuffer depthBuffer, out KinectBuffer bodyIndexBuffer) { depthBuffer = null; bodyIndexBuffer = null; body = getActiveBody(bodyFrame); if (body == null) { return(false); } depthBuffer = depthFrame.LockImageBuffer(); var width = depthFrameDescription.Width; var height = depthFrameDescription.Height; if (depthBuffer == null || (width * height) != (depthBuffer.Size / this.depthFrameDescription.BytesPerPixel)) { return(false); } bodyIndexBuffer = bodyIndexFrame.LockImageBuffer(); if (bodyIndexBuffer == null || bodyIndexBuffer.Size * 2 != depthBuffer.Size) { depthBuffer.Dispose(); return(false); } return(true); }
public void PollMostRecentInfraredFrame() { MultiSourceFrame multiFrame = _reader.AcquireLatestFrame(); if (multiFrame == null) { return; } using (InfraredFrame frame = multiFrame.InfraredFrameReference.AcquireFrame()) { if (frame == null) { return; // Could not find multi-frame or infrared-frame } using (KinectBuffer buffer = frame.LockImageBuffer()) { if (InfraredFrameDescription.Width * InfraredFrameDescription.Height == buffer.Size / InfraredFrameDescription.BytesPerPixel) { ProcessInfraredFrameData(buffer.UnderlyingBuffer, buffer.Size); } } } }
public unsafe void ParseToBitmaps(ColorFrame colorFrame, out Bitmap bitmap, out WriteableBitmap writeableBitmap) { int width = colorFrame.FrameDescription.Width; int height = colorFrame.FrameDescription.Height; WriteableBitmap colorWBitmap = new WriteableBitmap(width, height, 96.0, 96.0, PixelFormats.Bgr32, null); Bitmap colorBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { colorWBitmap.Lock(); colorFrame.CopyConvertedFrameDataToIntPtr( colorWBitmap.BackBuffer, (uint)(colorFrame.FrameDescription.Width * colorFrame.FrameDescription.Height * 4), ColorImageFormat.Bgra); colorWBitmap.AddDirtyRect(new Int32Rect(0, 0, colorWBitmap.PixelWidth, colorWBitmap.PixelHeight)); colorWBitmap.Unlock(); writeableBitmap = colorWBitmap; BitmapData bitmapData = colorBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, colorBitmap.PixelFormat); Buffer.MemoryCopy(colorWBitmap.BackBuffer.ToPointer(), bitmapData.Scan0.ToPointer(), colorFrame.FrameDescription.Width * colorFrame.FrameDescription.Height * 4, colorFrame.FrameDescription.Width * colorFrame.FrameDescription.Height * 4); colorBitmap.UnlockBits(bitmapData); bitmap = colorBitmap; } }
public void PollMostRecentSilhouetteFrame() { MultiSourceFrame multiFrame = _reader.AcquireLatestFrame(); if (multiFrame == null) { return; } using (BodyIndexFrame frame = multiFrame.BodyIndexFrameReference.AcquireFrame()) { if (frame == null) { return; // Could not find multi-frame or body index frame } using (KinectBuffer buffer = frame.LockImageBuffer()) { if ( SilhouetteFrameDescription.Width * SilhouetteFrameDescription.Height == buffer.Size) { ProcessSilhouetteData(buffer.UnderlyingBuffer, buffer.Size); } } } }
public void ParseToBitmaps(DepthFrame depthFrame, out Bitmap bitmap, out WriteableBitmap writeableBitmap) { int width = depthFrame.FrameDescription.Width; int height = depthFrame.FrameDescription.Height; Bitmap depthBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); WriteableBitmap depthWBitmap = new WriteableBitmap(width, height, 96.0, 96.0, PixelFormats.Gray8, null); using (KinectBuffer depthBuffer = depthFrame.LockImageBuffer()) { ushort maxDepth = ushort.MaxValue; //maxDepth = depthFrame.DepthMaxReliableDistance; //TODO: make this a setting? byte[] depthPixels = ConvertDepthFrameData(depthFrame.FrameDescription, depthBuffer.UnderlyingBuffer, depthBuffer.Size, depthFrame.DepthMinReliableDistance, maxDepth); //Create writeable bitmap depthWBitmap.WritePixels(new Int32Rect(0, 0, depthWBitmap.PixelWidth, depthWBitmap.PixelHeight), depthPixels, depthWBitmap.PixelWidth, 0); writeableBitmap = depthWBitmap; //Create bitmap BitmapData bitmapData = depthBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, depthBitmap.PixelFormat); IntPtr intPointer = bitmapData.Scan0; Marshal.Copy(depthPixels, 0, intPointer, depthPixels.Length); depthBitmap.UnlockBits(bitmapData); bitmap = depthBitmap; } }
private void UpdateCameraView(MultiSourceFrame multiframe) { using (ColorFrame colorFrame = multiframe.ColorFrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this._colorBitmap.Lock(); // verify data and write the new color frame data to the display bitmap if ((colorFrameDescription.Width == this._colorBitmap.PixelWidth) && (colorFrameDescription.Height == this._colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr( this._colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); this._colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this._colorBitmap.PixelWidth, this._colorBitmap.PixelHeight)); } this._colorBitmap.Unlock(); } } } }
private void Read_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) { colorFrame.CopyConvertedFrameDataToIntPtr(colorBitmap.BackBuffer, (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), ColorImageFormat.Bgra); //colorframe의 현제 프레임 정보를 colorbitmap에 복사하는 기능, 이미지 크기 = 가로*세로*4바이트의 색상, 이미지포멧은 Bgra포맷 this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); } this.colorBitmap.Unlock(); } } } }
/// <summary> /// Performs color based threshold on the color frame /// </summary> /// <param name="colorFrame"></param> /// <returns></returns> private async Task <Tuple <Mat, Mat> > ProcessColorFrame(ColorFrame colorFrame) { return(await Task.Run(() => { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { if (convertedColorDataPtr == IntPtr.Zero) { convertedColorDataPtr = Marshal.AllocHGlobal(4 * (int)colorFrameDescription.LengthInPixels); } colorFrame.CopyConvertedFrameDataToIntPtr(convertedColorDataPtr, 4 * colorFrameDescription.LengthInPixels, ColorImageFormat.Bgra); Mat resizedImage = new Mat(); using (Mat convertedImage_Bgr = new Mat(colorFrameDescription.Height, colorFrameDescription.Width, DepthType.Cv8U, 3)) using (Mat convertedImage_Bgra = new Mat(colorFrameDescription.Height, colorFrameDescription.Width, DepthType.Cv8U, 4, convertedColorDataPtr, colorFrameDescription.Width * 4)) { CvInvoke.CvtColor(convertedImage_Bgra, convertedImage_Bgr, ColorConversion.Bgra2Bgr); CvInvoke.Resize(convertedImage_Bgr, resizedImage, new System.Drawing.Size(imageSizeX, imageSizeY)); if (inspectPosition != null) { InspectPixel(resizedImage); } } //return Tuple.Create(resizedImage, CannyShapeDetection(resizedImage)); return Tuple.Create(resizedImage, ChromaShapeDetection(resizedImage)); } })); }
private static void Reader_FrameArrived(object sender, MultiSourceFrameArrivedEventArgs e) { if (e.FrameReference != null) { MultiSourceFrame multiFrame = e.FrameReference.AcquireFrame(); if (multiFrame.DepthFrameReference != null) { try { using (DepthFrame depthFrame = multiFrame.DepthFrameReference.AcquireFrame()) { if (depthFrame != null) { using (KinectBuffer buffer = depthFrame.LockImageBuffer()) { depthFrameDescription = depthFrame.FrameDescription; depthWidth = depthFrameDescription.Width; depthHeight = depthFrameDescription.Height; depthFrameData = new ushort[depthWidth * depthHeight]; depthFrame.CopyFrameDataToArray(depthFrameData); } } } } catch (Exception) { return; } } } }
private unsafe void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { // ColorFrame is IDisposable using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { FrameDescription colorFrameDescription = colorFrame.FrameDescription; using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { this.colorBitmap.Lock(); byte[] colorData = new byte[this.colorFrameDescription.Width * this.colorFrameDescription.Height * this.bytesPerPixel]; colorFrame.CopyConvertedFrameDataToArray(colorData, ColorImageFormat.Bgra); /** * Nothing works, not sure if it will work, I think the code is too inefficient to be executed at a rate to refresh the * frame properly. Although I am leary of this assessment because the coordinate mapping sample has a for loop that * executes the same number of times and includes 4 conditionals. This code has no problem refreshing the image. * * Note: Even when part of the image was displaying and being processed, the offset between the depth and color * pixels was not being resolved. **/ // verify data and write the new color frame data to the display bitmap // if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight)) // { // colorFrame.CopyConvertedFrameDataToIntPtr( // this.colorBitmap.BackBuffer, // (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), // ColorImageFormat.Bgra); //this.colorBitmap.CopyPixels(colorData, this.colorBitmap.BackBufferStride, 0); for (int i = 0; i < colorMappedToDepthPoints.Length / 3; i++) { DepthSpacePoint dPoint = this.colorMappedToDepthPoints[i]; if (float.IsNegativeInfinity((float)dPoint.X)) { colorData[i * 4] = 0; // Console.Write(i.ToString() + ","); } } this.colorBitmap.WritePixels(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight), colorData, this.colorBitmap.BackBufferStride, 0); // this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight)); // } this.colorBitmap.Unlock(); } } } }
private unsafe void ProcessDepthFrameData(KinectBuffer buffer, DepthFrame depthFrame) { ushort* frameData = (ushort*)buffer.UnderlyingBuffer; ushort minDepth = (ushort)(depthFrame.DepthMinReliableDistance * DEPTH_SCALE); ushort maxDepth = (ushort)(depthFrame.DepthMaxReliableDistance * DEPTH_SCALE); int maxCounter = (int)(buffer.Size / depthFrame.FrameDescription.BytesPerPixel); for (int i = 0; i < maxCounter; ++i) { ushort depth = frameData[i]; depth *= DEPTH_SCALE; depthPixels[i] = (ushort)(depth >= minDepth && depth <= maxDepth ? depth : 0); } }