private void HandModule_FrameProcessed(object sender, RS.FrameProcessedEventArgs args) { handData.Update(); IHand[] ileftHand = null; IHand[] irightHand = null; if (handData.QueryHandData(AccessOrderType.ACCESS_ORDER_LEFT_HANDS, out ileftHand) == RS.Status.STATUS_NO_ERROR) { leftHand = new Hand(ileftHand[0]); } else { leftHand = null; } if (handData.QueryHandData(AccessOrderType.ACCESS_ORDER_RIGHT_HANDS, out irightHand) == RS.Status.STATUS_NO_ERROR) { rightHand = new Hand(irightHand[0]); } else { rightHand = null; } if (irightHand != null || ileftHand != null) { HandDataChanged.Invoke(rightHand, leftHand); DataStreamUpdate.Invoke(GetAllHandDataAttribute(rightHand, leftHand), GetPreprocessedHand(rightHand, leftHand)); } sm.ReleaseFrame(); }
/* Displaying current frames hand joints */ private void DisplayJoints(HandData handOutput, long timeStamp = 0) { if (_form.GetJointsState() || _form.GetSkeletonState() || _form.GetExtremitiesState()) { //Iterate hands var nodes = new JointData[][] { new JointData[0x20], new JointData[0x20] }; var extremityNodes = new ExtremityData[][] { new ExtremityData[0x6], new ExtremityData[0x6] }; int numOfHands = handOutput.NumberOfHands; if (numOfHands == 1) { _mCursorPoints[1].Clear(); } for (int i = 0; i < 2; i++) { //Get hand by time of appearance IHand handData; if (handOutput.QueryHandData(AccessOrderType.ACCESS_ORDER_BY_TIME, i, out handData) == Status.STATUS_NO_ERROR) { if (handData != null) { var nodeIndex = handData.BodySide == BodySideType.BODY_SIDE_LEFT ? 0 : 1; //Iterate Joints foreach (var tJ in handData.TrackedJoints) { nodes[nodeIndex][(int)tJ.Key] = tJ.Value; } // end iterating over joints // iterate over extremity points if (_form.GetExtremitiesState()) { for (int j = 0; j < HandData.NUMBER_OF_EXTREMITIES; j++) { foreach (var eP in handData.ExtremityPoints) { extremityNodes[nodeIndex][(int)eP.Key] = eP.Value; } } } } } } // end iterating over hands _form.DisplayJoints(nodes, 2); if (numOfHands > 0) { if (_form.GetExtremitiesState()) { _form.DisplayExtremities(numOfHands, extremityNodes); } } } }
/* 显示当前帧手关节*/ private void DisplayJoints(HandData handOutput, long timeStamp = 0) { if (_form.GetJointsState() || _form.GetSkeletonState() || _form.GetExtremitiesState()) { //迭代手 var nodes = new JointData[][] { new JointData[0x20], new JointData[0x20] }; var extremityNodes = new ExtremityData[][] { new ExtremityData[0x6], new ExtremityData[0x6] }; int numOfHands = handOutput.NumberOfHands; if (numOfHands == 1) { _mCursorPoints[1].Clear(); } for (int i = 0; i < numOfHands; i++) { //在出现的时候检测手 IHand handData; if (handOutput.QueryHandData(AccessOrderType.ACCESS_ORDER_BY_TIME, i, out handData) == Status.STATUS_NO_ERROR) { if (handData != null) { //迭代手关节 foreach (var tJ in handData.TrackedJoints) { nodes[i][(int)tJ.Key] = tJ.Value; } // 手关节迭代结束 // 迭代手边缘点 if (_form.GetExtremitiesState()) { for (int j = 0; j < HandData.NUMBER_OF_EXTREMITIES; j++) { foreach (var eP in handData.ExtremityPoints) { extremityNodes[i][(int)eP.Key] = eP.Value; } } } } } } //迭代手结束 _form.DisplayJoints(nodes, numOfHands); if (numOfHands > 0) { if (_form.GetExtremitiesState()) { _form.DisplayExtremities(numOfHands, extremityNodes); } } } }
/* 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(); } } }