【问题标题】:Trouble using Kinect ColorImageFrame with Emgu CV在 Emgu CV 中使用 Kinect ColorImageFrame 时遇到问题
【发布时间】:2013-02-15 03:44:00
【问题描述】:

我正在为学校开展一个涉及 C#、Kinect 和 Emgu CV 的项目。我对 C# 和 Emgu CV 都很陌生,所以我可能会遗漏一些简单的东西。我想要做的是使用来自 Kinect 的图像作为 Emgu CV 图像进行处理,但我不断收到错误消息。出现的错误是“TypeInitializationException 未处理”和“'Emgu.Cv.CVInvoke' 的类型初始化程序引发了异常。”

我用来从 Kinect 获取图像的代码是:

using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
{
    if (colorFrame != null)
    {           
        byte[] pixels = new byte[colorFrame.PixelDataLength];
        colorFrame.CopyPixelDataTo(pixels);

        int stride = colorFrame.Width * 4;
        BitmapSource color = BitmapImage.Create(colorFrame.Width, colorFrame.Height,96, 96, PixelFormats.Bgr32, null, pixels, stride);
        liveFeed.Source = color;

        EmguCVProcessing(color); 
    }
    else
    {
        return;
    }
}

我还发现了一些用于将 BitmapSource 转换为位图的代码:Is there a good way to convert between BitmapSource and Bitmap?

不起作用的代码如下:

void EmguCVProcessing(BitmapSource bitmap)
{
    Bitmap bmp = GetBitmap(bitmap);

    Image<Bgr, Byte> imgLiveFeed = new Image<Bgr, Byte>(bmp);
}

据我所知,这应该将位图转换为 Emgu CV 图像,但由于某种原因它不是。

更多信息:

InnerException:确保文件图像是有效的托管程序集。

InnerException: 确保您为程序集提供了正确的文件路径。

我的最佳猜测是该程序使用不同版本的 .NET Framework,但我不确定如何解决此问题。

【问题讨论】:

    标签: c# kinect emgucv computer-vision


    【解决方案1】:

    我遇到了同样的问题和一个半合适的解决方案。所以我也希望有人有一个好主意。

    我目前做的是逐像素转换图像:

    private KinectSensor sensor;
    private byte[] colorPixels;
    private Image<Gray, UInt16> grayImage;
    // during init:
    this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
    grayImage = new Emgu.CV.Image<Gray, UInt16>(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, new Gray(0));
    
    // the callback for the Kinect SDK
    private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                // Copy the pixel data from the image to a temporary array
                colorFrame.CopyPixelDataTo(this.colorPixels);
    
                int width = 640;
                int height = 480;
                int bytesPerPx = 2;
                if (nthShot == 0)
                {
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            grayImage[height - y -1, x] = new Gray(((this.colorPixels[(y * width + x) * bytesPerPx + 1])));
                        }
                    }
                    // *** processing of the image comes here ***
                }
                nthShot++;
                if (nthShot == 4)
                    nthShot = 0;
            }
        }
    }
    

    但是,这种方法很慢,所以我只处理每第 n 个样本。

    如果有人有更好/更快的解决方案,那就太棒了:)

    谢谢,

    弗洛

    【讨论】:

      【解决方案2】:

      TypeInitializationException 可能与无法找到 Emgu CV 的必要 DLL 的二进制文件有关。

      这个问题解决了你的问题: EmguCV TypeInitializationException

      此外,如果您想将 Kinect v2 中的 ColorFrame 转换为 Emgu CV 可处理图像 (Image&lt;Bgra,byte&gt;),您可以使用此功能:

      /**
       * Converts the ColorFrame of the Kinect v2 to an image applicable for Emgu CV
       */
          public static Image<Bgra, byte> ToImage(this ColorFrame frame)
          {
              int width = frame.FrameDescription.Width;
              int height = frame.FrameDescription.Height;
              PixelFormat format = PixelFormats.Bgr32;
      
              byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)];
      
              if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
              {
                  frame.CopyRawFrameDataToArray(pixels);
              }
              else
              {
                  frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
              }
      
              Image<Bgra, byte> img = new Image<Bgra, byte>(width, height);
              img.Bytes = pixels;
      
              return img;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多