& # 39; System.AccessViolationException& # 39;同时分配“Emgu.CV”.

本文关键字:Emgu CV System AccessViolationException 分配 | 更新日期: 2023-09-27 18:15:16

嗨,我要做的是将Emgu.CV.Mat分配给Emgu.CV.UI.ImageBox,但我一直在CvInvokeCore.cs文件(第2379行 cveMinMaxLoc(iaArr, ref minVal, ref maxVal, ref minLoc, ref maxLoc, iaMask);)中获得System.AccessViolationException错误:

类型为"System"的未处理异常。AccessViolationException'发生在Emgu.CV.World.dll

附加信息:试图读写受保护的内存。这通常表明其他内存已损坏。

代码非常简单。生成一个位图

Bitmap tmp = mybitmap.getBitmap(); // generates my Bitmap
pictureBox1.Image = tmp; // assigning it to an Win Form picture Box works pretty fine
imageBox1.Image = ConvertBitmapToMat(tmp); // but converting the Bitmap to an Mat file 
                                           // and try to assign it to an 
                                           // Emgu.CV.UI.ImageBox throws the
                                           // error in the above mentioned file.

我使用的ConvertBitmapToMat()代码是:

public Mat ConvertBitmapToMat(Bitmap bmp)
{
   // Lock the bitmap's bits.  
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData =
        bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);
    // data = scan0 is a pointer to our memory block.
    IntPtr data = bmpData.Scan0;
    // step = stride = amount of bytes for a single line of the image
    int step = bmpData.Stride;
    // So you can try to get you Mat instance like this:
    Mat mat = new Mat(bmp.Height, bmp.Width, Emgu.CV.CvEnum.DepthType.Cv32F, 4, data, step);
    // Unlock the bits.
    bmp.UnlockBits(bmpData);
    return mat;
}

发现于http://avidprogrammer.blogspot.de/2016/05/emgucv-c-convert-bitmap-object-to-mat.html

有什么建议吗?

& # 39; System.AccessViolationException& # 39;同时分配“Emgu.CV”.

使用

Bitmap tmp = mybitmap.getBitmap();
Image<Bgr,Byte> img = new Image<Bgr,Byte>(tmp);
imageBox1.Image = img;

如果需要Mat类型,只需使用img。垫