1

I'm working on a project for school involving C#, Kinect, and Emgu CV. I fairly new to both C# and Emgu CV, so I may be missing something simple. What I am trying to do is use the image from the Kinect as an Emgu CV image for processing, but I keep getting an error. The error that comes up is "TypeInitializationException was unhandled" and "The type initializer for 'Emgu.Cv.CVInvoke' threw an exception."

The code I'm using to get the image from the Kinect is:

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;
    }
}

I also found some code that I am using to convert the BitmapSource to a Bitmap from: Is there a good way to convert between BitmapSource and Bitmap?

The code that isn't working is as follows:

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

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

From what I can find, this should convert the Bitmap into an Emgu CV image but for some reason it isn't.

Just some more information:

InnerException: Make sure the file image is a valid managed assembly.

InnerException: Make sure you have supplied a correct file path for the assembly.

My best guess is that the program is using different versions of the .NET Framework, but I am unsure of how to fix this problem.

Community
  • 1
  • 1

2 Answers2

0

I got the same problem and a semi-suitable solution. So i am also hoping that somebody has a good idea.

What I currently do is convert the image pixel by pixel:

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;
        }
    }
}

However, this approach is quite slow, so I process only every nth sample.

It would be awesome if someone has a better/faster solution :)

Thanks,

Flo

Flo
  • 540
  • 6
  • 20
0

The TypeInitializationException is likely related to the binary not being able to find the necessary DLLs of Emgu CV.

This questions solves your problem: EmguCV TypeInitializationException

Also if you want to convert a ColorFrame from the Kinect v2 into a Emgu CV processable image (Image<Bgra,byte>) you can use this function:

/**
 * 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;
    }
Community
  • 1
  • 1
Hedge
  • 16,142
  • 42
  • 141
  • 246