简体   繁体   中英

How can I flip the Kinect v2 colorstream horizontally?

I recently set-up my Kinect v2 on my win 10 PC. Intending to use it for some VR mixed reality streaming I have found that the video feed is mirrored and there is no way to flip it.

I have attempted to modify the Coordinate Mapping Basics program in the SDK to be a green screen and to flip the image. The green I have done but I can not get the colorstream to flip!

I have tried turning the WriteableBitmap to a bitmap, exporting it an array then flipping the array and writing it back to a WritableBitmap but that didn't work.

I have tried installing WriteableBitmapEx library and using the flip function but the program just freezes.

Any suggestions?

Long term I also want the program to act as a virtual webcam to pass the video feed straight to a compositor, but one step at a time.

UPDATE: I Have got the image to flip BUT its at a HUGE performance hit. Wrote this to convert the WriteableBitmap into a byte Array then reverse the order. Doing some tests its the loop which is killing the performance. Any alternatives or ways to optimise the loop?

private void flipBitmap(int method)
    {            
        if (method == 1)//works but slow.
        {
            var width = this.bitmap.PixelWidth;
            var height = this.bitmap.PixelHeight;
            var stride = ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8) * width;
            int current, flipped = 0;
            var raw= new byte[stride * height];
            this.bitmap.CopyPixels(bitmapData, stride, 0);
            var flippedPixels = new byte[raw.Length];
            for (int y = 0; y < height; y++)
            {

                for (int x = 4; x < width; x++)
                {
                    current = (y * stride) + (x * 4);
                    flipped = (y * stride) + (width - x) * 4;
                    for (int i = 0; i < 3; i++)
                    {
                        flippedPixels[flipped + i] = raw[current + i];
                    }
                    flippedPixels[flipped + 3] = 255;
                }
            }
            this.bitmap.WritePixels(new Int32Rect(0, 0, width, height), flippedPixels, stride, 0);
        }
        else if(method == 2)//placeholder
        {
            return;
        }
        else 
        {
            return;
        }                        
    }

UPDATE 2: I now have it running at what looks like 30fps. Just got to get the performance better! I pulled the variables out so it was not making new arrays all the time, just recycling the old ones. Now thinking about running parallel for loops, one going from the start of the array, the other from the end. No idea if that will speed it up or use more/less processing power. Right now the program uses 40-50% of my cpu which is insane and much more than I can do as well as VR. Still interested in faster/more efficient ways to flip the image if anyone has them.

UPDATE 3: With A little more tinkering and cleaning of old experiments the performance has improved but its still using twice the cpu the nonflipped version uses.

private void flipBitmap(int method)
    {

        if (method == 1)//works but cpu intensive.
        {           
            int current, flipped = 0;
            this.bitmap.CopyPixels(raw, stride, 0);                

            for (int y = 0; y < height; y++)
            {

                for (int x = 4; x < width; x++)
                {
                    current = (y * stride) + (x * 4);
                    flipped = (y * stride) + (width - x) * 4;

                    flippedPixels[flipped + 0] = raw[current + 0];
                    flippedPixels[flipped + 1] = raw[current + 1];
                    flippedPixels[flipped + 2] = raw[current + 2];
                    flippedPixels[flipped + 3] = 255;                        
                }
            }
            this.bitmap.WritePixels(new Int32Rect(0, 0, width, height), flippedPixels, stride, 0);

            return;
        }
        else if(method == 2)//placeholder
        {               
            return;
        }
        else 
        {                
            return;
        }                        
    }

Have you tried this? https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx

It looks like exactly what you're looking for...

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