Example #1
0
        /* 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();
                }
            }
        }