Parallel.ForEach 循环中的 AccessViolationException
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案:我必须将 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.