【问题标题】:'System.AccessViolationException' while assigning `Emgu.CV.Mat` to `Emgu.CV.UI.ImageBox` (Emgu)将“Emgu.CV.Mat”分配给“Emgu.CV.UI.ImageBox”时出现“System.AccessViolationException”(Emgu)
【发布时间】:2017-03-08 23:08:51
【问题描述】:

您好,我正在尝试将Emgu.CV.Mat 分配给Emgu.CV.UI.ImageBox,但我在CvInvokeCore.cs 文件中不断收到System.AccessViolationException 错误(第2379 行 cveMinMaxLoc(iaArr, ref minVal, ref maxVal, ref minLoc, ref maxLoc, iaMask);):

Emgu.CV.World.dll 中出现“System.AccessViolationException”类型的未处理异常

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

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

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

对此有何建议?

【问题讨论】:

  • 请不要发布指向某些外部位置的普通链接。链接目标可能会在一段时间内变得不可用,因此您的问题将丢失重要信息。您应该在问题本身中提供所有相关代码 sn-ps。
  • 我更新了帖子

标签: c# emgucv access-violation mat


【解决方案1】:

使用图片

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

如果你需要 Mat 类型,只需使用 img.Mat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多