Parallel.ForEach 循环中的 AccessViolationException

发布于 2024-10-10 13:18:41 字数 1711 浏览 4 评论 0原文

我正在尝试使用 EmguCV 在 .NET 中执行此代码:

Parallel.ForEach<Bitmap>(GetImagesFromCamera(), bmp =>
{
    Image<Gray, Byte> src = new Image<Gray, Byte>(bmp);
    Image<Gray, Byte> dest = new Image<Gray, Byte>(src.Width, src.Height);
    CvInvoke.cvThreshold(src, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
    form.setImage(dest);
});

这是我的 GetPicturesFromCamera() 方法:

static IEnumerable<Bitmap> GetImagesFromCamera()
{
    Camera cam = new Camera();
    cam.StartCapture();
    while (true)
    {
        yield return new Image<Gray, Byte>(cam.getBitmap());// <=== AccessViolationException
    }
}

在标记点处,我总是收到一个 AccessViolationException,显示“尝试读取或写入受保护的内存。这通常表明其他内存正在使用”腐败。”, 但我没有发现错误。

你能弄清楚吗?

编辑:

我也按顺序尝试过,这样它可以工作:

foreach (var bmp in GetImagesFromCamera())
        {
            Image<Gray, Byte> dest = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            CvInvoke.cvThreshold(bmp, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            form.setImage(dest);
        };

但这样它不会:

foreach (var bmp in GetImagesFromCamera())
        {
            Image<Gray, Byte> dest = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            Image<Gray, Byte> tmp = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            CvInvoke.cvThreshold(bmp, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            CvInvoke.cvErode(dest, tmp, IntPtr.Zero, 2);
            CvInvoke.cvDilate(tmp, dest, IntPtr.Zero, 2);
            form.setImage(dest);
        };

I'm trying to execute this code in .NET using EmguCV:

Parallel.ForEach<Bitmap>(GetImagesFromCamera(), bmp =>
{
    Image<Gray, Byte> src = new Image<Gray, Byte>(bmp);
    Image<Gray, Byte> dest = new Image<Gray, Byte>(src.Width, src.Height);
    CvInvoke.cvThreshold(src, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
    form.setImage(dest);
});

This is my GetPicturesFromCamera() Method:

static IEnumerable<Bitmap> GetImagesFromCamera()
{
    Camera cam = new Camera();
    cam.StartCapture();
    while (true)
    {
        yield return new Image<Gray, Byte>(cam.getBitmap());// <=== AccessViolationException
    }
}

At the marked point I always get a AccessViolationException saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.",
but I don't find the mistake.

Can you figure it out?

EDIT:

I tried it sequencialy too, this way it works:

foreach (var bmp in GetImagesFromCamera())
        {
            Image<Gray, Byte> dest = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            CvInvoke.cvThreshold(bmp, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            form.setImage(dest);
        };

but this way it doesn't:

foreach (var bmp in GetImagesFromCamera())
        {
            Image<Gray, Byte> dest = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            Image<Gray, Byte> tmp = new Image<Gray, Byte>(bmp.Width, bmp.Height);
            CvInvoke.cvThreshold(bmp, dest, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            CvInvoke.cvErode(dest, tmp, IntPtr.Zero, 2);
            CvInvoke.cvDilate(tmp, dest, IntPtr.Zero, 2);
            form.setImage(dest);
        };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-10-17 13:18:41

我找到了一个解决方案:我必须将 Camera 类中的所有代码放入 GetImagesFromCamera 方法中,但我不知道为什么。

I found a solution: I had to put all the code in the Camera class into the GetImagesFromCamera method, but I have no idea why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文