コード例 #1
0
        private void Uninitialize()
        {
            if (senseManager != null)
            {
                senseManager.Dispose();
                senseManager = null;
            }

            if (segmentation != null)
            {
                segmentation.Dispose();
                segmentation = null;
            }
        }
コード例 #2
0
        private void AcquireThread()
        {
            // Stream data
            while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                if (click == true)
                {
                    Thread.Sleep(500);
                    click = false;
                }
                // Retrieve the results
                PXCM3DSeg segmentation = senseManager.Query3DSeg();

                if (segmentation != null)
                {
                    // Get the segmented image
                    PXCMImage segmentedImage = segmentation.AcquireSegmentedImage();

                    if (segmentedImage != null)
                    {
                        // Access the segmented image data
                        PXCMImage.ImageData segmentedImageData;
                        segmentedImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out segmentedImageData);

                        // Lock the backdrop image bitmap bits into system memory and access its data
                        // (Reference: https://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx
                        // (Reference: http://csharpexamples.com/fast-image-processing-c/)
                        Rectangle  imageRect          = new Rectangle(0, 0, WIDTH, HEIGHT);
                        BitmapData backdropBitmapData = backdrop.LockBits(imageRect, ImageLockMode.ReadWrite, backdrop.PixelFormat);
                        int        bytesPerPixel      = Bitmap.GetPixelFormatSize(backdropBitmapData.PixelFormat) / 8;
                        int        widthInBytes       = WIDTH * bytesPerPixel;

                        for (int h = 0; h < HEIGHT; h++)
                        {
                            // Use unsafe keyword to work with pointers for faster image processing
                            // (Required setting: Project -> Properties -> Build -> Allow unsafe code)
                            unsafe
                            {
                                byte *segmentedImagePixel = (byte *)segmentedImageData.planes[0] + h * segmentedImageData.pitches[0];

                                for (int w = 0; w < widthInBytes; w = w + bytesPerPixel)
                                {
                                    byte *backdropPixel = (byte *)backdropBitmapData.Scan0 + (h * backdropBitmapData.Stride);

                                    // Substitute segmented background pixels (those containing an alpha channel of zero)
                                    // with pixels from the selected backdrop image, if the checkbox is selected
                                    if ((segmentedImagePixel[3] <= 0))
                                    {
                                        segmentedImagePixel[0] = backdropPixel[w];
                                        segmentedImagePixel[1] = backdropPixel[w + 1];
                                        segmentedImagePixel[2] = backdropPixel[w + 2];
                                    }

                                    segmentedImagePixel += 4;
                                }
                            }
                        }

                        // Unlock the backdrop image bitmap bits
                        backdrop.UnlockBits(backdropBitmapData);

                        // Export the image data to a bitmap
                        Bitmap bitmap = segmentedImageData.ToBitmap(0, segmentedImage.info.width, segmentedImage.info.height);

                        // Update the UI by delegating work to the Dispatcher associated with the UI thread
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                        {
                            imgBackdrop.Source = ImageUtils.ConvertBitmapToWpf(bitmap);
                        }));

                        // Optionally save a snapshot of the image (captureSnapshot is set in the Capture button's event handler)
                        if (captureSnapshot)
                        {
                            bitmap.Save(path + "MyPic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            captureSnapshot = false;
                        }

                        segmentedImage.ReleaseAccess(segmentedImageData);
                        segmentedImage.Dispose();
                        segmentation.Dispose();
                        bitmap.Dispose();
                    }
                }

                // Resume next frame processing
                senseManager.ReleaseFrame();
            }
        }