1

私は、C#、Kinect、および Emgu CV を含む学校向けのプロジェクトに取り組んでいます。私は C# と Emgu CV の両方にかなり慣れていないので、単純なものが欠けている可能性があります。私がやろうとしているのは、Kinect の画像を処理用の Emgu CV 画像として使用することですが、エラーが発生し続けます。表示されるエラーは、「TypeInitializationException was unhandled」と「The type initializer for 'Emgu.Cv.CVInvoke' throw an exception.」です。

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 を Bitmap に変換するために使用しているコードもいくつか見つけました

動作しないコードは次のとおりです。

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

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

私が見つけたものから、これはビットマップを Emgu CV イメージに変換するはずですが、何らかの理由でそうではありません。

もう少し情報:

InnerException: ファイル イメージが有効なマネージド アセンブリであることを確認してください。

InnerException: アセンブリに正しいファイル パスを指定したことを確認してください。

プログラムが異なるバージョンの .NET Framework を使用していると推測されますが、この問題を解決する方法がわかりません。

4

2 に答える 2

0

私は同じ問題と半適切な解決策を得ました。だから私も誰かが良い考えを持っていることを望んでいます。

私が現在行っているのは、画像をピクセルごとに変換することです。

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 個ごとのサンプルのみを処理します。

誰かがより良い/より速い解決策を持っていれば素晴らしいでしょう:)

ありがとう、

フロー

于 2013-02-20T18:03:11.377 に答える
0

これTypeInitializationExceptionは、Emgu CV の必要な DLL を見つけることができないバイナリに関連している可能性があります。

この質問はあなたの問題を解決します: EmguCV TypeInitializationException

またColorFrame、Kinect v2 から Emgu CV 処理可能な画像 ( Image<Bgra,byte>) に変換する場合は、次の関数を使用できます。

/**
 * 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;
    }
于 2015-02-08T18:21:52.863 に答える