private void SampleArrived(object sender, SampleArrivedEventArgs args) { Image dimage = args.sample.Depth; if (dimage != null) { ImageData outBuffer; Status acquireAccessStatus = dimage.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_DEPTH_F32, out outBuffer); if (acquireAccessStatus != Status.STATUS_NO_ERROR) { Debug.Log(string.Format("Failed to acquire access to the image. Return code:{0}", acquireAccessStatus)); } var dwidth = dimage.Info.width; var dheight = dimage.Info.height; var centerIndex = ((640 * 190) + 320); var dpixels = outBuffer.ToFloatArray(0, 640 * dheight); var result = dpixels[centerIndex]; test = result / (float)2047; dimage.ReleaseAccess(outBuffer); } else { Debug.Log("Null Depth Image"); } }
public ColorDepthImage GetImage() { //Checks to make sure that the camera has been properly initialized if (Status == CameraStatus.Stopped) { throw new InvalidOperationException("The camera cannot produce data unless it is running"); } //Acquires the frame Status acquisitionResult = SMInstance.AcquireFrame(true); if (acquisitionResult.IsError()) { throw new Exception("Unable to capture frame: " + acquisitionResult); } Texture2D colorTex = new Texture2D(WIDTH, HEIGHT, TextureFormat.RGBA32, false); Texture2D depthTex = new Texture2D(WIDTH, HEIGHT, TextureFormat.RGBA32, false); //Extract the color image Image colorImage = SampleStream.Sample.Color; ImageData colorImageData; colorImage.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_RGB32, out colorImageData); colorImageData.ToTexture2D(0, colorTex); //Extract and map the depth image Image initialDepthImage = SampleStream.Sample.Depth; Projection proj = SMInstance.CaptureManager.Device.CreateProjection(); Image depthImage = proj.CreateDepthImageMappedToColor(initialDepthImage, colorImage); ImageData depthImageData; depthImage.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_RGB32, out depthImageData); depthImageData.ToTexture2D(0, depthTex); //Clean up resources colorImage.Dispose(); depthImage.Dispose(); initialDepthImage.Dispose(); proj.Dispose(); colorImage.ReleaseAccess(colorImageData); depthImage.ReleaseAccess(depthImageData); //Release the acquired frame SMInstance.ReleaseFrame(); return(new ColorDepthImage(colorTex, depthTex)); }
/* Displaying Depth/Mask Images - for depth image only we use a delay of NumberOfFramesToDelay to sync image with tracking */ private unsafe void DisplayPicture(Image depth, HandData handAnalysis) { if (depth == null) { return; } Image image = depth; //Mask Image if (_form.GetLabelmapState()) { System.Drawing.Bitmap labeledBitmap = null; try { int numOfHands = handAnalysis.NumberOfHands; PointI32[][] pointOuter = new PointI32[numOfHands][]; PointI32[][] pointInner = new PointI32[numOfHands][]; labeledBitmap = new System.Drawing.Bitmap(image.Info.width, image.Info.height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); for (int j = 0; j < numOfHands; j++) { int id; ImageData data; handAnalysis.QueryHandId(AccessOrderType.ACCESS_ORDER_BY_TIME, j, out id); //Get hand by time of appearance IHand handData; handAnalysis.QueryHandData(AccessOrderType.ACCESS_ORDER_BY_TIME, j, out handData); if (handData != null) { image = handData.SegmentationImage; if (image != null) { if (image.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_Y8, out data) >= Status.STATUS_NO_ERROR) { var rect = new System.Drawing.Rectangle(0, 0, image.Info.width, image.Info.height); var bitmapdata = labeledBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, labeledBitmap.PixelFormat); byte *numPtr = (byte *)bitmapdata.Scan0; //dst byte *numPtr2 = (byte *)data.planes[0]; //row int imagesize = image.Info.width * image.Info.height; byte num2 = (_form.GetFullHandModeState()) ? (byte)handData.BodySide : (byte)1; byte tmp = 0; for (int i = 0; i < imagesize; i++, numPtr += 4, numPtr2++) { tmp = (byte)(_lut[numPtr2[0]] * num2 * 100); numPtr[0] = (Byte)(tmp | numPtr[0]); numPtr[1] = (Byte)(tmp | numPtr[1]); numPtr[2] = (Byte)(tmp | numPtr[2]); numPtr[3] = 0xff; } labeledBitmap.UnlockBits(bitmapdata); image.ReleaseAccess(data); } if ((_form.GetContourState())) { int contourNumber = handData.NumberOfContours; if (contourNumber > 0) { for (int k = 0; k < contourNumber; ++k) { IContour contour = handData.Contours[k]; if (contour != null) { if (contour.Outer) { pointOuter[j] = contour.Points; } else { pointInner[j] = contour.Points; } } } } } } } } if (labeledBitmap != null) { _form.DisplayBitmap(labeledBitmap); labeledBitmap.Dispose(); } if (image != null) { image.Dispose(); } for (int i = 0; i < numOfHands; i++) { if (_form.GetContourState()) { if (pointOuter[i] != null && pointOuter[i].Length > 0) { _form.DisplayContour(pointOuter[i], i); } if (pointInner[i] != null && pointInner[i].Length > 0) { _form.DisplayContour(pointInner[i], i); } } } } catch (Exception) { if (labeledBitmap != null) { labeledBitmap.Dispose(); } if (image != null) { image.Dispose(); } } } //end label image //Depth Image else { //collecting 3 images inside a queue and displaying the oldest image ImageInfo info; Image image2; ImageData imageData = new ImageData(); info = image.Info; image2 = Image.CreateInstance(_form.session, info, imageData); //image2 = _form.g_session. CreateImage(info); if (image2 == null) { return; } image2.CopyImage(image); _mImages.Enqueue(image2); if (_mImages.Count == NumberOfFramesToDelay) { System.Drawing.Bitmap depthBitmap; try { depthBitmap = new System.Drawing.Bitmap(image.Info.width, image.Info.height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); } catch (Exception) { image.Dispose(); Image queImage = _mImages.Dequeue(); queImage.Dispose(); return; } ImageData data3; Image image3 = _mImages.Dequeue(); if (image3.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_DEPTH, out data3) >= Status.STATUS_NO_ERROR) { float fMaxValue = _maxRange; byte cVal; var rect = new System.Drawing.Rectangle(0, 0, image.Info.width, image.Info.height); var bitmapdata = depthBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, depthBitmap.PixelFormat); byte * pDst = (byte *)bitmapdata.Scan0; short *pSrc = (short *)data3.planes[0]; int size = image.Info.width * image.Info.height; for (int i = 0; i < size; i++, pSrc++, pDst += 4) { cVal = (byte)((*pSrc) / fMaxValue * 255); if (cVal != 0) { cVal = (byte)(255 - cVal); } pDst[0] = cVal; pDst[1] = cVal; pDst[2] = cVal; pDst[3] = 255; } try { depthBitmap.UnlockBits(bitmapdata); } catch (Exception) { image3.ReleaseAccess(data3); depthBitmap.Dispose(); image3.Dispose(); return; } _form.DisplayBitmap(depthBitmap); image3.ReleaseAccess(data3); } depthBitmap.Dispose(); image3.Dispose(); } } }
/* Displaying Depth/Mask Images - for depth image only we use a delay of NumberOfFramesToDelay to sync image with tracking */ private unsafe void DisplayPicture(Image depth, HandData handAnalysis) { if (depth == null) { return; } //Make Copy of Depth Image Image image = depth; //collecting 3 images inside a queue and displaying the oldest image ImageInfo info; Image image2; ImageData imageData = new ImageData(); info = image.Info; image2 = Image.CreateInstance(manager.Session, info, imageData); if (image2 == null) { return; } image2.CopyImage(image); _mImages.Enqueue(image2); if (_mImages.Count == NumberOfFramesToDelay) { System.Drawing.Bitmap depthBitmap; try { depthBitmap = new System.Drawing.Bitmap(image.Info.width, image.Info.height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); } catch (Exception) { image.Dispose(); Image queImage = _mImages.Dequeue(); queImage.Dispose(); return; } ImageData data3; Image image3 = _mImages.Dequeue(); if (image3.AcquireAccess(ImageAccess.ACCESS_READ, PixelFormat.PIXEL_FORMAT_DEPTH, out data3) >= Status.STATUS_NO_ERROR) { float fMaxValue = manager.GetDeviceRange(); byte cVal; var rect = new System.Drawing.Rectangle(0, 0, image.Info.width, image.Info.height); var bitmapdata = depthBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, depthBitmap.PixelFormat); byte * pDst = (byte *)bitmapdata.Scan0; short *pSrc = (short *)data3.planes[0]; int size = image.Info.width * image.Info.height; for (int i = 0; i < size; i++, pSrc++, pDst += 4) { cVal = (byte)((*pSrc) / fMaxValue * 255); if (cVal != 0) { cVal = (byte)(255 - cVal); } pDst[0] = cVal; pDst[1] = cVal; pDst[2] = cVal; pDst[3] = 255; } try { depthBitmap.UnlockBits(bitmapdata); } catch (Exception) { image3.ReleaseAccess(data3); depthBitmap.Dispose(); image3.Dispose(); return; } form.DisplayBitmap(depthBitmap); image3.ReleaseAccess(data3); } depthBitmap.Dispose(); image3.Dispose(); } }