简体   繁体   中英

Capture head image using kinect for wpf C#

I'm trying to capture an image using Microsoft Kinect around the head without the using of face detection. I manage to capture the whole camera image but not just around the head only. I have found some sample code on the internet but it doesn't seem to work on my program. Using Microsoft SDK v1.6 and Microsoft Visual Studio for my program. Can anyone help me on this? Thanks a lot XD

I tried the 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;

and also the Kinect Crop Image coding but i heard that planar image is no longer exist in SDKv.16 Kinect crop image

I have also tried https://stackoverflow.com/questions/11435544/how-do-i-save-a-croppedbitmap-to-an-image-file , but i'm having NullReferenceException was unhandled error.

double imageHeight = heightDiff * 1.2;

double imageWidth = ((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;

I got the result i wanted. Thanks alot guys!

This is how i capture the image.

The name of my image is called headImage.

private void captureImage()

    {

        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);
        }
    }

And this is the coding that i used from 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM