如何将Kinect for Windows SDK v1中的ImageColorFrame类型转换为Image<>的类型在Emgu Cv?

时间:2012-02-07 08:38:52

标签: wpf kinect emgucv

我目前正在使用Emgu CV和Kinect for Windows SDK v1进行面部检测WPF应用程序。我之前曾使用Emgu CV制作相同的应用程序,但使用网络摄像头捕捉图像帧。使用网络摄像头,获取相机框并将其转换为Image<>类型并不困难。在Emgu CV中,我可以用面部检测算法处理它。但是现在,当我使用Kinect硬件的彩色RGB相机时,我无法做到这一点。从ImageColorFrame类型(在Kinect for Windows SDK中)到Image<>类型的隐式或显式转换都不是(在Emgu CV中)不起作用。我怎么能这样做?

仅供参考,我还尝试分配Image<>类型的字节属性,而不是进行转换。像下面这段代码。但它也不起作用。提前谢谢。

using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame == null)
            {
                return;
            }

            byte[] pixels = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixels);

            nextFrame = new Image<Bgr, byte>(448, 336);
            nextFrame.Bytes = pixels;
            using (nextFrame.Bitmap = colorFrame.ToBitmap())
            {
                grayImage = new Image<Gray, byte>(448, 336);
                grayImage = nextFrame.Convert<Gray, Byte>();
                HaarCascade faceHaarCascade = new HaarCascade("" + Environment.CurrentDirectory + "\\haarcascade_frontalface_alt_tree.xml");

                MCvAvgComp[][] facesDetected = grayImage.DetectHaarCascade(
                   faceHaarCascade,
                   1.1,
                   10,
                   Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                   new System.Drawing.Size(20, 20));

                foreach (MCvAvgComp face in facesDetected[0])
                {
                    // draw rectangle in the facial image
                    nextFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 2);
                }
                imageVideoRGB.Source = ToBitmapSource(nextFrame);
            }
        }

1 个答案:

答案 0 :(得分:0)

要将Microsoft.Kinect.ImageColorFrame转换为Emgu.CV.Image,您应该尝试这样做:

var bitmapSource = colorImageFrame.ToBitmapSource(); // using Coding4Fun.Kinect.Wpf extension
//Get a bitmap
var currentImage = KinectHelpers.GetBitmapFromBitmapSource(ColorBitmap);
// Then get the Image
var image = new Image<Bgr, Byte>(currentImage)

KinectHelpers是一个静态类:

public static class KinectHelpers
{
    public static Bitmap GetBitmapFromBitmapSource(BitmapSource bSource)
    {
        Bitmap bmp;
        using (MemoryStream ms = new MemoryStream())
        {
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create(bSource));
            enc.Save(ms);
            bmp = new Bitmap(ms);
        }
        return bmp;
    }
}