Kinect crop image
Asked Answered
C

1

7

I'm trying to crop a rectangular region of a video RGB. First i found the coordinates of head joint and with this coordinates i drew a rectangle over the RGB video. Now i want to show in another video just the image that is inside of the rentangle in the first image. Any help would be great.

video RGB is displayed in a "RGBvideo" Image Control. cropped image i want to display in a "faceImage" Image Control

I've search online but couldn't find a solution to it. I am confuse.

thank you so much

Carleycarli answered 7/10, 2011 at 2:17 Comment(0)
H
11

Welcome to Stack Overflow, please don't ask the same question multiple times. With less popular tags like Kinect, it can take some time for people to answer (the tag only has 79 followers).

For simplicity, I'm going to assume you want to crop out a set size image (for example, 60x60 pixels out of the original 800x600). In your VideoFrameReady method, you get the PlanarImage from the event args. This PlanarImage has the bits field, which contains all of the RGB data for the image. With a little math, you can cut out a small chunk of that data and use it as a smaller image.

// update video feeds
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
{
    PlanarImage image = e.ImageFrame.Image;

    // Large video feed
    video.Source = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel);

    // X and Y coordinates of the smaller image, and the width and height of smaller image
    int xCoord = 100, yCoord = 150, width = 60, height = 60;

    // Create an array to copy data into
    byte[] bytes = new byte[width * height * image.BytesPerPixel];

    // Copy over the relevant bytes
    for (int i = 0; i < height; i++) 
    {
        for (int j = 0; j < width * image.BytesPerPixel; j++)
        {
            bytes[i * (width * image.BytesPerPixel) + j] = image.Bits[(i + yCoord) * (image.Width * image.BytesPerPixel) + (j + xCoord * image.BytesPerPixel)];
        }
    }

    // Create the smaller image
    smallVideo.Source = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, null, bytes, width * image.BytesPerPixel);
}

Please make sure you understand the code, instead of just copy/pasting it. The two for loops are for basic array copying, with the number of bytes per pixel taken into account (4 for BGR32). Then you use that small subset of the original data to create a new BitmapSource. You'll need to you can change the width/height as you see fit, and determine the X and Y coordinates from the head tracking.

Hilaire answered 7/10, 2011 at 5:52 Comment(5)
Thank you for your help! But this will just stay in a position. So if I want to track my head, when my head move, smallVideo will move as well to fit my head into the image control. how to do so? What you gave has been a great help to me anyway!Carleycarli
@user981924: what you'll need to do is make the xCoord and yCoord variables global. Then, on the SkeletonFrameReady event, change those variables depending on the location of the head. Just make sure you keep the values within bounds (if xCoord or yCoord is less than 0 for example, you risk an OutOfBoundsException).Hilaire
Fixed an error in the code, apparently didn't test the code very well.Hilaire
Okay, thank you! i manage to follow my head somehow but the color of the Image control(smallVideo) keep changing from purple to green to white to bla bla bla. i mean keep changing, like it's unstable. but anyway, it is working now. Thank You!Carleycarli
@user981924: the color changing is from a mistake I made in the code. In the byte assignment, xCoord should be multiplied by image.BytesPerPixel on the right half of the equation. Update that and try it again. I've edited the code in the post to reflect that change.Hilaire

© 2022 - 2024 — McMap. All rights reserved.