1

我正在尝试在不使用面部检测的情况下在头部周围使用 Microsoft Kinect 捕获图像。我设法捕捉到整个相机图像,但不仅仅是头部周围。我在互联网上找到了一些示例代码,但它似乎不适用于我的程序。在我的程序中使用 Microsoft SDK v1.6 和 Microsoft Visual Studio。谁可以帮我这个事?非常感谢XD

我尝试了 CroppedBitmap

CroppedBitmap croppedImage = new CroppedBitmap();
              croppedImage.BeginInit();
              croppedImage.Source = colorImageBitmap;
              croppedImage.SourceRect = new Int32Rect((int)headPos.X, (int)headPos.Y, (int)imageHeight, (int)imageWidth);
              croppedImage.EndInit();
              headImage.Source = croppedImage;

还有 Kinect 裁剪图像编码,但我听说 SDKv.16 Kinect 裁剪图像中不再存在平面 图像

我也试过https://stackoverflow.com/questions/11435544/how-do-i-save-a-croppedbitmap-to-an-image-file,但我有 NullReferenceException 是未处理的错误。

双图像高度 = heightDiff * 1.2;

双图像宽度 = ((heightDiff * 1.2) / 2);

            Int32Rect cropRect = new Int32Rect((int)headPos.X, (int)headPos.Y, (int)imageWidth, (int)imageHeight);

            this.headImageBitmap = new WriteableBitmap(this.headImageBitmap.PixelWidth, this.headImageBitmap.PixelHeight, 96, 96, PixelFormats.Bgr32, null);

            this.headImageBitmap.WritePixels(new Int32Rect((int)headPos.X, (int)headPos.Y, (int)imageWidth, (int)imageHeight), colorData, (int)imageWidth * colorFrame.BytesPerPixel, 0);

            CroppedBitmap croppedImage = new CroppedBitmap(colorImageBitmap, cropRect);
            headImage.Source = croppedImage;
4

1 回答 1

0

我得到了我想要的结果。非常感谢你们!

这就是我捕捉图像的方式。

我的图像的名称称为 headImage。

私人无效捕获图像()

    {

        BitmapEncoder encoder = new JpegBitmapEncoder();

        encoder.Frames.Add(BitmapFrame.Create(this.croppedImage));

        string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentCulture.DateTimeFormat);

        string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

        string path = System.IO.Path.Combine(myPhotos, "KinectSnapShot-" + time + ".jpg");

        try
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                encoder.Save(fs);
            }
            imageTB.Text = string.Format("{0} {1}", Properties.Resources.ScreenshotWriteSuccess, path);
        }
        catch (IOException)
        {
            imageTB.Text = string.Format("{0} {1}", Properties.Resources.ScreenshotWriteFailed, path);
        }
    }

这是我从croppedBitmap中使用的编码

Int32Rect cropRect = new Int32Rect((int)(headPos.X * 0.8), (int)(headPos.Y * 0.7), (int)headImage.Width, (int)headImage.Height);

            this.headImageBitmap = new WriteableBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null);

            this.headImageBitmap.WritePixels(new Int32Rect((int)headPos.X, (int)headPos.Y, (int)headImage.Width, (int)headImage.Height), colorData, (int)headImage.Width * colorFrame.BytesPerPixel, 0);

            croppedImage = new CroppedBitmap(colorImageBitmap, cropRect);

            headImage.Source = croppedImage;
于 2013-03-01T05:48:26.697 回答